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