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))
163 (org-export-to-buffer 'rss
"*Org RSS Export*"
164 async subtreep visible-only nil nil
(lambda () (text-mode))))
167 (defun org-rss-export-to-rss (&optional async subtreep visible-only
)
168 "Export current buffer to a RSS file.
170 If narrowing is active in the current buffer, only export its
173 If a region is active, export that region.
175 A non-nil optional argument ASYNC means the process should happen
176 asynchronously. The resulting file should be accessible through
177 the `org-export-stack' interface.
179 When optional argument SUBTREEP is non-nil, export the sub-tree
180 at point, extracting information from the headline properties
183 When optional argument VISIBLE-ONLY is non-nil, don't export
184 contents of hidden elements.
186 Return output file's name."
188 (let ((file (buffer-file-name (buffer-base-buffer))))
189 (org-icalendar-create-uid file
'warn-user
)
190 (org-rss-add-pubdate-property))
191 (let ((outfile (org-export-output-file-name
192 (concat "." org-rss-extension
) subtreep
)))
193 (org-export-to-file 'rss outfile async subtreep visible-only
)))
196 (defun org-rss-publish-to-rss (plist filename pub-dir
)
197 "Publish an org file to RSS.
199 FILENAME is the filename of the Org file to be published. PLIST
200 is the property list for the given project. PUB-DIR is the
201 publishing directory.
203 Return output file name."
204 (let ((bf (get-file-buffer filename
)))
206 (with-current-buffer bf
207 (org-rss-add-pubdate-property)
208 (write-file filename
))
210 (org-rss-add-pubdate-property)
211 (write-file filename
) (kill-buffer)))
213 'rss filename
(concat "." org-rss-extension
) plist pub-dir
))
215 ;;; Main transcoding functions
217 (defun org-rss-headline (headline contents info
)
218 "Transcode HEADLINE element into RSS format.
219 CONTENTS is the headline contents. INFO is a plist used as a
220 communication channel."
221 (unless (or (org-element-property :footnote-section-p headline
)
222 ;; Only consider first-level headlines
223 (> (org-export-get-relative-level headline info
) 1))
224 (let* ((htmlext (plist-get info
:html-extension
))
225 (hl-number (org-export-get-headline-number headline info
))
226 (hl-home (file-name-as-directory (plist-get info
:html-link-home
)))
227 (hl-pdir (plist-get info
:publishing-directory
))
228 (hl-perm (org-element-property :RSS_PERMALINK headline
))
230 (org-export-solidify-link-text
231 (or (org-element-property :CUSTOM_ID headline
)
232 (concat "sec-" (mapconcat 'number-to-string hl-number
"-")))))
233 (category (org-rss-plain-text
234 (or (org-element-property :CATEGORY headline
) "") info
))
236 (let ((system-time-locale "C"))
238 "%a, %d %h %Y %H:%M:%S %z"
239 (org-time-string-to-time
240 (or (org-element-property :PUBDATE headline
)
241 (error "Missing PUBDATE property"))))))
242 (title (org-element-property :raw-value headline
))
244 (or (and hl-perm
(concat (or hl-home hl-pdir
) hl-perm
))
247 (file-name-nondirectory
248 (file-name-sans-extension
249 (plist-get info
:input-file
))) "." htmlext
"#" anchor
)))
250 (guid (if org-rss-use-entry-url-as-guid
253 (or (org-element-property :ID headline
)
254 (org-element-property :CUSTOM_ID headline
)
260 "<title>%s</title>\n"
262 "<guid isPermaLink=\"false\">%s</guid>\n"
263 "<pubDate>%s</pubDate>\n"
264 (org-rss-build-categories headline info
) "\n"
265 "<description><![CDATA[%s]]></description>\n"
267 title publink guid pubdate contents
))))
269 (defun org-rss-build-categories (headline info
)
270 "Build categories for the RSS item."
271 (if (eq (plist-get info
:rss-categories
) 'from-tags
)
273 (lambda (c) (format "<category><![CDATA[%s]]></category>" c
))
274 (org-element-property :tags headline
)
276 (let ((c (org-element-property :CATEGORY headline
)))
277 (format "<category><![CDATA[%s]]></category>" c
))))
279 (defun org-rss-template (contents info
)
280 "Return complete document string after RSS conversion.
281 CONTENTS is the transcoded contents string. INFO is a plist used
282 as a communication channel."
284 (format "<?xml version=\"1.0\" encoding=\"%s\"?>"
285 (symbol-name org-html-coding-system
))
286 "\n<rss version=\"2.0\"
287 xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"
288 xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"
289 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
290 xmlns:atom=\"http://www.w3.org/2005/Atom\"
291 xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"
292 xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"
293 xmlns:georss=\"http://www.georss.org/georss\"
294 xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"
295 xmlns:media=\"http://search.yahoo.com/mrss/\">"
297 (org-rss-build-channel-info info
) "\n"
302 (defun org-rss-build-channel-info (info)
303 "Build the RSS channel information."
304 (let* ((system-time-locale "C")
305 (title (plist-get info
:title
))
306 (email (org-export-data (plist-get info
:email
) info
))
307 (author (and (plist-get info
:with-author
)
308 (let ((auth (plist-get info
:author
)))
309 (and auth
(org-export-data auth info
)))))
310 (date (format-time-string "%a, %d %h %Y %H:%M:%S %z")) ;; RFC 882
311 (description (org-export-data (plist-get info
:description
) info
))
312 (lang (plist-get info
:language
))
313 (keywords (plist-get info
:keywords
))
314 (rssext (plist-get info
:rss-extension
))
315 (blogurl (or (plist-get info
:html-link-home
)
316 (plist-get info
:publishing-directory
)))
317 (image (url-encode-url (plist-get info
:rss-image-url
)))
318 (ifile (plist-get info
:input-file
))
320 (concat (file-name-as-directory blogurl
)
321 (file-name-nondirectory
322 (file-name-sans-extension ifile
))
326 <atom:link href=\"%s\" rel=\"self\" type=\"application/rss+xml\" />
328 <description><![CDATA[%s]]></description>
329 <language>%s</language>
330 <pubDate>%s</pubDate>
331 <lastBuildDate>%s</lastBuildDate>
332 <generator>%s</generator>
333 <webMaster>%s (%s)</webMaster>
340 title publink blogurl description lang date date
341 (concat (format "Emacs %d.%d"
344 " Org-mode " (org-version))
345 email author image title blogurl
)))
347 (defun org-rss-section (section contents info
)
348 "Transcode SECTION element into RSS format.
349 CONTENTS is the section contents. INFO is a plist used as
350 a communication channel."
353 (defun org-rss-timestamp (timestamp contents info
)
354 "Transcode a TIMESTAMP object from Org to RSS.
355 CONTENTS is nil. INFO is a plist holding contextual
357 (org-html-encode-plain-text
358 (org-timestamp-translate timestamp
)))
360 (defun org-rss-plain-text (contents info
)
361 "Convert plain text into RSS encoded text."
363 (setq output
(org-html-encode-plain-text contents
)
364 output
(org-export-activate-smart-quotes
365 output
:html info
))))
369 (defun org-rss-final-function (contents backend info
)
370 "Prettify the RSS output."
374 (indent-region (point-min) (point-max))
375 (buffer-substring-no-properties (point-min) (point-max))))
379 (defun org-rss-add-pubdate-property ()
380 "Set the PUBDATE property for top-level headlines."
384 (let* ((entry (org-element-at-point))
385 (level (org-element-property :level entry
)))
387 (unless (org-entry-get (point) "PUBDATE")
390 "PUBDATE" (format-time-string
391 (cdr org-time-stamp-formats
)))))))
392 nil nil
'comment
'archive
)
394 (message "Property PUBDATE added to top-level entries in %s"
400 ;;; ox-rss.el ends here