Remove :version and :package-version in contributed packages
[org-mode.git] / contrib / lisp / ox-freemind.el
blobaf7cffa35826d9cfd9da5feb41b875553f2e9ed8
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)
69 (defcustom org-freemind-styles
70 '((default . "<node>\n</node>")
71 (0 . "<node COLOR=\"#000000\">\n<font NAME=\"SansSerif\" SIZE=\"20\"/>\n</node>")
72 (1 . "<node COLOR=\"#0033ff\">\n<edge STYLE=\"sharp_bezier\" WIDTH=\"8\"/>\n<font NAME=\"SansSerif\" SIZE=\"18\"/>\n</node>")
73 (2 . "<node COLOR=\"#00b439\">\n<edge STYLE=\"bezier\" WIDTH=\"thin\"/>\n<font NAME=\"SansSerif\" SIZE=\"16\"/>\n</node>")
74 (3 . "<node COLOR=\"#990000\" FOLDED=\"true\">\n<font NAME=\"SansSerif\" SIZE=\"14\"/>\n</node>")
75 (4 . "<node COLOR=\"#111111\">\n</node>"))
76 "List of Freemind node styles.
77 Each entry is of the form (STYLE-NAME . STYLE-SPEC). STYLE-NAME
78 can be one of an integer (signifying an outline level), a string
79 or the symbol `default'. STYLE-SPEC, a string, is a Freemind
80 node style."
81 :type '(alist :options (default 0 1 2 3)
82 :key-type (choice :tag "Style tag"
83 (integer :tag "Outline level")
84 (const :tag "Default value" default)
85 (string :tag "Node style"))
86 :value-type (string :tag "Style spec"))
87 :group 'org-export-freemind)
89 (defcustom org-freemind-style-map-function 'org-freemind-style-map--automatic
90 "Function to map an Org element to it's node style.
91 The mapping function takes two arguments an Org ELEMENT and INFO.
92 ELEMENT can be one of the following types - `org-data',
93 `headline' or `section'. INFO is a plist holding contextual
94 information during export. The function must return a STYLE-SPEC
95 to be applied to ELEMENT.
97 See `org-freemind-style-map--automatic' for a sample style
98 function. See `org-freemind-styles' for a list of named styles."
99 :type '(radio
100 (function-item org-freemind-style-map--automatic)
101 (function-item org-freemind-style-map--default)
102 function)
103 :group 'org-export-freemind)
105 (defcustom org-freemind-section-format 'note
106 "Specify how outline sections are to be formatted.
107 If `inline', append it to the contents of it's heading node. If
108 `note', attach it as a note to it's heading node. If `node',
109 attach it as a separate node to it's heading node.
111 Use `note', if the input Org file contains large sections. Use
112 `node', if the Org file contains mid-sized sections that need to
113 stand apart. Otherwise, use `inline'."
114 :type '(choice
115 (const :tag "Append to outline title" inline)
116 (const :tag "Attach as a note" note)
117 (const :tag "Create a separate node" node))
118 :group 'org-export-freemind)
120 ;;;; Debugging
122 (defcustom org-freemind-pretty-output nil
123 "Enable this to generate pretty Freemind Mindmap."
124 :type 'boolean
125 :group 'org-export-freemind)
128 ;;; Internal Functions
130 ;;;; XML Manipulation
132 (defun org-freemind--serialize (parsed-xml &optional contents)
133 "Convert PARSED-XML in to XML string.
134 PARSED-XML is a parse tree as returned by
135 `libxml-parse-xml-region'. CONTENTS is an optional string.
137 Ignore CONTENTS, if PARSED-XML is not a sole XML element.
138 Otherwise, append CONTENTS to the contents of top-level element
139 in PARSED-XML.
141 This is an inverse function of `libxml-parse-xml-region'.
143 For purposes of Freemind export, PARSED-XML is a node style
144 specification - \"<node ...>...</node>\" - as a parse tree."
145 (when contents
146 (assert (symbolp (car parsed-xml))))
147 (cond
148 ((null parsed-xml) "")
149 ((stringp parsed-xml) parsed-xml)
150 ((symbolp (car parsed-xml))
151 (let ((attributes (mapconcat
152 (lambda (av)
153 (format "%s=\"%s\"" (car av) (cdr av)))
154 (cadr parsed-xml) " ")))
155 (if (or (cddr parsed-xml) contents)
156 (format "\n<%s%s>%s\n</%s>"
157 (car parsed-xml)
158 (if (string= attributes "") "" (concat " " attributes))
159 (concat (org-freemind--serialize (cddr parsed-xml))
160 contents )
161 (car parsed-xml))
162 (format "\n<%s%s/>"
163 (car parsed-xml)
164 (if (string= attributes "") "" (concat " " attributes))))))
165 (t (mapconcat #'org-freemind--serialize parsed-xml ""))))
167 (defun org-freemind--parse-xml (xml-string)
168 "Return parse tree for XML-STRING using `libxml-parse-xml-region'.
169 For purposes of Freemind export, XML-STRING is a node style
170 specification - \"<node ...>...</node>\" - as a string."
171 (with-temp-buffer
172 (insert (or xml-string ""))
173 (libxml-parse-xml-region (point-min) (point-max))))
176 ;;;; Style mappers :: Default and Automatic layout
178 (defun org-freemind-style-map--automatic (element info)
179 "Return a node style corresponding to relative outline level of ELEMENT.
180 ELEMENT can be any of the following types - `org-data',
181 `headline' or `section'. See `org-freemind-styles' for style
182 mappings of different outline levels."
183 (let ((style-name
184 (case (org-element-type element)
185 (headline
186 (org-export-get-relative-level element info))
187 (section
188 (let ((parent (org-export-get-parent-headline element)))
189 (if (not parent) 1
190 (1+ (org-export-get-relative-level parent info)))))
191 (t 0))))
192 (or (assoc-default style-name org-freemind-styles)
193 (assoc-default 'default org-freemind-styles)
194 "<node></node>")))
196 (defun org-freemind-style-map--default (element info)
197 "Return the default style for all ELEMENTs.
198 ELEMENT can be any of the following types - `org-data',
199 `headline' or `section'. See `org-freemind-styles' for current
200 value of default style."
201 (or (assoc-default 'default org-freemind-styles)
202 "<node></node>"))
205 ;;;; Helpers :: Retrieve, apply Freemind styles
207 (defun org-freemind--get-node-style (element info)
208 "Return Freemind node style applicable for HEADLINE.
209 ELEMENT is an Org element of type `org-data', `headline' or
210 `section'. INFO is a plist holding contextual information."
211 (unless (fboundp org-freemind-style-map-function)
212 (setq org-freemind-style-map-function 'org-freemind-style-map--default))
213 (let ((style (funcall org-freemind-style-map-function element info)))
214 ;; Sanitize node style.
216 ;; Loop through the attributes of node element and purge those
217 ;; attributes that look suspicious. This is an extra bit of work
218 ;; that allows one to copy verbatim node styles from an existing
219 ;; Freemind Mindmap file without messing with the exported data.
220 (let* ((data (org-freemind--parse-xml style))
221 (attributes (cadr data))
222 (ignored-attrs '(POSITION FOLDED TEXT CREATED ID
223 MODIFIED)))
224 (let (attr)
225 (while (setq attr (pop ignored-attrs))
226 (setq attributes (assq-delete-all attr attributes))))
227 (when data (setcar (cdr data) attributes))
228 (org-freemind--serialize data))))
230 (defun org-freemind--build-stylized-node (style-1 style-2 &optional contents)
231 "Build a Freemind node with style STYLE-1 + STYLE-2 and add CONTENTS to it.
232 STYLE-1 and STYLE-2 are Freemind node styles as a string.
233 STYLE-1 is the base node style and STYLE-2 is the overriding
234 style that takes precedence over STYLE-1. CONTENTS is a string.
236 Return value is a Freemind node with following properties:
238 1. The attributes of \"<node ...> </node>\" element is the union
239 of corresponding attributes of STYLE-1 and STYLE-2. When
240 STYLE-1 and STYLE-2 specify values for the same attribute
241 name, choose the attribute value from STYLE-2.
243 2. The children of \"<node ...> </node>\" element is the union of
244 top-level children of STYLE-1 and STYLE-2 with CONTENTS
245 appended to it. When STYLE-1 and STYLE-2 share a child
246 element of same type, the value chosen is that from STYLE-2.
248 For example, merging with following parameters
250 STYLE-1 =>
251 <node COLOR=\"#00b439\" STYLE=\"Bubble\">
252 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
253 <font NAME=\"SansSerif\" SIZE=\"16\"/>
254 </node>
256 STYLE-2 =>
257 <node COLOR=\"#990000\" FOLDED=\"true\">
258 <font NAME=\"SansSerif\" SIZE=\"14\"/>
259 </node>
261 CONTENTS =>
262 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
264 will result in following node:
266 RETURN =>
267 <node STYLE=\"Bubble\" COLOR=\"#990000\" FOLDED=\"true\">
268 <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
269 <font NAME=\"SansSerif\" SIZE=\"14\"/>
270 <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
271 </node>."
272 (let* ((data1 (org-freemind--parse-xml (or style-1 "")))
273 (data2 (org-freemind--parse-xml (or style-2 "")))
274 (attr1 (cadr data1))
275 (attr2 (cadr data2))
276 (merged-attr attr2)
277 (children1 (cddr data1))
278 (children2 (cddr data2))
279 (merged-children children2))
280 (let (attr)
281 (while (setq attr (pop attr1))
282 (unless (assq (car attr) merged-attr)
283 (push attr merged-attr))))
284 (let (child)
285 (while (setq child (pop children1))
286 (when (or (stringp child) (not (assq (car child) merged-children)))
287 (push child merged-children))))
288 (let ((merged-data (nconc (list 'node merged-attr) merged-children)))
289 (org-freemind--serialize merged-data contents))))
292 ;;;; Helpers :: Node contents
294 (defun org-freemind--richcontent (type contents &optional css-style)
295 (let* ((type (case type
296 (note "NOTE")
297 (node "NODE")
298 (t "NODE")))
299 (contents (org-trim contents)))
300 (if (string= (org-trim contents) "") ""
301 (format "\n<richcontent TYPE=\"%s\">%s\n</richcontent>"
302 type
303 (format "\n<html>\n<head>%s\n</head>\n%s\n</html>"
304 (or css-style "")
305 (format "<body>\n%s\n</body>" contents))))))
307 (defun org-freemind--build-node-contents (element contents info)
308 (let* ((title (case (org-element-type element)
309 (headline
310 (org-element-property :title element))
311 (org-data
312 (plist-get info :title))
313 (t (error "Shouldn't come here."))))
314 (element-contents (org-element-contents element))
315 (section (assoc 'section element-contents))
316 (section-contents
317 (let* ((translations
318 (nconc (list (cons 'section
319 (lambda (section contents info)
320 contents)))
321 (plist-get info :translate-alist))))
322 (org-export-data-with-translations section translations info)))
323 (itemized-contents-p (let ((first-child-headline
324 (org-element-map element-contents
325 'headline 'identity info t)))
326 (when first-child-headline
327 (org-export-low-level-p first-child-headline
328 info))))
329 (node-contents (concat section-contents
330 (when itemized-contents-p
331 contents))))
332 (concat (let ((title (org-export-data title info)))
333 (case org-freemind-section-format
334 (inline
335 (org-freemind--richcontent
336 'node (concat (format "\n<h2>%s</h2>" title)
337 node-contents) ))
338 (note
339 (concat (org-freemind--richcontent
340 'node (format "\n<p>%s\n</p>" title))
341 (org-freemind--richcontent
342 'note node-contents)))
343 (node
344 (concat
345 (org-freemind--richcontent
346 'node (format "\n<p>%s\n</p>" title))
347 (when section
348 (org-freemind--build-stylized-node
349 (org-freemind--get-node-style section info) nil
350 (org-freemind--richcontent 'node node-contents)))))))
351 (unless itemized-contents-p
352 contents))))
356 ;;; Template
358 (defun org-freemind-template (contents info)
359 "Return complete document string after Freemind Mindmap conversion.
360 CONTENTS is the transcoded contents string. RAW-DATA is the
361 original parsed data. INFO is a plist holding export options."
362 (format
363 "<map version=\"0.9.0\">\n%s\n</map>"
364 (org-freemind--build-stylized-node
365 (org-freemind--get-node-style nil info) nil
366 (let ((org-data (plist-get info :parse-tree)))
367 (org-freemind--build-node-contents org-data contents info)))))
369 (defun org-freemind-inner-template (contents info)
370 "Return body of document string after Freemind Mindmap conversion.
371 CONTENTS is the transcoded contents string. INFO is a plist
372 holding export options."
373 contents)
375 ;;;; Tags
377 (defun org-freemind--tags (tags)
378 (mapconcat (lambda (tag)
379 (format "\n<attribute NAME=\"%s\" VALUE=\"%s\"/>" tag ""))
380 tags "\n"))
384 ;;; Transcode Functions
386 ;;;; Entity
388 (defun org-freemind-entity (entity contents info)
389 "Transcode an ENTITY object from Org to Freemind Mindmap.
390 CONTENTS are the definition itself. INFO is a plist holding
391 contextual information."
392 (org-element-property :utf-8 entity))
394 ;;;; Headline
396 (defun org-freemind-headline (headline contents info)
397 "Transcode a HEADLINE element from Org to Freemind Mindmap.
398 CONTENTS holds the contents of the headline. INFO is a plist
399 holding contextual information."
400 ;; Empty contents?
401 (setq contents (or contents ""))
402 (let* ((numberedp (org-export-numbered-headline-p headline info))
403 (level (org-export-get-relative-level headline info))
404 (text (org-export-data (org-element-property :title headline) info))
405 (todo (and (plist-get info :with-todo-keywords)
406 (let ((todo (org-element-property :todo-keyword headline)))
407 (and todo (org-export-data todo info)))))
408 (todo-type (and todo (org-element-property :todo-type headline)))
409 (tags (and (plist-get info :with-tags)
410 (org-export-get-tags headline info)))
411 (priority (and (plist-get info :with-priority)
412 (org-element-property :priority headline)))
413 (section-number (and (not (org-export-low-level-p headline info))
414 (org-export-numbered-headline-p headline info)
415 (mapconcat 'number-to-string
416 (org-export-get-headline-number
417 headline info) ".")))
418 ;; Create the headline text.
419 (full-text (org-export-data (org-element-property :title headline)
420 info))
421 ;; Headline order (i.e, first digit of the section number)
422 (headline-order (car (org-export-get-headline-number headline info))))
423 (cond
424 ;; Case 1: This is a footnote section: ignore it.
425 ((org-element-property :footnote-section-p headline) nil)
426 ;; Case 2. This is a deep sub-tree, export it as a list item.
427 ;; Delegate the actual export to `html' backend.
428 ((org-export-low-level-p headline info)
429 (org-html-headline headline contents info))
430 ;; Case 3. Standard headline. Export it as a section.
432 (let* ((section-number (mapconcat 'number-to-string
433 (org-export-get-headline-number
434 headline info) "-"))
435 (ids (remove 'nil
436 (list (org-element-property :CUSTOM_ID headline)
437 (concat "sec-" section-number)
438 (org-element-property :ID headline))))
439 (preferred-id (car ids))
440 (extra-ids (cdr ids))
441 (left-p (zerop (% headline-order 2))))
442 (org-freemind--build-stylized-node
443 (org-freemind--get-node-style headline info)
444 (format "<node ID=\"%s\" POSITION=\"%s\" FOLDED=\"%s\">\n</node>"
445 preferred-id
446 (if left-p "left" "right")
447 (if (= level 1) "true" "false"))
448 (concat (org-freemind--build-node-contents headline contents info)
449 (org-freemind--tags tags))))))))
452 ;;;; Section
454 (defun org-freemind-section (section contents info)
455 "Transcode a SECTION element from Org to Freemind Mindmap.
456 CONTENTS holds the contents of the section. INFO is a plist
457 holding contextual information."
458 (let ((parent (org-export-get-parent-headline section)))
459 (when (and parent (org-export-low-level-p parent info))
460 contents)))
464 ;;; Filter Functions
466 (defun org-freemind-final-function (contents backend info)
467 "Return CONTENTS as pretty XML using `indent-region'."
468 (if (not org-freemind-pretty-output) contents
469 (with-temp-buffer
470 (nxml-mode)
471 (insert contents)
472 (indent-region (point-min) (point-max))
473 (buffer-substring-no-properties (point-min) (point-max)))))
475 (defun org-freemind-options-function (info backend)
476 "Install script in export options when appropriate.
477 EXP-PLIST is a plist containing export options. BACKEND is the
478 export back-end currently used."
479 ;; Freemind/Freeplane doesn't seem to like named html entities in
480 ;; richcontent. For now, turn off smart quote processing so that
481 ;; entities like "&rsquo;" & friends are avoided in the exported
482 ;; output.
483 (plist-put info :with-smart-quotes nil))
487 ;;; End-user functions
489 ;;;###autoload
490 (defun org-freemind-export-to-freemind
491 (&optional async subtreep visible-only body-only ext-plist)
492 "Export current buffer to a Freemind Mindmap file.
494 If narrowing is active in the current buffer, only export its
495 narrowed part.
497 If a region is active, export that region.
499 A non-nil optional argument ASYNC means the process should happen
500 asynchronously. The resulting file should be accessible through
501 the `org-export-stack' interface.
503 When optional argument SUBTREEP is non-nil, export the sub-tree
504 at point, extracting information from the headline properties
505 first.
507 When optional argument VISIBLE-ONLY is non-nil, don't export
508 contents of hidden elements.
510 When optional argument BODY-ONLY is non-nil, only write code
511 between \"<body>\" and \"</body>\" tags.
513 EXT-PLIST, when provided, is a property list with external
514 parameters overriding Org default settings, but still inferior to
515 file-local settings.
517 Return output file's name."
518 (interactive)
519 (let* ((extension (concat ".mm" ))
520 (file (org-export-output-file-name extension subtreep)))
521 (if async
522 (org-export-async-start
523 (lambda (f) (org-export-add-to-stack f 'freemind))
524 (let ((org-export-coding-system 'utf-8))
525 `(expand-file-name
526 (org-export-to-file
527 'freemind ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
528 (let ((org-export-coding-system 'utf-8))
529 (org-export-to-file
530 'freemind file subtreep visible-only body-only ext-plist)))))
532 (provide 'ox-freemind)
534 ;; Local variables:
535 ;; generated-autoload-file: "org-loaddefs.el"
536 ;; End:
538 ;;; ox-freemind.el ends here