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