ox-rss.el 13 KB

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