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/>.
23 ;; This library implements a Freemind Mindmap back-end for Org generic
28 ;; M-x org-freemind-export-to-freemind
30 ;; in an Org mode buffer. See ox.el for more details on how this
43 (org-export-define-derived-backend freemind html
44 :export-block
"FREEMIND"
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"
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"
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
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
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."
102 (function-item org-freemind-style-map--automatic
)
103 (function-item org-freemind-style-map--default
)
105 :group
'org-export-freemind
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'."
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
126 (defcustom org-freemind-pretty-output nil
127 "Enable this to generate pretty Freemind Mindmap."
129 :group
'org-export-freemind
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
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."
151 (assert (symbolp (car parsed-xml
))))
153 ((null parsed-xml
) "")
154 ((stringp parsed-xml
) parsed-xml
)
155 ((symbolp (car parsed-xml
))
156 (let ((attributes (mapconcat
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>"
163 (if (string= attributes
"") "" (concat " " attributes
))
164 (concat (org-freemind--serialize (cddr 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."
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."
189 (case (org-element-type element
)
191 (org-export-get-relative-level element info
))
193 (let ((parent (org-export-get-parent-headline element
)))
195 (1+ (org-export-get-relative-level parent info
)))))
197 (or (assoc-default style-name org-freemind-styles
)
198 (assoc-default 'default org-freemind-styles
)
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
)
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
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
256 <node COLOR=\"#00b439\" STYLE=\"Bubble\">
257 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
258 <font NAME=\"SansSerif\" SIZE=\"16\"/>
262 <node COLOR=\"#990000\" FOLDED=\"true\">
263 <font NAME=\"SansSerif\" SIZE=\"14\"/>
267 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
269 will result in following node:
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\"/>
277 (let* ((data1 (org-freemind--parse-xml (or style-1
"")))
278 (data2 (org-freemind--parse-xml (or style-2
"")))
282 (children1 (cddr data1
))
283 (children2 (cddr data2
))
284 (merged-children children2
))
286 (while (setq attr
(pop attr1
))
287 (unless (assq (car attr
) merged-attr
)
288 (push attr merged-attr
))))
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
304 (contents (org-trim contents
)))
305 (if (string= (org-trim contents
) "") ""
306 (format "\n<richcontent TYPE=\"%s\">%s\n</richcontent>"
308 (format "\n<html>\n<head>%s\n</head>\n%s\n</html>"
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
)
315 (org-element-property :title element
))
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
))
323 (nconc (list (cons 'section
324 (lambda (section contents info
)
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
334 (node-contents (concat section-contents
335 (when itemized-contents-p
337 (concat (let ((title (org-export-data title info
)))
338 (case org-freemind-section-format
340 (org-freemind--richcontent
341 'node
(concat (format "\n<h2>%s</h2>" title
)
344 (concat (org-freemind--richcontent
345 'node
(format "\n<p>%s\n</p>" title
))
346 (org-freemind--richcontent
347 'note node-contents
)))
350 (org-freemind--richcontent
351 'node
(format "\n<p>%s\n</p>" title
))
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
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."
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."
382 (defun org-freemind--tags (tags)
383 (mapconcat (lambda (tag)
384 (format "\n<attribute NAME=\"%s\" VALUE=\"%s\"/>" tag
""))
389 ;;; Transcode Functions
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
))
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."
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
)
426 ;; Headline order (i.e, first digit of the section number)
427 (headline-order (car (org-export-get-headline-number headline info
))))
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
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>"
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
))))))))
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
))
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
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 "’" & friends are avoided in the exported
488 (plist-put info
:with-smart-quotes nil
))
492 ;;; End-user functions
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
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
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
522 Return output file's name."
524 (let* ((extension (concat ".mm" ))
525 (file (org-export-output-file-name extension subtreep
)))
527 (org-export-async-start
528 (lambda (f) (org-export-add-to-stack f
'freemind
))
529 (let ((org-export-coding-system 'utf-8
))
532 'freemind
,file
,subtreep
,visible-only
,body-only
',ext-plist
))))
533 (let ((org-export-coding-system 'utf-8
))
535 'freemind file subtreep visible-only body-only ext-plist
)))))
537 (provide 'ox-freemind
)
540 ;; generated-autoload-file: "org-loaddefs.el"
543 ;;; ox-freemind.el ends here