Add Freemind Mindmap Back-End for Org Export Engine
[org-mode.git] / contrib / lisp / ox-freemind.el
blobd15b2d54428833eea9552dffb6c2352e8f6d048b
1 ;;; ox-freemind.el --- Freemind Mindmap Back-End for Org Export Engine
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a Freemind Mindmap back-end for Org generic
24 ;; exporter.
26 ;; To test it, run:
28 ;; M-x org-freemind-export-to-freemind
30 ;; in an Org mode buffer. See ox.el for more details on how this
31 ;; exporter works.
33 ;;; Code:
35 ;;; Dependencies
37 (require 'ox-html)
41 ;;; Define Back-End
43 (org-export-define-derived-backend freemind html
44 :export-block "FREEMIND"
45 :menu-entry
46 (?f "Export to Freemind Mindmap"
47 ((?f "As Freemind Mindmap file" org-freemind-export-to-freemind)
48 (?o "As Freemind Mindmap file and open"
49 (lambda (a s v b)
50 (if a (org-freemind-export-to-freemind t s v b)
51 (org-open-file (org-freemind-export-to-freemind nil s v b)))))))
52 :translate-alist ((headline . org-freemind-headline)
53 (template . org-freemind-template)
54 (inner-template . org-freemind-inner-template)
55 (section . org-freemind-section)
56 (entity . org-freemind-entity))
57 :filters-alist ((:filter-options . org-freemind-options-function)
58 (:filter-final-output . org-freemind-final-function)))
62 ;;; User Configuration Variables
64 (defgroup org-export-freemind nil
65 "Options for exporting Org mode files to Freemind Mindmap."
66 :tag "Org Export Freemind Mindmap"
67 :group 'org-export
68 :version "24.4")
70 (defcustom org-freemind-styles
71 '((default . "<node>\n</node>")
72 (0 . "<node COLOR=\"#000000\">\n<font NAME=\"SansSerif\" SIZE=\"20\"/>\n</node>")
73 (1 . "<node COLOR=\"#0033ff\">\n<edge STYLE=\"sharp_bezier\" WIDTH=\"8\"/>\n<font NAME=\"SansSerif\" SIZE=\"18\"/>\n</node>")
74 (2 . "<node COLOR=\"#00b439\">\n<edge STYLE=\"bezier\" WIDTH=\"thin\"/>\n<font NAME=\"SansSerif\" SIZE=\"16\"/>\n</node>")
75 (3 . "<node COLOR=\"#990000\" FOLDED=\"true\">\n<font NAME=\"SansSerif\" SIZE=\"14\"/>\n</node>")
76 (4 . "<node COLOR=\"#111111\">\n</node>"))
77 "List of Freemind node styles.
78 Each entry is of the form (STYLE-NAME . STYLE-SPEC). STYLE-NAME
79 can be one of an integer (signifying an outline level), a string
80 or the symbol `default'. STYLE-SPEC, a string, is a Freemind
81 node style."
82 :type '(alist :options (default 0 1 2 3)
83 :key-type (choice :tag "Style tag"
84 (integer :tag "Outline level")
85 (const :tag "Default value" default)
86 (string :tag "Node style"))
87 :value-type (string :tag "Style spec"))
88 :group 'org-export-freemind
89 :version "24.4")
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
106 :version "24.4")
108 (defcustom org-freemind-section-format 'note
109 "Specify how outline sections are to be formatted.
110 If `inline', append it to the contents of it's heading node. If
111 `note', attach it as a note to it's heading node. If `node',
112 attach it as a separate node to it's heading node.
114 Use `note', if the input Org file contains large sections. Use
115 `node', if the Org file contains mid-sized sections that need to
116 stand apart. Otherwise, use `inline'."
117 :type '(choice
118 (const :tag "Append to outline title" inline)
119 (const :tag "Attach as a note" note)
120 (const :tag "Create a separate node" node))
121 :group 'org-export-freemind
122 :version "24.4")
124 ;;;; Debugging
126 (defcustom org-freemind-pretty-output nil
127 "Enable this to generate pretty Freemind Mindmap."
128 :type 'boolean
129 :group 'org-export-freemind
130 :version "24.4")
133 ;;; Internal Functions
135 ;;;; XML Manipulation
137 (defun org-freemind--serialize (parsed-xml &optional contents)
138 "Convert PARSED-XML in to XML string.
139 PARSED-XML is a parse tree as returned by
140 `libxml-parse-xml-region'. CONTENTS is an optional string.
142 Ignore CONTENTS, if PARSED-XML is not a sole XML element.
143 Otherwise, append CONTENTS to the contents of top-level element
144 in PARSED-XML.
146 This is an inverse function of `libxml-parse-xml-region'.
148 For purposes of Freemind export, PARSED-XML is a node style
149 specification - \"<node ...>...</node>\" - as a parse tree."
150 (when contents
151 (assert (symbolp (car parsed-xml))))
152 (cond
153 ((null parsed-xml) "")
154 ((stringp parsed-xml) parsed-xml)
155 ((symbolp (car parsed-xml))
156 (let ((attributes (mapconcat
157 (lambda (av)
158 (format "%s=\"%s\"" (car av) (cdr av)))
159 (cadr parsed-xml) " ")))
160 (if (or (cddr parsed-xml) contents)
161 (format "\n<%s%s>%s\n</%s>"
162 (car parsed-xml)
163 (if (string= attributes "") "" (concat " " attributes))
164 (concat (org-freemind--serialize (cddr parsed-xml))
165 contents )
166 (car parsed-xml))
167 (format "\n<%s%s/>"
168 (car parsed-xml)
169 (if (string= attributes "") "" (concat " " attributes))))))
170 (t (mapconcat #'org-freemind--serialize parsed-xml ""))))
172 (defun org-freemind--parse-xml (xml-string)
173 "Return parse tree for XML-STRING using `libxml-parse-xml-region'.
174 For purposes of Freemind export, XML-STRING is a node style
175 specification - \"<node ...>...</node>\" - as a string."
176 (with-temp-buffer
177 (insert (or xml-string ""))
178 (libxml-parse-xml-region (point-min) (point-max))))
181 ;;;; Style mappers :: Default and Automatic layout
183 (defun org-freemind-style-map--automatic (element info)
184 "Return a node style corresponding to relative outline level of ELEMENT.
185 ELEMENT can be any of the following types - `org-data',
186 `headline' or `section'. See `org-freemind-styles' for style
187 mappings of different outline levels."
188 (let ((style-name
189 (case (org-element-type element)
190 (headline
191 (org-export-get-relative-level element info))
192 (section
193 (let ((parent (org-export-get-parent-headline element)))
194 (if (not parent) 1
195 (1+ (org-export-get-relative-level parent info)))))
196 (t 0))))
197 (or (assoc-default style-name org-freemind-styles)
198 (assoc-default 'default org-freemind-styles)
199 "<node></node>")))
201 (defun org-freemind-style-map--default (element info)
202 "Return the default style for all ELEMENTs.
203 ELEMENT can be any of the following types - `org-data',
204 `headline' or `section'. See `org-freemind-styles' for current
205 value of default style."
206 (or (assoc-default 'default org-freemind-styles)
207 "<node></node>"))
210 ;;;; Helpers :: Retrieve, apply Freemind styles
212 (defun org-freemind--get-node-style (element info)
213 "Return Freemind node style applicable for HEADLINE.
214 ELEMENT is an Org element of type `org-data', `headline' or
215 `section'. INFO is a plist holding contextual information."
216 (unless (fboundp org-freemind-style-map-function)
217 (setq org-freemind-style-map-function 'org-freemind-style-map--default))
218 (let ((style (funcall org-freemind-style-map-function element info)))
219 ;; Sanitize node style.
221 ;; Loop through the attributes of node element and purge those
222 ;; attributes that look suspicious. This is an extra bit of work
223 ;; that allows one to copy verbatim node styles from an existing
224 ;; Freemind Mindmap file without messing with the exported data.
225 (let* ((data (org-freemind--parse-xml style))
226 (attributes (cadr data))
227 (ignored-attrs '(POSITION FOLDED TEXT CREATED ID
228 MODIFIED)))
229 (let (attr)
230 (while (setq attr (pop ignored-attrs))
231 (setq attributes (assq-delete-all attr attributes))))
232 (when data (setcar (cdr data) attributes))
233 (org-freemind--serialize data))))
235 (defun org-freemind--build-stylized-node (style-1 style-2 &optional contents)
236 "Build a Freemind node with style STYLE-1 + STYLE-2 and add CONTENTS to it.
237 STYLE-1 and STYLE-2 are Freemind node styles as a string.
238 STYLE-1 is the base node style and STYLE-2 is the overriding
239 style that takes precedence over STYLE-1. CONTENTS is a string.
241 Return value is a Freemind node with following properties:
243 1. The attributes of \"<node ...> </node>\" element is the union
244 of corresponding attributes of STYLE-1 and STYLE-2. When
245 STYLE-1 and STYLE-2 specify values for the same attribute
246 name, choose the attribute value from STYLE-2.
248 2. The children of \"<node ...> </node>\" element is the union of
249 top-level children of STYLE-1 and STYLE-2 with CONTENTS
250 appended to it. When STYLE-1 and STYLE-2 share a child
251 element of same type, the value chosen is that from STYLE-2.
253 For example, merging with following parameters
255 STYLE-1 =>
256 <node COLOR=\"#00b439\" STYLE=\"Bubble\">
257 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
258 <font NAME=\"SansSerif\" SIZE=\"16\"/>
259 </node>
261 STYLE-2 =>
262 <node COLOR=\"#990000\" FOLDED=\"true\">
263 <font NAME=\"SansSerif\" SIZE=\"14\"/>
264 </node>
266 CONTENTS =>
267 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
269 will result in following node:
271 RETURN =>
272 <node STYLE=\"Bubble\" COLOR=\"#990000\" FOLDED=\"true\">
273 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
274 <font NAME=\"SansSerif\" SIZE=\"14\"/>
275 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
276 </node>."
277 (let* ((data1 (org-freemind--parse-xml (or style-1 "")))
278 (data2 (org-freemind--parse-xml (or style-2 "")))
279 (attr1 (cadr data1))
280 (attr2 (cadr data2))
281 (merged-attr attr2)
282 (children1 (cddr data1))
283 (children2 (cddr data2))
284 (merged-children children2))
285 (let (attr)
286 (while (setq attr (pop attr1))
287 (unless (assq (car attr) merged-attr)
288 (push attr merged-attr))))
289 (let (child)
290 (while (setq child (pop children1))
291 (when (or (stringp child) (not (assq (car child) merged-children)))
292 (push child merged-children))))
293 (let ((merged-data (nconc (list 'node merged-attr) merged-children)))
294 (org-freemind--serialize merged-data contents))))
297 ;;;; Helpers :: Node contents
299 (defun org-freemind--richcontent (type contents &optional css-style)
300 (let* ((type (case type
301 (note "NOTE")
302 (node "NODE")
303 (t "NODE")))
304 (contents (org-trim contents)))
305 (if (string= (org-trim contents) "") ""
306 (format "\n<richcontent TYPE=\"%s\">%s\n</richcontent>"
307 type
308 (format "\n<html>\n<head>%s\n</head>\n%s\n</html>"
309 (or css-style "")
310 (format "<body>\n%s\n</body>" contents))))))
312 (defun org-freemind--build-node-contents (element contents info)
313 (let* ((title (case (org-element-type element)
314 (headline
315 (org-element-property :title element))
316 (org-data
317 (plist-get info :title))
318 (t (error "Shouldn't come here."))))
319 (element-contents (org-element-contents element))
320 (section (assoc 'section element-contents))
321 (section-contents
322 (let* ((translations
323 (nconc (list (cons 'section
324 (lambda (section contents info)
325 contents)))
326 (plist-get info :translate-alist))))
327 (org-export-data-with-translations section translations info)))
328 (itemized-contents-p (let ((first-child-headline
329 (org-element-map element-contents
330 'headline 'identity info t)))
331 (when first-child-headline
332 (org-export-low-level-p first-child-headline
333 info))))
334 (node-contents (concat section-contents
335 (when itemized-contents-p
336 contents))))
337 (concat (let ((title (org-export-data title info)))
338 (case org-freemind-section-format
339 (inline
340 (org-freemind--richcontent
341 'node (concat (format "\n<h2>%s</h2>" title)
342 node-contents) ))
343 (note
344 (concat (org-freemind--richcontent
345 'node (format "\n<p>%s\n</p>" title))
346 (org-freemind--richcontent
347 'note node-contents)))
348 (node
349 (concat
350 (org-freemind--richcontent
351 'node (format "\n<p>%s\n</p>" title))
352 (when section
353 (org-freemind--build-stylized-node
354 (org-freemind--get-node-style section info) nil
355 (org-freemind--richcontent 'node node-contents)))))))
356 (unless itemized-contents-p
357 contents))))
361 ;;; Template
363 (defun org-freemind-template (contents info)
364 "Return complete document string after Freemind Mindmap conversion.
365 CONTENTS is the transcoded contents string. RAW-DATA is the
366 original parsed data. INFO is a plist holding export options."
367 (format
368 "<map version=\"0.9.0\">\n%s\n</map>"
369 (org-freemind--build-stylized-node
370 (org-freemind--get-node-style nil info) nil
371 (let ((org-data (plist-get info :parse-tree)))
372 (org-freemind--build-node-contents org-data contents info)))))
374 (defun org-freemind-inner-template (contents info)
375 "Return body of document string after Freemind Mindmap conversion.
376 CONTENTS is the transcoded contents string. INFO is a plist
377 holding export options."
378 contents)
380 ;;;; Tags
382 (defun org-freemind--tags (tags)
383 (mapconcat (lambda (tag)
384 (format "\n<attribute NAME=\"%s\" VALUE=\"%s\"/>" tag ""))
385 tags "\n"))
389 ;;; Transcode Functions
391 ;;;; Entity
393 (defun org-freemind-entity (entity contents info)
394 "Transcode an ENTITY object from Org to Freemind Mindmap.
395 CONTENTS are the definition itself. INFO is a plist holding
396 contextual information."
397 (org-element-property :utf-8 entity))
399 ;;;; Headline
401 (defun org-freemind-headline (headline contents info)
402 "Transcode a HEADLINE element from Org to Freemind Mindmap.
403 CONTENTS holds the contents of the headline. INFO is a plist
404 holding contextual information."
405 ;; Empty contents?
406 (setq contents (or contents ""))
407 (let* ((numberedp (org-export-numbered-headline-p headline info))
408 (level (org-export-get-relative-level headline info))
409 (text (org-export-data (org-element-property :title headline) info))
410 (todo (and (plist-get info :with-todo-keywords)
411 (let ((todo (org-element-property :todo-keyword headline)))
412 (and todo (org-export-data todo info)))))
413 (todo-type (and todo (org-element-property :todo-type headline)))
414 (tags (and (plist-get info :with-tags)
415 (org-export-get-tags headline info)))
416 (priority (and (plist-get info :with-priority)
417 (org-element-property :priority headline)))
418 (section-number (and (not (org-export-low-level-p headline info))
419 (org-export-numbered-headline-p headline info)
420 (mapconcat 'number-to-string
421 (org-export-get-headline-number
422 headline info) ".")))
423 ;; Create the headline text.
424 (full-text (org-export-data (org-element-property :title headline)
425 info))
426 ;; Headline order (i.e, first digit of the section number)
427 (headline-order (car (org-export-get-headline-number headline info))))
428 (cond
429 ;; Case 1: This is a footnote section: ignore it.
430 ((org-element-property :footnote-section-p headline) nil)
431 ;; Case 2. This is a deep sub-tree, export it as a list item.
432 ;; Delegate the actual export to `html' backend.
433 ((org-export-low-level-p headline info)
434 (org-html-headline headline contents info))
435 ;; Case 3. Standard headline. Export it as a section.
437 (let* ((section-number (mapconcat 'number-to-string
438 (org-export-get-headline-number
439 headline info) "-"))
440 (ids (remove 'nil
441 (list (org-element-property :CUSTOM_ID headline)
442 (concat "sec-" section-number)
443 (org-element-property :ID headline))))
444 (preferred-id (car ids))
445 (extra-ids (cdr ids))
446 (left-p (zerop (% headline-order 2))))
447 (org-freemind--build-stylized-node
448 (org-freemind--get-node-style headline info)
449 (format "<node ID=\"%s\" POSITION=\"%s\" FOLDED=\"%s\">\n</node>"
450 preferred-id
451 (if left-p "left" "right")
452 (if (= level 1) "true" "false"))
453 (concat (org-freemind--build-node-contents headline contents info)
454 (org-freemind--tags tags))))))))
457 ;;;; Section
459 (defun org-freemind-section (section contents info)
460 "Transcode a SECTION element from Org to Freemind Mindmap.
461 CONTENTS holds the contents of the section. INFO is a plist
462 holding contextual information."
463 (let ((parent (org-export-get-parent-headline section)))
464 (when (and parent (org-export-low-level-p parent info))
465 contents)))
469 ;;; Filter Functions
471 (defun org-freemind-final-function (contents backend info)
472 "Return CONTENTS as pretty XML using `indent-region'."
473 (if (not org-freemind-pretty-output) contents
474 (with-temp-buffer
475 (nxml-mode)
476 (insert contents)
477 (indent-region (point-min) (point-max))
478 (buffer-substring-no-properties (point-min) (point-max)))))
480 (defun org-freemind-options-function (info backend)
481 "Install script in export options when appropriate.
482 EXP-PLIST is a plist containing export options. BACKEND is the
483 export back-end currently used."
484 ;; Freemind/Freeplane doesn't seem to like named html entities in
485 ;; richcontent. For now, turn off smart quote processing so that
486 ;; entities like "&rsquo;" & friends are avoided in the exported
487 ;; output.
488 (plist-put info :with-smart-quotes nil))
492 ;;; End-user functions
494 ;;;###autoload
495 (defun org-freemind-export-to-freemind
496 (&optional async subtreep visible-only body-only ext-plist)
497 "Export current buffer to a Freemind Mindmap file.
499 If narrowing is active in the current buffer, only export its
500 narrowed part.
502 If a region is active, export that region.
504 A non-nil optional argument ASYNC means the process should happen
505 asynchronously. The resulting file should be accessible through
506 the `org-export-stack' interface.
508 When optional argument SUBTREEP is non-nil, export the sub-tree
509 at point, extracting information from the headline properties
510 first.
512 When optional argument VISIBLE-ONLY is non-nil, don't export
513 contents of hidden elements.
515 When optional argument BODY-ONLY is non-nil, only write code
516 between \"<body>\" and \"</body>\" tags.
518 EXT-PLIST, when provided, is a property list with external
519 parameters overriding Org default settings, but still inferior to
520 file-local settings.
522 Return output file's name."
523 (interactive)
524 (let* ((extension (concat ".mm" ))
525 (file (org-export-output-file-name extension subtreep)))
526 (if async
527 (org-export-async-start
528 (lambda (f) (org-export-add-to-stack f 'freemind))
529 (let ((org-export-coding-system 'utf-8))
530 `(expand-file-name
531 (org-export-to-file
532 'freemind ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
533 (let ((org-export-coding-system 'utf-8))
534 (org-export-to-file
535 'freemind file subtreep visible-only body-only ext-plist)))))
537 (provide 'ox-freemind)
539 ;; Local variables:
540 ;; generated-autoload-file: "org-loaddefs.el"
541 ;; End:
543 ;;; ox-freemind.el ends here