orgguide.texi: break lines after @noindent
[org-mode/org-tableheadings.git] / contrib / lisp / ox-rss.el
blobfddaa1df0b4d9ba90e85e7344fb5bb2e56c6ccd3
1 ;;; ox-rss.el --- RSS 2.0 Back-End for Org Export Engine
3 ;; Copyright (C) 2013, 2014 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 ;; 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 ;; :html-link-use-abs-url t
55 ;; :rss-extension "xml"
56 ;; :publishing-directory "/home/guerry/public_html/"
57 ;; :publishing-function (org-rss-publish-to-rss)
58 ;; :section-numbers nil
59 ;; :exclude ".*" ;; To exclude all files...
60 ;; :include ("index.org") ;; ... except index.org.
61 ;; :table-of-contents nil))
63 ;; ... then rsync /home/guerry/public_html/ with your server.
65 ;; By default, the permalink for a blog entry points to the headline.
66 ;; You can specify a different one by using the :RSS_PERMALINK:
67 ;; property within an entry.
69 ;;; Code:
71 (require 'ox-html)
72 (declare-function url-encode-url "url-util" (url))
74 ;;; Variables and options
76 (defgroup org-export-rss nil
77 "Options specific to RSS export back-end."
78 :tag "Org RSS"
79 :group 'org-export
80 :version "24.4"
81 :package-version '(Org . "8.0"))
83 (defcustom org-rss-image-url "http://orgmode.org/img/org-mode-unicorn-logo.png"
84 "The URL of the an image for the RSS feed."
85 :group 'org-export-rss
86 :type 'string)
88 (defcustom org-rss-extension "xml"
89 "File extension for the RSS 2.0 feed."
90 :group 'org-export-rss
91 :type 'string)
93 (defcustom org-rss-categories 'from-tags
94 "Where to extract items category information from.
95 The default is to extract categories from the tags of the
96 headlines. When set to another value, extract the category
97 from the :CATEGORY: property of the entry."
98 :group 'org-export-rss
99 :type '(choice
100 (const :tag "From tags" from-tags)
101 (const :tag "From the category property" from-category)))
103 (defcustom org-rss-use-entry-url-as-guid t
104 "Use the URL for the <guid> metatag?
105 When nil, Org will create ids using `org-icalendar-create-uid'."
106 :group 'org-export-rss
107 :type 'boolean)
109 ;;; Define backend
111 (org-export-define-derived-backend 'rss 'html
112 :menu-entry
113 '(?r "Export to RSS"
114 ((?R "As RSS buffer"
115 (lambda (a s v b) (org-rss-export-as-rss a s v)))
116 (?r "As RSS file" (lambda (a s v b) (org-rss-export-to-rss a s v)))
117 (?o "As RSS file and open"
118 (lambda (a s v b)
119 (if a (org-rss-export-to-rss t s v)
120 (org-open-file (org-rss-export-to-rss nil s v)))))))
121 :options-alist
122 '((:with-toc nil nil nil) ;; Never include HTML's toc
123 (:rss-extension "RSS_EXTENSION" nil org-rss-extension)
124 (:rss-image-url "RSS_IMAGE_URL" nil org-rss-image-url)
125 (:rss-categories nil nil org-rss-categories))
126 :filters-alist '((:filter-final-output . org-rss-final-function))
127 :translate-alist '((headline . org-rss-headline)
128 (comment . (lambda (&rest args) ""))
129 (comment-block . (lambda (&rest args) ""))
130 (timestamp . (lambda (&rest args) ""))
131 (plain-text . org-rss-plain-text)
132 (section . org-rss-section)
133 (template . org-rss-template)))
135 ;;; Export functions
137 ;;;###autoload
138 (defun org-rss-export-as-rss (&optional async subtreep visible-only)
139 "Export current buffer to a RSS buffer.
141 If narrowing is active in the current buffer, only export its
142 narrowed part.
144 If a region is active, export that region.
146 A non-nil optional argument ASYNC means the process should happen
147 asynchronously. The resulting buffer should be accessible
148 through the `org-export-stack' interface.
150 When optional argument SUBTREEP is non-nil, export the sub-tree
151 at point, extracting information from the headline properties
152 first.
154 When optional argument VISIBLE-ONLY is non-nil, don't export
155 contents of hidden elements.
157 Export is done in a buffer named \"*Org RSS Export*\", which will
158 be displayed when `org-export-show-temporary-export-buffer' is
159 non-nil."
160 (interactive)
161 (let ((file (buffer-file-name (buffer-base-buffer))))
162 (org-icalendar-create-uid file 'warn-user)
163 (org-rss-add-pubdate-property))
164 (org-export-to-buffer 'rss "*Org RSS Export*"
165 async subtreep visible-only nil nil (lambda () (text-mode))))
167 ;;;###autoload
168 (defun org-rss-export-to-rss (&optional async subtreep visible-only)
169 "Export current buffer to a RSS file.
171 If narrowing is active in the current buffer, only export its
172 narrowed part.
174 If a region is active, export that region.
176 A non-nil optional argument ASYNC means the process should happen
177 asynchronously. The resulting file should be accessible through
178 the `org-export-stack' interface.
180 When optional argument SUBTREEP is non-nil, export the sub-tree
181 at point, extracting information from the headline properties
182 first.
184 When optional argument VISIBLE-ONLY is non-nil, don't export
185 contents of hidden elements.
187 Return output file's name."
188 (interactive)
189 (let ((file (buffer-file-name (buffer-base-buffer))))
190 (org-icalendar-create-uid file 'warn-user)
191 (org-rss-add-pubdate-property))
192 (let ((outfile (org-export-output-file-name
193 (concat "." org-rss-extension) subtreep)))
194 (org-export-to-file 'rss outfile async subtreep visible-only)))
196 ;;;###autoload
197 (defun org-rss-publish-to-rss (plist filename pub-dir)
198 "Publish an org file to RSS.
200 FILENAME is the filename of the Org file to be published. PLIST
201 is the property list for the given project. PUB-DIR is the
202 publishing directory.
204 Return output file name."
205 (let ((bf (get-file-buffer filename)))
206 (if bf
207 (with-current-buffer bf
208 (org-icalendar-create-uid filename 'warn-user)
209 (org-rss-add-pubdate-property)
210 (write-file filename))
211 (find-file filename)
212 (org-icalendar-create-uid filename 'warn-user)
213 (org-rss-add-pubdate-property)
214 (write-file filename) (kill-buffer)))
215 (org-publish-org-to
216 'rss filename (concat "." org-rss-extension) plist pub-dir))
218 ;;; Main transcoding functions
220 (defun org-rss-headline (headline contents info)
221 "Transcode HEADLINE element into RSS format.
222 CONTENTS is the headline contents. INFO is a plist used as a
223 communication channel."
224 (unless (or (org-element-property :footnote-section-p headline)
225 ;; Only consider first-level headlines
226 (> (org-export-get-relative-level headline info) 1))
227 (let* ((author (and (plist-get info :with-author)
228 (let ((auth (plist-get info :author)))
229 (and auth (org-export-data auth info)))))
230 (htmlext (plist-get info :html-extension))
231 (hl-number (org-export-get-headline-number headline info))
232 (hl-home (file-name-as-directory (plist-get info :html-link-home)))
233 (hl-pdir (plist-get info :publishing-directory))
234 (hl-perm (org-element-property :RSS_PERMALINK headline))
235 (anchor
236 (org-export-solidify-link-text
237 (or (org-element-property :CUSTOM_ID headline)
238 (concat "sec-" (mapconcat 'number-to-string hl-number "-")))))
239 (category (org-rss-plain-text
240 (or (org-element-property :CATEGORY headline) "") info))
241 (pubdate0 (org-element-property :PUBDATE headline))
242 (pubdate (let ((system-time-locale "C"))
243 (if pubdate0
244 (format-time-string
245 "%a, %d %b %Y %H:%M:%S %z"
246 (org-time-string-to-time pubdate0)))))
247 (title (replace-regexp-in-string
248 org-bracket-link-regexp
249 (lambda (m) (or (match-string 3 m)
250 (match-string 1 m)))
251 (org-element-property :raw-value headline)))
252 (publink
253 (or (and hl-perm (concat (or hl-home hl-pdir) hl-perm))
254 (concat
255 (or hl-home hl-pdir)
256 (file-name-nondirectory
257 (file-name-sans-extension
258 (plist-get info :input-file))) "." htmlext "#" anchor)))
259 (guid (if org-rss-use-entry-url-as-guid
260 publink
261 (org-rss-plain-text
262 (or (org-element-property :ID headline)
263 (org-element-property :CUSTOM_ID headline)
264 publink)
265 info))))
266 (if (not pubdate0) "" ;; Skip entries with no PUBDATE prop
267 (format
268 (concat
269 "<item>\n"
270 "<title>%s</title>\n"
271 "<link>%s</link>\n"
272 "<author>%s</author>\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 author 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 %b %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 (ifile (plist-get info :input-file))
330 (publink
331 (concat (file-name-as-directory blogurl)
332 (file-name-nondirectory
333 (file-name-sans-extension ifile))
334 "." rssext)))
335 (format
336 "\n<title>%s</title>
337 <atom:link href=\"%s\" rel=\"self\" type=\"application/rss+xml\" />
338 <link>%s</link>
339 <description><![CDATA[%s]]></description>
340 <language>%s</language>
341 <pubDate>%s</pubDate>
342 <lastBuildDate>%s</lastBuildDate>
343 <generator>%s</generator>
344 <webMaster>%s (%s)</webMaster>
345 <image>
346 <url>%s</url>
347 <title>%s</title>
348 <link>%s</link>
349 </image>
351 title publink blogurl description lang date date
352 (concat (format "Emacs %d.%d"
353 emacs-major-version
354 emacs-minor-version)
355 " Org-mode " (org-version))
356 email author image title blogurl)))
358 (defun org-rss-section (section contents info)
359 "Transcode SECTION element into RSS format.
360 CONTENTS is the section contents. INFO is a plist used as
361 a communication channel."
362 contents)
364 (defun org-rss-timestamp (timestamp contents info)
365 "Transcode a TIMESTAMP object from Org to RSS.
366 CONTENTS is nil. INFO is a plist holding contextual
367 information."
368 (org-html-encode-plain-text
369 (org-timestamp-translate timestamp)))
371 (defun org-rss-plain-text (contents info)
372 "Convert plain text into RSS encoded text."
373 (let (output)
374 (setq output (org-html-encode-plain-text contents)
375 output (org-export-activate-smart-quotes
376 output :html info))))
378 ;;; Filters
380 (defun org-rss-final-function (contents backend info)
381 "Prettify the RSS output."
382 (with-temp-buffer
383 (xml-mode)
384 (insert contents)
385 (indent-region (point-min) (point-max))
386 (buffer-substring-no-properties (point-min) (point-max))))
388 ;;; Miscellaneous
390 (defun org-rss-add-pubdate-property ()
391 "Set the PUBDATE property for top-level headlines."
392 (let (msg)
393 (org-map-entries
394 (lambda ()
395 (let* ((entry (org-element-at-point))
396 (level (org-element-property :level entry)))
397 (when (= level 1)
398 (unless (org-entry-get (point) "PUBDATE")
399 (setq msg t)
400 (org-set-property
401 "PUBDATE" (format-time-string
402 (cdr org-time-stamp-formats)))))))
403 nil nil 'comment 'archive)
404 (when msg
405 (message "Property PUBDATE added to top-level entries in %s"
406 (buffer-file-name))
407 (sit-for 2))))
409 (provide 'ox-rss)
411 ;;; ox-rss.el ends here