ox-rss.el 14 KB

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