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/>.
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:
48 ;; 'org-publish-project-alist
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.
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."
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
87 (defcustom org-rss-extension
"xml"
88 "File extension for the RSS 2.0 feed."
89 :group
'org-export-rss
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
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
110 (org-export-define-derived-backend 'rss
'html
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"
118 (if a
(org-rss-export-to-rss t s v
)
119 (org-open-file (org-rss-export-to-rss nil s v
)))))))
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
)))
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
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
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
160 (let ((file (buffer-file-name (buffer-base-buffer))))
161 (org-icalendar-create-uid file
'warn-user
)
162 (org-rss-add-pubdate-property))
164 (org-export-async-start
166 (with-current-buffer (get-buffer-create "*Org RSS Export*")
169 (goto-char (point-min))
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
)))))
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
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
196 When optional argument VISIBLE-ONLY is non-nil, don't export
197 contents of hidden elements.
199 Return output file's name."
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
)))
207 (org-export-async-start
208 (lambda (f) (org-export-add-to-stack f
'rss
))
210 (org-export-to-file 'rss
,outfile
,subtreep
,visible-only
)))
211 (org-export-to-file 'rss outfile subtreep visible-only
))))
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."
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
))
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
))
245 (let ((system-time-locale "C"))
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
))
255 (org-element-property :RSS_PERMALINK headline
))
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
264 (or (org-element-property :ID headline
)
265 (org-element-property :CUSTOM_ID headline
)
271 "<title>%s</title>\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"
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
)
284 (lambda (c) (format "<category><![CDATA[%s]]></category>" c
))
285 (org-element-property :tags headline
)
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."
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/\">"
308 (org-rss-build-channel-info info
) "\n"
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
)))
330 (concat (file-name-as-directory blogurl
)
331 (file-name-nondirectory
332 (file-name-sans-extension (buffer-file-name)))
336 <atom:link href=\"%s\" rel=\"self\" type=\"application/rss+xml\" />
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>
350 title publink blogurl description lang date date
351 (concat (format "Emacs %d.%d"
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."
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
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."
373 (setq output
(org-html-encode-plain-text contents
)
374 output
(org-export-activate-smart-quotes
375 output
:html info
))))
379 (defun org-rss-final-function (contents backend info
)
380 "Prettify the RSS output."
384 (indent-region (point-min) (point-max))
385 (buffer-substring-no-properties (point-min) (point-max))))
389 (defun org-rss-add-pubdate-property ()
390 "Set the PUBDATE property for top-level headlines."
394 (let* ((entry (org-element-at-point))
395 (level (org-element-property :level entry
)))
397 (unless (org-entry-get (point) "PUBDATE")
400 "PUBDATE" (format-time-string
401 (cdr org-time-stamp-formats
)))))))
402 nil nil
'comment
'archive
)
404 (message "Property PUBDATE added to top-level entries in %s"
410 ;;; ox-rss.el ends here