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