ox-rss.el (org-rss-headline): Various fixes.
[org-mode.git] / contrib / lisp / ox-rss.el
blobad0267306ec1cc1d574cb1a4e7a77edd9cefa578
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 ;; :html-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 "As RSS buffer"
108 (lambda (a s v b) (org-rss-export-as-rss a s v)))
109 (?r "As RSS file" (lambda (a s v b) (org-rss-export-to-rss a s v)))
110 (?o "As RSS 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
237 (let ((system-time-locale "C"))
238 (format-time-string
239 "%a, %d %h %Y %H:%M:%S %z"
240 (org-time-string-to-time
241 (or (org-element-property :PUBDATE headline)
242 (error "Missing PUBDATE property"))))))
243 (title (org-element-property :raw-value headline))
244 (publink
245 (concat
246 (file-name-as-directory
247 (or (plist-get info :html-link-home)
248 (plist-get info :publishing-directory)))
249 (file-name-nondirectory
250 (file-name-sans-extension
251 (buffer-file-name))) "." htmlext "#" anchor))
252 (guid (if org-rss-use-entry-url-as-guid
253 publink
254 (org-rss-plain-text
255 (or (org-element-property :ID headline)
256 (org-element-property :CUSTOM_ID headline)
257 publink)
258 info))))
259 (format
260 (concat
261 "<item>\n"
262 "<title>%s</title>\n"
263 "<link>%s</link>\n"
264 "<guid isPermaLink=\"false\">%s</guid>\n"
265 "<pubDate>%s</pubDate>\n"
266 (org-rss-build-categories headline info) "\n"
267 "<description><![CDATA[%s]]></description>\n"
268 "</item>\n")
269 title publink guid pubdate contents))))
271 (defun org-rss-build-categories (headline info)
272 "Build categories for the RSS item."
273 (if (eq (plist-get info :rss-categories) 'from-tags)
274 (mapconcat
275 (lambda (c) (format "<category><![CDATA[%s]]></category>" c))
276 (org-element-property :tags headline)
277 "\n")
278 (let ((c (org-element-property :CATEGORY headline)))
279 (format "<category><![CDATA[%s]]></category>" c))))
281 (defun org-rss-template (contents info)
282 "Return complete document string after RSS conversion.
283 CONTENTS is the transcoded contents string. INFO is a plist used
284 as a communication channel."
285 (concat
286 (format "<?xml version=\"1.0\" encoding=\"%s\"?>"
287 (symbol-name org-html-coding-system))
288 "\n<rss version=\"2.0\"
289 xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"
290 xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"
291 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
292 xmlns:atom=\"http://www.w3.org/2005/Atom\"
293 xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"
294 xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"
295 xmlns:georss=\"http://www.georss.org/georss\"
296 xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"
297 xmlns:media=\"http://search.yahoo.com/mrss/\">"
298 "<channel>"
299 (org-rss-build-channel-info info) "\n"
300 contents
301 "</channel>\n"
302 "</rss>"))
304 (defun org-rss-build-channel-info (info)
305 "Build the RSS channel information."
306 (let* ((system-time-locale "C")
307 (title (org-export-data (plist-get info :title) info))
308 (email (org-export-data (plist-get info :email) info))
309 (author (and (plist-get info :with-author)
310 (let ((auth (plist-get info :author)))
311 (and auth (org-export-data auth info)))))
312 (date (format-time-string "%a, %d %h %Y %H:%M:%S %z")) ;; RFC 882
313 (description (org-export-data (plist-get info :description) info))
314 (lang (plist-get info :language))
315 (keywords (plist-get info :keywords))
316 (rssext (plist-get info :rss-extension))
317 (blogurl (or (plist-get info :html-link-home)
318 (plist-get info :publishing-directory)))
319 (image (url-encode-url (plist-get info :rss-image-url)))
320 (publink
321 (concat (file-name-as-directory blogurl)
322 (file-name-nondirectory
323 (file-name-sans-extension (buffer-file-name)))
324 "." rssext)))
325 (format
326 "\n<title>%s</title>
327 <atom:link href=\"%s\" rel=\"self\" type=\"application/rss+xml\" />
328 <link>%s</link>
329 <description><![CDATA[%s]]></description>
330 <language>%s</language>
331 <pubDate>%s</pubDate>
332 <lastBuildDate>%s</lastBuildDate>
333 <generator>%s</generator>
334 <webMaster>%s (%s)</webMaster>
335 <image>
336 <url>%s</url>
337 <title>%s</title>
338 <link>%s</link>
339 </image>
341 title publink blogurl description lang date date
342 (concat (format "Emacs %d.%d"
343 emacs-major-version
344 emacs-minor-version)
345 " Org-mode " (org-version))
346 email author image title blogurl)))
348 (defun org-rss-section (section contents info)
349 "Transcode SECTION element into RSS format.
350 CONTENTS is the section contents. INFO is a plist used as
351 a communication channel."
352 contents)
354 (defun org-rss-timestamp (timestamp contents info)
355 "Transcode a TIMESTAMP object from Org to RSS.
356 CONTENTS is nil. INFO is a plist holding contextual
357 information."
358 (org-html-encode-plain-text
359 (org-timestamp-translate timestamp)))
361 (defun org-rss-plain-text (contents info)
362 "Convert plain text into RSS encoded text."
363 (let (output)
364 (setq output (org-html-encode-plain-text contents)
365 output (org-export-activate-smart-quotes
366 output :html info))))
368 ;;; Filters
370 (defun org-rss-final-function (contents backend info)
371 "Prettify the RSS output."
372 (with-temp-buffer
373 (xml-mode)
374 (insert contents)
375 (indent-region (point-min) (point-max))
376 (buffer-substring-no-properties (point-min) (point-max))))
378 ;;; Miscellaneous
380 (defun org-rss-add-pubdate-property ()
381 "Set the PUBDATE property for top-level headlines."
382 (let (msg)
383 (org-map-entries
384 (lambda ()
385 (let* ((entry (org-element-at-point))
386 (level (org-element-property :level entry)))
387 (when (= level 1)
388 (unless (org-entry-get (point) "PUBDATE")
389 (setq msg t)
390 (org-set-property
391 "PUBDATE" (format-time-string
392 (cdr org-time-stamp-formats)))))))
393 nil nil 'comment 'archive)
394 (when msg
395 (message "Property PUBDATE added to top-level entries in %s"
396 (buffer-file-name))
397 (sit-for 2))))
399 (provide 'ox-rss)
401 ;;; ox-rss.el ends here