ox-rss.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. ;;; ox-rss.el --- RSS 2.0 Back-End for Org Export Engine
  2. ;; Copyright (C) 2013-2015 Bastien Guerry
  3. ;; Author: Bastien Guerry <bzg@gnu.org>
  4. ;; Keywords: org, wp, blog, feed, rss
  5. ;; This file is not yet part of GNU Emacs.
  6. ;; This program is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This library implements a RSS 2.0 back-end for Org exporter, based on
  18. ;; the `html' back-end.
  19. ;;
  20. ;; It requires Emacs 24.1 at least.
  21. ;;
  22. ;; It provides two commands for export, depending on the desired output:
  23. ;; `org-rss-export-as-rss' (temporary buffer) and `org-rss-export-to-rss'
  24. ;; (as a ".xml" file).
  25. ;;
  26. ;; This backend understands two new option keywords:
  27. ;;
  28. ;; #+RSS_EXTENSION: xml
  29. ;; #+RSS_IMAGE_URL: http://myblog.org/mypicture.jpg
  30. ;;
  31. ;; It uses #+HTML_LINK_HOME: to set the base url of the feed.
  32. ;;
  33. ;; Exporting an Org file to RSS modifies each top-level entry by adding a
  34. ;; PUBDATE property. If `org-rss-use-entry-url-as-guid', it will also add
  35. ;; an ID property, later used as the guid for the feed's item.
  36. ;;
  37. ;; The top-level headline is used as the title of each RSS item unless
  38. ;; an RSS_TITLE property is set on the headline.
  39. ;;
  40. ;; You typically want to use it within a publishing project like this:
  41. ;;
  42. ;; (add-to-list
  43. ;; 'org-publish-project-alist
  44. ;; '("homepage_rss"
  45. ;; :base-directory "~/myhomepage/"
  46. ;; :base-extension "org"
  47. ;; :rss-image-url "http://lumiere.ens.fr/~guerry/images/faces/15.png"
  48. ;; :html-link-home "http://lumiere.ens.fr/~guerry/"
  49. ;; :html-link-use-abs-url t
  50. ;; :rss-extension "xml"
  51. ;; :publishing-directory "/home/guerry/public_html/"
  52. ;; :publishing-function (org-rss-publish-to-rss)
  53. ;; :section-numbers nil
  54. ;; :exclude ".*" ;; To exclude all files...
  55. ;; :include ("index.org") ;; ... except index.org.
  56. ;; :table-of-contents nil))
  57. ;;
  58. ;; ... then rsync /home/guerry/public_html/ with your server.
  59. ;;
  60. ;; By default, the permalink for a blog entry points to the headline.
  61. ;; You can specify a different one by using the :RSS_PERMALINK:
  62. ;; property within an entry.
  63. ;;; Code:
  64. (require 'ox-html)
  65. (declare-function url-encode-url "url-util" (url))
  66. ;;; Variables and options
  67. (defgroup org-export-rss nil
  68. "Options specific to RSS export back-end."
  69. :tag "Org RSS"
  70. :group 'org-export
  71. :version "24.4"
  72. :package-version '(Org . "8.0"))
  73. (defcustom org-rss-image-url "http://orgmode.org/img/org-mode-unicorn-logo.png"
  74. "The URL of the an image for the RSS feed."
  75. :group 'org-export-rss
  76. :type 'string)
  77. (defcustom org-rss-extension "xml"
  78. "File extension for the RSS 2.0 feed."
  79. :group 'org-export-rss
  80. :type 'string)
  81. (defcustom org-rss-categories 'from-tags
  82. "Where to extract items category information from.
  83. The default is to extract categories from the tags of the
  84. headlines. When set to another value, extract the category
  85. from the :CATEGORY: property of the entry."
  86. :group 'org-export-rss
  87. :type '(choice
  88. (const :tag "From tags" from-tags)
  89. (const :tag "From the category property" from-category)))
  90. (defcustom org-rss-use-entry-url-as-guid t
  91. "Use the URL for the <guid> metatag?
  92. When nil, Org will create ids using `org-icalendar-create-uid'."
  93. :group 'org-export-rss
  94. :type 'boolean)
  95. ;;; Define backend
  96. (org-export-define-derived-backend 'rss 'html
  97. :menu-entry
  98. '(?r "Export to RSS"
  99. ((?R "As RSS buffer"
  100. (lambda (a s v b) (org-rss-export-as-rss a s v)))
  101. (?r "As RSS file" (lambda (a s v b) (org-rss-export-to-rss a s v)))
  102. (?o "As RSS file and open"
  103. (lambda (a s v b)
  104. (if a (org-rss-export-to-rss t s v)
  105. (org-open-file (org-rss-export-to-rss nil s v)))))))
  106. :options-alist
  107. '((:description "DESCRIPTION" nil nil newline)
  108. (:keywords "KEYWORDS" nil nil space)
  109. (:with-toc nil nil nil) ;; Never include HTML's toc
  110. (:rss-extension "RSS_EXTENSION" nil org-rss-extension)
  111. (:rss-image-url "RSS_IMAGE_URL" nil org-rss-image-url)
  112. (:rss-categories nil nil org-rss-categories))
  113. :filters-alist '((:filter-final-output . org-rss-final-function))
  114. :translate-alist '((headline . org-rss-headline)
  115. (comment . (lambda (&rest args) ""))
  116. (comment-block . (lambda (&rest args) ""))
  117. (timestamp . (lambda (&rest args) ""))
  118. (plain-text . org-rss-plain-text)
  119. (section . org-rss-section)
  120. (template . org-rss-template)))
  121. ;;; Export functions
  122. ;;;###autoload
  123. (defun org-rss-export-as-rss (&optional async subtreep visible-only)
  124. "Export current buffer to a RSS buffer.
  125. If narrowing is active in the current buffer, only export its
  126. narrowed part.
  127. If a region is active, export that region.
  128. A non-nil optional argument ASYNC means the process should happen
  129. asynchronously. The resulting buffer should be accessible
  130. through the `org-export-stack' interface.
  131. When optional argument SUBTREEP is non-nil, export the sub-tree
  132. at point, extracting information from the headline properties
  133. first.
  134. When optional argument VISIBLE-ONLY is non-nil, don't export
  135. contents of hidden elements.
  136. Export is done in a buffer named \"*Org RSS Export*\", which will
  137. be displayed when `org-export-show-temporary-export-buffer' is
  138. non-nil."
  139. (interactive)
  140. (let ((file (buffer-file-name (buffer-base-buffer))))
  141. (org-icalendar-create-uid file 'warn-user)
  142. (org-rss-add-pubdate-property))
  143. (org-export-to-buffer 'rss "*Org RSS Export*"
  144. async subtreep visible-only nil nil (lambda () (text-mode))))
  145. ;;;###autoload
  146. (defun org-rss-export-to-rss (&optional async subtreep visible-only)
  147. "Export current buffer to a RSS file.
  148. If narrowing is active in the current buffer, only export its
  149. narrowed part.
  150. If a region is active, export that region.
  151. A non-nil optional argument ASYNC means the process should happen
  152. asynchronously. The resulting file should be accessible through
  153. the `org-export-stack' interface.
  154. When optional argument SUBTREEP is non-nil, export the sub-tree
  155. at point, extracting information from the headline properties
  156. first.
  157. When optional argument VISIBLE-ONLY is non-nil, don't export
  158. contents of hidden elements.
  159. Return output file's name."
  160. (interactive)
  161. (let ((file (buffer-file-name (buffer-base-buffer))))
  162. (org-icalendar-create-uid file 'warn-user)
  163. (org-rss-add-pubdate-property))
  164. (let ((outfile (org-export-output-file-name
  165. (concat "." org-rss-extension) subtreep)))
  166. (org-export-to-file 'rss outfile async subtreep visible-only)))
  167. ;;;###autoload
  168. (defun org-rss-publish-to-rss (plist filename pub-dir)
  169. "Publish an org file to RSS.
  170. FILENAME is the filename of the Org file to be published. PLIST
  171. is the property list for the given project. PUB-DIR is the
  172. publishing directory.
  173. Return output file name."
  174. (let ((bf (get-file-buffer filename)))
  175. (if bf
  176. (with-current-buffer bf
  177. (org-icalendar-create-uid filename 'warn-user)
  178. (org-rss-add-pubdate-property)
  179. (write-file filename))
  180. (find-file filename)
  181. (org-icalendar-create-uid filename 'warn-user)
  182. (org-rss-add-pubdate-property)
  183. (write-file filename) (kill-buffer)))
  184. (org-publish-org-to
  185. 'rss filename (concat "." org-rss-extension) plist pub-dir))
  186. ;;; Main transcoding functions
  187. (defun org-rss-headline (headline contents info)
  188. "Transcode HEADLINE element into RSS format.
  189. CONTENTS is the headline contents. INFO is a plist used as a
  190. communication channel."
  191. (unless (or (org-element-property :footnote-section-p headline)
  192. ;; Only consider first-level headlines
  193. (> (org-export-get-relative-level headline info) 1))
  194. (let* ((author (and (plist-get info :with-author)
  195. (let ((auth (plist-get info :author)))
  196. (and auth (org-export-data auth info)))))
  197. (htmlext (plist-get info :html-extension))
  198. (hl-number (org-export-get-headline-number headline info))
  199. (hl-home (file-name-as-directory (plist-get info :html-link-home)))
  200. (hl-pdir (plist-get info :publishing-directory))
  201. (hl-perm (org-element-property :RSS_PERMALINK headline))
  202. (anchor (org-export-get-reference headline info))
  203. (category (org-rss-plain-text
  204. (or (org-element-property :CATEGORY headline) "") info))
  205. (pubdate0 (org-element-property :PUBDATE headline))
  206. (pubdate (let ((system-time-locale "C"))
  207. (if pubdate0
  208. (format-time-string
  209. "%a, %d %b %Y %H:%M:%S %z"
  210. (org-time-string-to-time pubdate0)))))
  211. (title (or (org-element-property :RSS_TITLE headline)
  212. (replace-regexp-in-string
  213. org-bracket-link-regexp
  214. (lambda (m) (or (match-string 3 m)
  215. (match-string 1 m)))
  216. (org-element-property :raw-value headline))))
  217. (publink
  218. (or (and hl-perm (concat (or hl-home hl-pdir) hl-perm))
  219. (concat
  220. (or hl-home hl-pdir)
  221. (file-name-nondirectory
  222. (file-name-sans-extension
  223. (plist-get info :input-file))) "." htmlext "#" anchor)))
  224. (guid (if org-rss-use-entry-url-as-guid
  225. publink
  226. (org-rss-plain-text
  227. (or (org-element-property :ID headline)
  228. (org-element-property :CUSTOM_ID headline)
  229. publink)
  230. info))))
  231. (if (not pubdate0) "" ;; Skip entries with no PUBDATE prop
  232. (format
  233. (concat
  234. "<item>\n"
  235. "<title>%s</title>\n"
  236. "<link>%s</link>\n"
  237. "<author>%s</author>\n"
  238. "<guid isPermaLink=\"false\">%s</guid>\n"
  239. "<pubDate>%s</pubDate>\n"
  240. (org-rss-build-categories headline info) "\n"
  241. "<description><![CDATA[%s]]></description>\n"
  242. "</item>\n")
  243. title publink author guid pubdate contents)))))
  244. (defun org-rss-build-categories (headline info)
  245. "Build categories for the RSS item."
  246. (if (eq (plist-get info :rss-categories) 'from-tags)
  247. (mapconcat
  248. (lambda (c) (format "<category><![CDATA[%s]]></category>" c))
  249. (org-element-property :tags headline)
  250. "\n")
  251. (let ((c (org-element-property :CATEGORY headline)))
  252. (format "<category><![CDATA[%s]]></category>" c))))
  253. (defun org-rss-template (contents info)
  254. "Return complete document string after RSS conversion.
  255. CONTENTS is the transcoded contents string. INFO is a plist used
  256. as a communication channel."
  257. (concat
  258. (format "<?xml version=\"1.0\" encoding=\"%s\"?>"
  259. (symbol-name org-html-coding-system))
  260. "\n<rss version=\"2.0\"
  261. xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"
  262. xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"
  263. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  264. xmlns:atom=\"http://www.w3.org/2005/Atom\"
  265. xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"
  266. xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"
  267. xmlns:georss=\"http://www.georss.org/georss\"
  268. xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"
  269. xmlns:media=\"http://search.yahoo.com/mrss/\">"
  270. "<channel>"
  271. (org-rss-build-channel-info info) "\n"
  272. contents
  273. "</channel>\n"
  274. "</rss>"))
  275. (defun org-rss-build-channel-info (info)
  276. "Build the RSS channel information."
  277. (let* ((system-time-locale "C")
  278. (title (plist-get info :title))
  279. (email (org-export-data (plist-get info :email) info))
  280. (author (and (plist-get info :with-author)
  281. (let ((auth (plist-get info :author)))
  282. (and auth (org-export-data auth info)))))
  283. (date (format-time-string "%a, %d %b %Y %H:%M:%S %z")) ;; RFC 882
  284. (description (org-export-data (plist-get info :description) info))
  285. (lang (plist-get info :language))
  286. (keywords (plist-get info :keywords))
  287. (rssext (plist-get info :rss-extension))
  288. (blogurl (or (plist-get info :html-link-home)
  289. (plist-get info :publishing-directory)))
  290. (image (url-encode-url (plist-get info :rss-image-url)))
  291. (ifile (plist-get info :input-file))
  292. (publink
  293. (concat (file-name-as-directory blogurl)
  294. (file-name-nondirectory
  295. (file-name-sans-extension ifile))
  296. "." rssext)))
  297. (format
  298. "\n<title>%s</title>
  299. <atom:link href=\"%s\" rel=\"self\" type=\"application/rss+xml\" />
  300. <link>%s</link>
  301. <description><![CDATA[%s]]></description>
  302. <language>%s</language>
  303. <pubDate>%s</pubDate>
  304. <lastBuildDate>%s</lastBuildDate>
  305. <generator>%s</generator>
  306. <webMaster>%s (%s)</webMaster>
  307. <image>
  308. <url>%s</url>
  309. <title>%s</title>
  310. <link>%s</link>
  311. </image>
  312. "
  313. title publink blogurl description lang date date
  314. (concat (format "Emacs %d.%d"
  315. emacs-major-version
  316. emacs-minor-version)
  317. " Org-mode " (org-version))
  318. email author image title blogurl)))
  319. (defun org-rss-section (section contents info)
  320. "Transcode SECTION element into RSS format.
  321. CONTENTS is the section contents. INFO is a plist used as
  322. a communication channel."
  323. contents)
  324. (defun org-rss-timestamp (timestamp contents info)
  325. "Transcode a TIMESTAMP object from Org to RSS.
  326. CONTENTS is nil. INFO is a plist holding contextual
  327. information."
  328. (org-html-encode-plain-text
  329. (org-timestamp-translate timestamp)))
  330. (defun org-rss-plain-text (contents info)
  331. "Convert plain text into RSS encoded text."
  332. (let (output)
  333. (setq output (org-html-encode-plain-text contents)
  334. output (org-export-activate-smart-quotes
  335. output :html info))))
  336. ;;; Filters
  337. (defun org-rss-final-function (contents backend info)
  338. "Prettify the RSS output."
  339. (with-temp-buffer
  340. (xml-mode)
  341. (insert contents)
  342. (indent-region (point-min) (point-max))
  343. (buffer-substring-no-properties (point-min) (point-max))))
  344. ;;; Miscellaneous
  345. (defun org-rss-add-pubdate-property ()
  346. "Set the PUBDATE property for top-level headlines."
  347. (let (msg)
  348. (org-map-entries
  349. (lambda ()
  350. (let* ((entry (org-element-at-point))
  351. (level (org-element-property :level entry)))
  352. (when (= level 1)
  353. (unless (org-entry-get (point) "PUBDATE")
  354. (setq msg t)
  355. (org-set-property
  356. "PUBDATE" (format-time-string
  357. (cdr org-time-stamp-formats)))))))
  358. nil nil 'comment 'archive)
  359. (when msg
  360. (message "Property PUBDATE added to top-level entries in %s"
  361. (buffer-file-name))
  362. (sit-for 2))))
  363. (provide 'ox-rss)
  364. ;;; ox-rss.el ends here