ORG-NEWS: Document inline source block export
[org-mode.git] / contrib / lisp / ox-freemind.el
blob39fb1cc2ce07a04d4d72d73052446f2cd3bd3a5b
1 ;;; ox-freemind.el --- Freemind Mindmap Back-End for Org Export Engine
3 ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is not 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 Freemind Mindmap back-end for Org generic
26 ;; exporter.
28 ;; To test it, run:
30 ;; M-x org-freemind-export-to-freemind
32 ;; in an Org mode buffer. See ox.el for more details on how this
33 ;; exporter works.
35 ;;; Code:
37 ;;; Dependencies
39 (require 'ox-html)
43 ;;; Define Back-End
45 (org-export-define-derived-backend 'freemind 'html
46 :export-block "FREEMIND"
47 :menu-entry
48 '(?f "Export to Freemind Mindmap"
49 ((?f "As Freemind Mindmap file" org-freemind-export-to-freemind)
50 (?o "As Freemind Mindmap file and open"
51 (lambda (a s v b)
52 (if a (org-freemind-export-to-freemind t s v b)
53 (org-open-file (org-freemind-export-to-freemind nil s v b)))))))
54 :translate-alist '((headline . org-freemind-headline)
55 (template . org-freemind-template)
56 (inner-template . org-freemind-inner-template)
57 (section . org-freemind-section)
58 (entity . org-freemind-entity))
59 :filters-alist '((:filter-options . org-freemind-options-function)
60 (:filter-final-output . org-freemind-final-function)))
64 ;;; User Configuration Variables
66 (defgroup org-export-freemind nil
67 "Options for exporting Org mode files to Freemind Mindmap."
68 :tag "Org Export Freemind Mindmap"
69 :group 'org-export)
71 (defcustom org-freemind-styles
72 '((default . "<node>\n</node>")
73 (0 . "<node COLOR=\"#000000\">\n<font NAME=\"SansSerif\" SIZE=\"20\"/>\n</node>")
74 (1 . "<node COLOR=\"#0033ff\">\n<edge STYLE=\"sharp_bezier\" WIDTH=\"8\"/>\n<font NAME=\"SansSerif\" SIZE=\"18\"/>\n</node>")
75 (2 . "<node COLOR=\"#00b439\">\n<edge STYLE=\"bezier\" WIDTH=\"thin\"/>\n<font NAME=\"SansSerif\" SIZE=\"16\"/>\n</node>")
76 (3 . "<node COLOR=\"#990000\" FOLDED=\"true\">\n<font NAME=\"SansSerif\" SIZE=\"14\"/>\n</node>")
77 (4 . "<node COLOR=\"#111111\">\n</node>"))
78 "List of Freemind node styles.
79 Each entry is of the form (STYLE-NAME . STYLE-SPEC). STYLE-NAME
80 can be one of an integer (signifying an outline level), a string
81 or the symbol `default'. STYLE-SPEC, a string, is a Freemind
82 node style."
83 :type '(alist :options (default 0 1 2 3)
84 :key-type (choice :tag "Style tag"
85 (integer :tag "Outline level")
86 (const :tag "Default value" default)
87 (string :tag "Node style"))
88 :value-type (string :tag "Style spec"))
89 :group 'org-export-freemind)
91 (defcustom org-freemind-style-map-function 'org-freemind-style-map--automatic
92 "Function to map an Org element to it's node style.
93 The mapping function takes two arguments an Org ELEMENT and INFO.
94 ELEMENT can be one of the following types - `org-data',
95 `headline' or `section'. INFO is a plist holding contextual
96 information during export. The function must return a STYLE-SPEC
97 to be applied to ELEMENT.
99 See `org-freemind-style-map--automatic' for a sample style
100 function. See `org-freemind-styles' for a list of named styles."
101 :type '(radio
102 (function-item org-freemind-style-map--automatic)
103 (function-item org-freemind-style-map--default)
104 function)
105 :group 'org-export-freemind)
107 (defcustom org-freemind-section-format 'note
108 "Specify how outline sections are to be formatted.
109 If `inline', append it to the contents of it's heading node. If
110 `note', attach it as a note to it's heading node. If `node',
111 attach it as a separate node to it's heading node.
113 Use `note', if the input Org file contains large sections. Use
114 `node', if the Org file contains mid-sized sections that need to
115 stand apart. Otherwise, use `inline'."
116 :type '(choice
117 (const :tag "Append to outline title" inline)
118 (const :tag "Attach as a note" note)
119 (const :tag "Create a separate node" node))
120 :group 'org-export-freemind)
122 ;;;; Debugging
124 (defcustom org-freemind-pretty-output nil
125 "Enable this to generate pretty Freemind Mindmap."
126 :type 'boolean
127 :group 'org-export-freemind)
130 ;;; Internal Functions
132 ;;;; XML Manipulation
134 (defun org-freemind--serialize (parsed-xml &optional contents)
135 "Convert PARSED-XML in to XML string.
136 PARSED-XML is a parse tree as returned by
137 `libxml-parse-xml-region'. CONTENTS is an optional string.
139 Ignore CONTENTS, if PARSED-XML is not a sole XML element.
140 Otherwise, append CONTENTS to the contents of top-level element
141 in PARSED-XML.
143 This is an inverse function of `libxml-parse-xml-region'.
145 For purposes of Freemind export, PARSED-XML is a node style
146 specification - \"<node ...>...</node>\" - as a parse tree."
147 (when contents
148 (assert (symbolp (car parsed-xml))))
149 (cond
150 ((null parsed-xml) "")
151 ((stringp parsed-xml) parsed-xml)
152 ((symbolp (car parsed-xml))
153 (let ((attributes (mapconcat
154 (lambda (av)
155 (format "%s=\"%s\"" (car av) (cdr av)))
156 (cadr parsed-xml) " ")))
157 (if (or (cddr parsed-xml) contents)
158 (format "\n<%s%s>%s\n</%s>"
159 (car parsed-xml)
160 (if (string= attributes "") "" (concat " " attributes))
161 (concat (org-freemind--serialize (cddr parsed-xml))
162 contents )
163 (car parsed-xml))
164 (format "\n<%s%s/>"
165 (car parsed-xml)
166 (if (string= attributes "") "" (concat " " attributes))))))
167 (t (mapconcat #'org-freemind--serialize parsed-xml ""))))
169 (defun org-freemind--parse-xml (xml-string)
170 "Return parse tree for XML-STRING using `libxml-parse-xml-region'.
171 For purposes of Freemind export, XML-STRING is a node style
172 specification - \"<node ...>...</node>\" - as a string."
173 (with-temp-buffer
174 (insert (or xml-string ""))
175 (libxml-parse-xml-region (point-min) (point-max))))
178 ;;;; Style mappers :: Default and Automatic layout
180 (defun org-freemind-style-map--automatic (element info)
181 "Return a node style corresponding to relative outline level of ELEMENT.
182 ELEMENT can be any of the following types - `org-data',
183 `headline' or `section'. See `org-freemind-styles' for style
184 mappings of different outline levels."
185 (let ((style-name
186 (case (org-element-type element)
187 (headline
188 (org-export-get-relative-level element info))
189 (section
190 (let ((parent (org-export-get-parent-headline element)))
191 (if (not parent) 1
192 (1+ (org-export-get-relative-level parent info)))))
193 (t 0))))
194 (or (assoc-default style-name org-freemind-styles)
195 (assoc-default 'default org-freemind-styles)
196 "<node></node>")))
198 (defun org-freemind-style-map--default (element info)
199 "Return the default style for all ELEMENTs.
200 ELEMENT can be any of the following types - `org-data',
201 `headline' or `section'. See `org-freemind-styles' for current
202 value of default style."
203 (or (assoc-default 'default org-freemind-styles)
204 "<node></node>"))
207 ;;;; Helpers :: Retrieve, apply Freemind styles
209 (defun org-freemind--get-node-style (element info)
210 "Return Freemind node style applicable for HEADLINE.
211 ELEMENT is an Org element of type `org-data', `headline' or
212 `section'. INFO is a plist holding contextual information."
213 (unless (fboundp org-freemind-style-map-function)
214 (setq org-freemind-style-map-function 'org-freemind-style-map--default))
215 (let ((style (funcall org-freemind-style-map-function element info)))
216 ;; Sanitize node style.
218 ;; Loop through the attributes of node element and purge those
219 ;; attributes that look suspicious. This is an extra bit of work
220 ;; that allows one to copy verbatim node styles from an existing
221 ;; Freemind Mindmap file without messing with the exported data.
222 (let* ((data (org-freemind--parse-xml style))
223 (attributes (cadr data))
224 (ignored-attrs '(POSITION FOLDED TEXT CREATED ID
225 MODIFIED)))
226 (let (attr)
227 (while (setq attr (pop ignored-attrs))
228 (setq attributes (assq-delete-all attr attributes))))
229 (when data (setcar (cdr data) attributes))
230 (org-freemind--serialize data))))
232 (defun org-freemind--build-stylized-node (style-1 style-2 &optional contents)
233 "Build a Freemind node with style STYLE-1 + STYLE-2 and add CONTENTS to it.
234 STYLE-1 and STYLE-2 are Freemind node styles as a string.
235 STYLE-1 is the base node style and STYLE-2 is the overriding
236 style that takes precedence over STYLE-1. CONTENTS is a string.
238 Return value is a Freemind node with following properties:
240 1. The attributes of \"<node ...> </node>\" element is the union
241 of corresponding attributes of STYLE-1 and STYLE-2. When
242 STYLE-1 and STYLE-2 specify values for the same attribute
243 name, choose the attribute value from STYLE-2.
245 2. The children of \"<node ...> </node>\" element is the union of
246 top-level children of STYLE-1 and STYLE-2 with CONTENTS
247 appended to it. When STYLE-1 and STYLE-2 share a child
248 element of same type, the value chosen is that from STYLE-2.
250 For example, merging with following parameters
252 STYLE-1 =>
253 <node COLOR=\"#00b439\" STYLE=\"Bubble\">
254 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
255 <font NAME=\"SansSerif\" SIZE=\"16\"/>
256 </node>
258 STYLE-2 =>
259 <node COLOR=\"#990000\" FOLDED=\"true\">
260 <font NAME=\"SansSerif\" SIZE=\"14\"/>
261 </node>
263 CONTENTS =>
264 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
266 will result in following node:
268 RETURN =>
269 <node STYLE=\"Bubble\" COLOR=\"#990000\" FOLDED=\"true\">
270 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
271 <font NAME=\"SansSerif\" SIZE=\"14\"/>
272 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
273 </node>."
274 (let* ((data1 (org-freemind--parse-xml (or style-1 "")))
275 (data2 (org-freemind--parse-xml (or style-2 "")))
276 (attr1 (cadr data1))
277 (attr2 (cadr data2))
278 (merged-attr attr2)
279 (children1 (cddr data1))
280 (children2 (cddr data2))
281 (merged-children children2))
282 (let (attr)
283 (while (setq attr (pop attr1))
284 (unless (assq (car attr) merged-attr)
285 (push attr merged-attr))))
286 (let (child)
287 (while (setq child (pop children1))
288 (when (or (stringp child) (not (assq (car child) merged-children)))
289 (push child merged-children))))
290 (let ((merged-data (nconc (list 'node merged-attr) merged-children)))
291 (org-freemind--serialize merged-data contents))))
294 ;;;; Helpers :: Node contents
296 (defun org-freemind--richcontent (type contents &optional css-style)
297 (let* ((type (case type
298 (note "NOTE")
299 (node "NODE")
300 (t "NODE")))
301 (contents (org-trim contents)))
302 (if (string= (org-trim contents) "") ""
303 (format "\n<richcontent TYPE=\"%s\">%s\n</richcontent>"
304 type
305 (format "\n<html>\n<head>%s\n</head>\n%s\n</html>"
306 (or css-style "")
307 (format "<body>\n%s\n</body>" contents))))))
309 (defun org-freemind--build-node-contents (element contents info)
310 (let* ((title (case (org-element-type element)
311 (headline
312 (org-element-property :title element))
313 (org-data
314 (plist-get info :title))
315 (t (error "Shouldn't come here."))))
316 (element-contents (org-element-contents element))
317 (section (assq 'section element-contents))
318 (section-contents
319 (let ((backend (org-export-create-backend
320 :parent (org-export-backend-name
321 (plist-get info :back-end))
322 :transcoders '((section . (lambda (e c i) c))))))
323 (org-export-data-with-backend section backend info)))
324 (itemized-contents-p (let ((first-child-headline
325 (org-element-map element-contents
326 'headline 'identity info t)))
327 (when first-child-headline
328 (org-export-low-level-p first-child-headline
329 info))))
330 (node-contents (concat section-contents
331 (when itemized-contents-p
332 contents))))
333 (concat (let ((title (org-export-data title info)))
334 (case org-freemind-section-format
335 (inline
336 (org-freemind--richcontent
337 'node (concat (format "\n<h2>%s</h2>" title)
338 node-contents) ))
339 (note
340 (concat (org-freemind--richcontent
341 'node (format "\n<p>%s\n</p>" title))
342 (org-freemind--richcontent
343 'note node-contents)))
344 (node
345 (concat
346 (org-freemind--richcontent
347 'node (format "\n<p>%s\n</p>" title))
348 (when section
349 (org-freemind--build-stylized-node
350 (org-freemind--get-node-style section info) nil
351 (org-freemind--richcontent 'node node-contents)))))))
352 (unless itemized-contents-p
353 contents))))
357 ;;; Template
359 (defun org-freemind-template (contents info)
360 "Return complete document string after Freemind Mindmap conversion.
361 CONTENTS is the transcoded contents string. RAW-DATA is the
362 original parsed data. INFO is a plist holding export options."
363 (format
364 "<map version=\"0.9.0\">\n%s\n</map>"
365 (org-freemind--build-stylized-node
366 (org-freemind--get-node-style nil info) nil
367 (let ((org-data (plist-get info :parse-tree)))
368 (org-freemind--build-node-contents org-data contents info)))))
370 (defun org-freemind-inner-template (contents info)
371 "Return body of document string after Freemind Mindmap conversion.
372 CONTENTS is the transcoded contents string. INFO is a plist
373 holding export options."
374 contents)
376 ;;;; Tags
378 (defun org-freemind--tags (tags)
379 (mapconcat (lambda (tag)
380 (format "\n<attribute NAME=\"%s\" VALUE=\"%s\"/>" tag ""))
381 tags "\n"))
385 ;;; Transcode Functions
387 ;;;; Entity
389 (defun org-freemind-entity (entity contents info)
390 "Transcode an ENTITY object from Org to Freemind Mindmap.
391 CONTENTS are the definition itself. INFO is a plist holding
392 contextual information."
393 (org-element-property :utf-8 entity))
395 ;;;; Headline
397 (defun org-freemind-headline (headline contents info)
398 "Transcode a HEADLINE element from Org to Freemind Mindmap.
399 CONTENTS holds the contents of the headline. INFO is a plist
400 holding contextual information."
401 ;; Empty contents?
402 (setq contents (or contents ""))
403 (let* ((numberedp (org-export-numbered-headline-p headline info))
404 (level (org-export-get-relative-level headline info))
405 (text (org-export-data (org-element-property :title headline) info))
406 (todo (and (plist-get info :with-todo-keywords)
407 (let ((todo (org-element-property :todo-keyword headline)))
408 (and todo (org-export-data todo info)))))
409 (todo-type (and todo (org-element-property :todo-type headline)))
410 (tags (and (plist-get info :with-tags)
411 (org-export-get-tags headline info)))
412 (priority (and (plist-get info :with-priority)
413 (org-element-property :priority headline)))
414 (section-number (and (not (org-export-low-level-p headline info))
415 (org-export-numbered-headline-p headline info)
416 (mapconcat 'number-to-string
417 (org-export-get-headline-number
418 headline info) ".")))
419 ;; Create the headline text.
420 (full-text (org-export-data (org-element-property :title headline)
421 info))
422 ;; Headline order (i.e, first digit of the section number)
423 (headline-order (car (org-export-get-headline-number headline info))))
424 (cond
425 ;; Case 1: This is a footnote section: ignore it.
426 ((org-element-property :footnote-section-p headline) nil)
427 ;; Case 2. This is a deep sub-tree, export it as a list item.
428 ;; Delegate the actual export to `html' backend.
429 ((org-export-low-level-p headline info)
430 (org-html-headline headline contents info))
431 ;; Case 3. Standard headline. Export it as a section.
433 (let* ((section-number (mapconcat 'number-to-string
434 (org-export-get-headline-number
435 headline info) "-"))
436 (ids (remove 'nil
437 (list (org-element-property :CUSTOM_ID headline)
438 (concat "sec-" section-number)
439 (org-element-property :ID headline))))
440 (preferred-id (car ids))
441 (extra-ids (cdr ids))
442 (left-p (zerop (% headline-order 2))))
443 (org-freemind--build-stylized-node
444 (org-freemind--get-node-style headline info)
445 (format "<node ID=\"%s\" POSITION=\"%s\" FOLDED=\"%s\">\n</node>"
446 preferred-id
447 (if left-p "left" "right")
448 (if (= level 1) "true" "false"))
449 (concat (org-freemind--build-node-contents headline contents info)
450 (org-freemind--tags tags))))))))
453 ;;;; Section
455 (defun org-freemind-section (section contents info)
456 "Transcode a SECTION element from Org to Freemind Mindmap.
457 CONTENTS holds the contents of the section. INFO is a plist
458 holding contextual information."
459 (let ((parent (org-export-get-parent-headline section)))
460 (when (and parent (org-export-low-level-p parent info))
461 contents)))
465 ;;; Filter Functions
467 (defun org-freemind-final-function (contents backend info)
468 "Return CONTENTS as pretty XML using `indent-region'."
469 (if (not org-freemind-pretty-output) contents
470 (with-temp-buffer
471 (nxml-mode)
472 (insert contents)
473 (indent-region (point-min) (point-max))
474 (buffer-substring-no-properties (point-min) (point-max)))))
476 (defun org-freemind-options-function (info backend)
477 "Install script in export options when appropriate.
478 EXP-PLIST is a plist containing export options. BACKEND is the
479 export back-end currently used."
480 ;; Freemind/Freeplane doesn't seem to like named html entities in
481 ;; richcontent. For now, turn off smart quote processing so that
482 ;; entities like "&rsquo;" & friends are avoided in the exported
483 ;; output.
484 (plist-put info :with-smart-quotes nil))
488 ;;; End-user functions
490 ;;;###autoload
491 (defun org-freemind-export-to-freemind
492 (&optional async subtreep visible-only body-only ext-plist)
493 "Export current buffer to a Freemind Mindmap file.
495 If narrowing is active in the current buffer, only export its
496 narrowed part.
498 If a region is active, export that region.
500 A non-nil optional argument ASYNC means the process should happen
501 asynchronously. The resulting file should be accessible through
502 the `org-export-stack' interface.
504 When optional argument SUBTREEP is non-nil, export the sub-tree
505 at point, extracting information from the headline properties
506 first.
508 When optional argument VISIBLE-ONLY is non-nil, don't export
509 contents of hidden elements.
511 When optional argument BODY-ONLY is non-nil, only write code
512 between \"<body>\" and \"</body>\" tags.
514 EXT-PLIST, when provided, is a property list with external
515 parameters overriding Org default settings, but still inferior to
516 file-local settings.
518 Return output file's name."
519 (interactive)
520 (let* ((extension (concat ".mm" ))
521 (file (org-export-output-file-name extension subtreep))
522 (org-export-coding-system 'utf-8))
523 (org-export-to-file 'freemind file
524 async subtreep visible-only body-only ext-plist)))
526 (provide 'ox-freemind)
528 ;;; ox-freemind.el ends here