ox-freemind.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. ;;; ox-freemind.el --- Freemind Mindmap Back-End for Org Export Engine
  2. ;; Copyright (C) 2013 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 (assoc 'section element-contents))
  261. (section-contents
  262. (let* ((translations
  263. (nconc (list (cons 'section
  264. (lambda (section contents info)
  265. contents)))
  266. (plist-get info :translate-alist))))
  267. (org-export-data-with-translations section translations info)))
  268. (itemized-contents-p (let ((first-child-headline
  269. (org-element-map element-contents
  270. 'headline 'identity info t)))
  271. (when first-child-headline
  272. (org-export-low-level-p first-child-headline
  273. info))))
  274. (node-contents (concat section-contents
  275. (when itemized-contents-p
  276. contents))))
  277. (concat (let ((title (org-export-data title info)))
  278. (case org-freemind-section-format
  279. (inline
  280. (org-freemind--richcontent
  281. 'node (concat (format "\n<h2>%s</h2>" title)
  282. node-contents) ))
  283. (note
  284. (concat (org-freemind--richcontent
  285. 'node (format "\n<p>%s\n</p>" title))
  286. (org-freemind--richcontent
  287. 'note node-contents)))
  288. (node
  289. (concat
  290. (org-freemind--richcontent
  291. 'node (format "\n<p>%s\n</p>" title))
  292. (when section
  293. (org-freemind--build-stylized-node
  294. (org-freemind--get-node-style section info) nil
  295. (org-freemind--richcontent 'node node-contents)))))))
  296. (unless itemized-contents-p
  297. contents))))
  298. ;;; Template
  299. (defun org-freemind-template (contents info)
  300. "Return complete document string after Freemind Mindmap conversion.
  301. CONTENTS is the transcoded contents string. RAW-DATA is the
  302. original parsed data. INFO is a plist holding export options."
  303. (format
  304. "<map version=\"0.9.0\">\n%s\n</map>"
  305. (org-freemind--build-stylized-node
  306. (org-freemind--get-node-style nil info) nil
  307. (let ((org-data (plist-get info :parse-tree)))
  308. (org-freemind--build-node-contents org-data contents info)))))
  309. (defun org-freemind-inner-template (contents info)
  310. "Return body of document string after Freemind Mindmap conversion.
  311. CONTENTS is the transcoded contents string. INFO is a plist
  312. holding export options."
  313. contents)
  314. ;;;; Tags
  315. (defun org-freemind--tags (tags)
  316. (mapconcat (lambda (tag)
  317. (format "\n<attribute NAME=\"%s\" VALUE=\"%s\"/>" tag ""))
  318. tags "\n"))
  319. ;;; Transcode Functions
  320. ;;;; Entity
  321. (defun org-freemind-entity (entity contents info)
  322. "Transcode an ENTITY object from Org to Freemind Mindmap.
  323. CONTENTS are the definition itself. INFO is a plist holding
  324. contextual information."
  325. (org-element-property :utf-8 entity))
  326. ;;;; Headline
  327. (defun org-freemind-headline (headline contents info)
  328. "Transcode a HEADLINE element from Org to Freemind Mindmap.
  329. CONTENTS holds the contents of the headline. INFO is a plist
  330. holding contextual information."
  331. ;; Empty contents?
  332. (setq contents (or contents ""))
  333. (let* ((numberedp (org-export-numbered-headline-p headline info))
  334. (level (org-export-get-relative-level headline info))
  335. (text (org-export-data (org-element-property :title headline) info))
  336. (todo (and (plist-get info :with-todo-keywords)
  337. (let ((todo (org-element-property :todo-keyword headline)))
  338. (and todo (org-export-data todo info)))))
  339. (todo-type (and todo (org-element-property :todo-type headline)))
  340. (tags (and (plist-get info :with-tags)
  341. (org-export-get-tags headline info)))
  342. (priority (and (plist-get info :with-priority)
  343. (org-element-property :priority headline)))
  344. (section-number (and (not (org-export-low-level-p headline info))
  345. (org-export-numbered-headline-p headline info)
  346. (mapconcat 'number-to-string
  347. (org-export-get-headline-number
  348. headline info) ".")))
  349. ;; Create the headline text.
  350. (full-text (org-export-data (org-element-property :title headline)
  351. info))
  352. ;; Headline order (i.e, first digit of the section number)
  353. (headline-order (car (org-export-get-headline-number headline info))))
  354. (cond
  355. ;; Case 1: This is a footnote section: ignore it.
  356. ((org-element-property :footnote-section-p headline) nil)
  357. ;; Case 2. This is a deep sub-tree, export it as a list item.
  358. ;; Delegate the actual export to `html' backend.
  359. ((org-export-low-level-p headline info)
  360. (org-html-headline headline contents info))
  361. ;; Case 3. Standard headline. Export it as a section.
  362. (t
  363. (let* ((section-number (mapconcat 'number-to-string
  364. (org-export-get-headline-number
  365. headline info) "-"))
  366. (ids (remove 'nil
  367. (list (org-element-property :CUSTOM_ID headline)
  368. (concat "sec-" section-number)
  369. (org-element-property :ID headline))))
  370. (preferred-id (car ids))
  371. (extra-ids (cdr ids))
  372. (left-p (zerop (% headline-order 2))))
  373. (org-freemind--build-stylized-node
  374. (org-freemind--get-node-style headline info)
  375. (format "<node ID=\"%s\" POSITION=\"%s\" FOLDED=\"%s\">\n</node>"
  376. preferred-id
  377. (if left-p "left" "right")
  378. (if (= level 1) "true" "false"))
  379. (concat (org-freemind--build-node-contents headline contents info)
  380. (org-freemind--tags tags))))))))
  381. ;;;; Section
  382. (defun org-freemind-section (section contents info)
  383. "Transcode a SECTION element from Org to Freemind Mindmap.
  384. CONTENTS holds the contents of the section. INFO is a plist
  385. holding contextual information."
  386. (let ((parent (org-export-get-parent-headline section)))
  387. (when (and parent (org-export-low-level-p parent info))
  388. contents)))
  389. ;;; Filter Functions
  390. (defun org-freemind-final-function (contents backend info)
  391. "Return CONTENTS as pretty XML using `indent-region'."
  392. (if (not org-freemind-pretty-output) contents
  393. (with-temp-buffer
  394. (nxml-mode)
  395. (insert contents)
  396. (indent-region (point-min) (point-max))
  397. (buffer-substring-no-properties (point-min) (point-max)))))
  398. (defun org-freemind-options-function (info backend)
  399. "Install script in export options when appropriate.
  400. EXP-PLIST is a plist containing export options. BACKEND is the
  401. export back-end currently used."
  402. ;; Freemind/Freeplane doesn't seem to like named html entities in
  403. ;; richcontent. For now, turn off smart quote processing so that
  404. ;; entities like "&rsquo;" & friends are avoided in the exported
  405. ;; output.
  406. (plist-put info :with-smart-quotes nil))
  407. ;;; End-user functions
  408. ;;;###autoload
  409. (defun org-freemind-export-to-freemind
  410. (&optional async subtreep visible-only body-only ext-plist)
  411. "Export current buffer to a Freemind Mindmap file.
  412. If narrowing is active in the current buffer, only export its
  413. narrowed part.
  414. If a region is active, export that region.
  415. A non-nil optional argument ASYNC means the process should happen
  416. asynchronously. The resulting file should be accessible through
  417. the `org-export-stack' interface.
  418. When optional argument SUBTREEP is non-nil, export the sub-tree
  419. at point, extracting information from the headline properties
  420. first.
  421. When optional argument VISIBLE-ONLY is non-nil, don't export
  422. contents of hidden elements.
  423. When optional argument BODY-ONLY is non-nil, only write code
  424. between \"<body>\" and \"</body>\" tags.
  425. EXT-PLIST, when provided, is a property list with external
  426. parameters overriding Org default settings, but still inferior to
  427. file-local settings.
  428. Return output file's name."
  429. (interactive)
  430. (let* ((extension (concat ".mm" ))
  431. (file (org-export-output-file-name extension subtreep)))
  432. (if async
  433. (org-export-async-start
  434. (lambda (f) (org-export-add-to-stack f 'freemind))
  435. (let ((org-export-coding-system 'utf-8))
  436. `(expand-file-name
  437. (org-export-to-file
  438. 'freemind ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
  439. (let ((org-export-coding-system 'utf-8))
  440. (org-export-to-file
  441. 'freemind file subtreep visible-only body-only ext-plist)))))
  442. (provide 'ox-freemind)
  443. ;;; ox-freemind.el ends here