ox-freemind.el 19 KB

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