org.el (org-read-date-minibuffer-local-map): Check if we are at the beginning of...
[org-mode.git] / contrib / lisp / ox-freemind.el
blob4e90eff2462b367a9498ed062ddcc651f2c66766
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 ;; 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 (assoc 'section element-contents))
318 (section-contents
319 (let* ((translations
320 (nconc (list (cons 'section
321 (lambda (section contents info)
322 contents)))
323 (plist-get info :translate-alist))))
324 (org-export-data-with-translations section translations info)))
325 (itemized-contents-p (let ((first-child-headline
326 (org-element-map element-contents
327 'headline 'identity info t)))
328 (when first-child-headline
329 (org-export-low-level-p first-child-headline
330 info))))
331 (node-contents (concat section-contents
332 (when itemized-contents-p
333 contents))))
334 (concat (let ((title (org-export-data title info)))
335 (case org-freemind-section-format
336 (inline
337 (org-freemind--richcontent
338 'node (concat (format "\n<h2>%s</h2>" title)
339 node-contents) ))
340 (note
341 (concat (org-freemind--richcontent
342 'node (format "\n<p>%s\n</p>" title))
343 (org-freemind--richcontent
344 'note node-contents)))
345 (node
346 (concat
347 (org-freemind--richcontent
348 'node (format "\n<p>%s\n</p>" title))
349 (when section
350 (org-freemind--build-stylized-node
351 (org-freemind--get-node-style section info) nil
352 (org-freemind--richcontent 'node node-contents)))))))
353 (unless itemized-contents-p
354 contents))))
358 ;;; Template
360 (defun org-freemind-template (contents info)
361 "Return complete document string after Freemind Mindmap conversion.
362 CONTENTS is the transcoded contents string. RAW-DATA is the
363 original parsed data. INFO is a plist holding export options."
364 (format
365 "<map version=\"0.9.0\">\n%s\n</map>"
366 (org-freemind--build-stylized-node
367 (org-freemind--get-node-style nil info) nil
368 (let ((org-data (plist-get info :parse-tree)))
369 (org-freemind--build-node-contents org-data contents info)))))
371 (defun org-freemind-inner-template (contents info)
372 "Return body of document string after Freemind Mindmap conversion.
373 CONTENTS is the transcoded contents string. INFO is a plist
374 holding export options."
375 contents)
377 ;;;; Tags
379 (defun org-freemind--tags (tags)
380 (mapconcat (lambda (tag)
381 (format "\n<attribute NAME=\"%s\" VALUE=\"%s\"/>" tag ""))
382 tags "\n"))
386 ;;; Transcode Functions
388 ;;;; Entity
390 (defun org-freemind-entity (entity contents info)
391 "Transcode an ENTITY object from Org to Freemind Mindmap.
392 CONTENTS are the definition itself. INFO is a plist holding
393 contextual information."
394 (org-element-property :utf-8 entity))
396 ;;;; Headline
398 (defun org-freemind-headline (headline contents info)
399 "Transcode a HEADLINE element from Org to Freemind Mindmap.
400 CONTENTS holds the contents of the headline. INFO is a plist
401 holding contextual information."
402 ;; Empty contents?
403 (setq contents (or contents ""))
404 (let* ((numberedp (org-export-numbered-headline-p headline info))
405 (level (org-export-get-relative-level headline info))
406 (text (org-export-data (org-element-property :title headline) info))
407 (todo (and (plist-get info :with-todo-keywords)
408 (let ((todo (org-element-property :todo-keyword headline)))
409 (and todo (org-export-data todo info)))))
410 (todo-type (and todo (org-element-property :todo-type headline)))
411 (tags (and (plist-get info :with-tags)
412 (org-export-get-tags headline info)))
413 (priority (and (plist-get info :with-priority)
414 (org-element-property :priority headline)))
415 (section-number (and (not (org-export-low-level-p headline info))
416 (org-export-numbered-headline-p headline info)
417 (mapconcat 'number-to-string
418 (org-export-get-headline-number
419 headline info) ".")))
420 ;; Create the headline text.
421 (full-text (org-export-data (org-element-property :title headline)
422 info))
423 ;; Headline order (i.e, first digit of the section number)
424 (headline-order (car (org-export-get-headline-number headline info))))
425 (cond
426 ;; Case 1: This is a footnote section: ignore it.
427 ((org-element-property :footnote-section-p headline) nil)
428 ;; Case 2. This is a deep sub-tree, export it as a list item.
429 ;; Delegate the actual export to `html' backend.
430 ((org-export-low-level-p headline info)
431 (org-html-headline headline contents info))
432 ;; Case 3. Standard headline. Export it as a section.
434 (let* ((section-number (mapconcat 'number-to-string
435 (org-export-get-headline-number
436 headline info) "-"))
437 (ids (remove 'nil
438 (list (org-element-property :CUSTOM_ID headline)
439 (concat "sec-" section-number)
440 (org-element-property :ID headline))))
441 (preferred-id (car ids))
442 (extra-ids (cdr ids))
443 (left-p (zerop (% headline-order 2))))
444 (org-freemind--build-stylized-node
445 (org-freemind--get-node-style headline info)
446 (format "<node ID=\"%s\" POSITION=\"%s\" FOLDED=\"%s\">\n</node>"
447 preferred-id
448 (if left-p "left" "right")
449 (if (= level 1) "true" "false"))
450 (concat (org-freemind--build-node-contents headline contents info)
451 (org-freemind--tags tags))))))))
454 ;;;; Section
456 (defun org-freemind-section (section contents info)
457 "Transcode a SECTION element from Org to Freemind Mindmap.
458 CONTENTS holds the contents of the section. INFO is a plist
459 holding contextual information."
460 (let ((parent (org-export-get-parent-headline section)))
461 (when (and parent (org-export-low-level-p parent info))
462 contents)))
466 ;;; Filter Functions
468 (defun org-freemind-final-function (contents backend info)
469 "Return CONTENTS as pretty XML using `indent-region'."
470 (if (not org-freemind-pretty-output) contents
471 (with-temp-buffer
472 (nxml-mode)
473 (insert contents)
474 (indent-region (point-min) (point-max))
475 (buffer-substring-no-properties (point-min) (point-max)))))
477 (defun org-freemind-options-function (info backend)
478 "Install script in export options when appropriate.
479 EXP-PLIST is a plist containing export options. BACKEND is the
480 export back-end currently used."
481 ;; Freemind/Freeplane doesn't seem to like named html entities in
482 ;; richcontent. For now, turn off smart quote processing so that
483 ;; entities like "&rsquo;" & friends are avoided in the exported
484 ;; output.
485 (plist-put info :with-smart-quotes nil))
489 ;;; End-user functions
491 ;;;###autoload
492 (defun org-freemind-export-to-freemind
493 (&optional async subtreep visible-only body-only ext-plist)
494 "Export current buffer to a Freemind Mindmap file.
496 If narrowing is active in the current buffer, only export its
497 narrowed part.
499 If a region is active, export that region.
501 A non-nil optional argument ASYNC means the process should happen
502 asynchronously. The resulting file should be accessible through
503 the `org-export-stack' interface.
505 When optional argument SUBTREEP is non-nil, export the sub-tree
506 at point, extracting information from the headline properties
507 first.
509 When optional argument VISIBLE-ONLY is non-nil, don't export
510 contents of hidden elements.
512 When optional argument BODY-ONLY is non-nil, only write code
513 between \"<body>\" and \"</body>\" tags.
515 EXT-PLIST, when provided, is a property list with external
516 parameters overriding Org default settings, but still inferior to
517 file-local settings.
519 Return output file's name."
520 (interactive)
521 (let* ((extension (concat ".mm" ))
522 (file (org-export-output-file-name extension subtreep)))
523 (if async
524 (org-export-async-start
525 (lambda (f) (org-export-add-to-stack f 'freemind))
526 (let ((org-export-coding-system 'utf-8))
527 `(expand-file-name
528 (org-export-to-file
529 'freemind ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
530 (let ((org-export-coding-system 'utf-8))
531 (org-export-to-file
532 'freemind file subtreep visible-only body-only ext-plist)))))
534 (provide 'ox-freemind)
536 ;;; ox-freemind.el ends here