ox-rss.el 13 KB

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