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