ox-freemind.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a Freemind Mindmap back-end for Org generic
  17. ;; exporter.
  18. ;; To test it, run:
  19. ;;
  20. ;; M-x org-freemind-export-to-freemind
  21. ;;
  22. ;; in an Org mode buffer. See ox.el for more details on how this
  23. ;; exporter works.
  24. ;;; Code:
  25. ;;; Dependencies
  26. (require 'ox-html)
  27. ;;; Define Back-End
  28. (org-export-define-derived-backend freemind html
  29. :export-block "FREEMIND"
  30. :menu-entry
  31. (?f "Export to Freemind Mindmap"
  32. ((?f "As Freemind Mindmap file" org-freemind-export-to-freemind)
  33. (?o "As Freemind Mindmap file and open"
  34. (lambda (a s v b)
  35. (if a (org-freemind-export-to-freemind t s v b)
  36. (org-open-file (org-freemind-export-to-freemind nil s v b)))))))
  37. :translate-alist ((headline . org-freemind-headline)
  38. (template . org-freemind-template)
  39. (inner-template . org-freemind-inner-template)
  40. (section . org-freemind-section)
  41. (entity . org-freemind-entity))
  42. :filters-alist ((:filter-options . org-freemind-options-function)
  43. (:filter-final-output . org-freemind-final-function)))
  44. ;;; User Configuration Variables
  45. (defgroup org-export-freemind nil
  46. "Options for exporting Org mode files to Freemind Mindmap."
  47. :tag "Org Export Freemind Mindmap"
  48. :group 'org-export)
  49. (defcustom org-freemind-styles
  50. '((default . "<node>\n</node>")
  51. (0 . "<node COLOR=\"#000000\">\n<font NAME=\"SansSerif\" SIZE=\"20\"/>\n</node>")
  52. (1 . "<node COLOR=\"#0033ff\">\n<edge STYLE=\"sharp_bezier\" WIDTH=\"8\"/>\n<font NAME=\"SansSerif\" SIZE=\"18\"/>\n</node>")
  53. (2 . "<node COLOR=\"#00b439\">\n<edge STYLE=\"bezier\" WIDTH=\"thin\"/>\n<font NAME=\"SansSerif\" SIZE=\"16\"/>\n</node>")
  54. (3 . "<node COLOR=\"#990000\" FOLDED=\"true\">\n<font NAME=\"SansSerif\" SIZE=\"14\"/>\n</node>")
  55. (4 . "<node COLOR=\"#111111\">\n</node>"))
  56. "List of Freemind node styles.
  57. Each entry is of the form (STYLE-NAME . STYLE-SPEC). STYLE-NAME
  58. can be one of an integer (signifying an outline level), a string
  59. or the symbol `default'. STYLE-SPEC, a string, is a Freemind
  60. node style."
  61. :type '(alist :options (default 0 1 2 3)
  62. :key-type (choice :tag "Style tag"
  63. (integer :tag "Outline level")
  64. (const :tag "Default value" default)
  65. (string :tag "Node style"))
  66. :value-type (string :tag "Style spec"))
  67. :group 'org-export-freemind)
  68. (defcustom org-freemind-style-map-function 'org-freemind-style-map--automatic
  69. "Function to map an Org element to it's node style.
  70. The mapping function takes two arguments an Org ELEMENT and INFO.
  71. ELEMENT can be one of the following types - `org-data',
  72. `headline' or `section'. INFO is a plist holding contextual
  73. information during export. The function must return a STYLE-SPEC
  74. to be applied to ELEMENT.
  75. See `org-freemind-style-map--automatic' for a sample style
  76. function. See `org-freemind-styles' for a list of named styles."
  77. :type '(radio
  78. (function-item org-freemind-style-map--automatic)
  79. (function-item org-freemind-style-map--default)
  80. function)
  81. :group 'org-export-freemind)
  82. (defcustom org-freemind-section-format 'note
  83. "Specify how outline sections are to be formatted.
  84. If `inline', append it to the contents of it's heading node. If
  85. `note', attach it as a note to it's heading node. If `node',
  86. attach it as a separate node to it's heading node.
  87. Use `note', if the input Org file contains large sections. Use
  88. `node', if the Org file contains mid-sized sections that need to
  89. stand apart. Otherwise, use `inline'."
  90. :type '(choice
  91. (const :tag "Append to outline title" inline)
  92. (const :tag "Attach as a note" note)
  93. (const :tag "Create a separate node" node))
  94. :group 'org-export-freemind)
  95. ;;;; Debugging
  96. (defcustom org-freemind-pretty-output nil
  97. "Enable this to generate pretty Freemind Mindmap."
  98. :type 'boolean
  99. :group 'org-export-freemind)
  100. ;;; Internal Functions
  101. ;;;; XML Manipulation
  102. (defun org-freemind--serialize (parsed-xml &optional contents)
  103. "Convert PARSED-XML in to XML string.
  104. PARSED-XML is a parse tree as returned by
  105. `libxml-parse-xml-region'. CONTENTS is an optional string.
  106. Ignore CONTENTS, if PARSED-XML is not a sole XML element.
  107. Otherwise, append CONTENTS to the contents of top-level element
  108. in PARSED-XML.
  109. This is an inverse function of `libxml-parse-xml-region'.
  110. For purposes of Freemind export, PARSED-XML is a node style
  111. specification - \"<node ...>...</node>\" - as a parse tree."
  112. (when contents
  113. (assert (symbolp (car parsed-xml))))
  114. (cond
  115. ((null parsed-xml) "")
  116. ((stringp parsed-xml) parsed-xml)
  117. ((symbolp (car parsed-xml))
  118. (let ((attributes (mapconcat
  119. (lambda (av)
  120. (format "%s=\"%s\"" (car av) (cdr av)))
  121. (cadr parsed-xml) " ")))
  122. (if (or (cddr parsed-xml) contents)
  123. (format "\n<%s%s>%s\n</%s>"
  124. (car parsed-xml)
  125. (if (string= attributes "") "" (concat " " attributes))
  126. (concat (org-freemind--serialize (cddr parsed-xml))
  127. contents )
  128. (car parsed-xml))
  129. (format "\n<%s%s/>"
  130. (car parsed-xml)
  131. (if (string= attributes "") "" (concat " " attributes))))))
  132. (t (mapconcat #'org-freemind--serialize parsed-xml ""))))
  133. (defun org-freemind--parse-xml (xml-string)
  134. "Return parse tree for XML-STRING using `libxml-parse-xml-region'.
  135. For purposes of Freemind export, XML-STRING is a node style
  136. specification - \"<node ...>...</node>\" - as a string."
  137. (with-temp-buffer
  138. (insert (or xml-string ""))
  139. (libxml-parse-xml-region (point-min) (point-max))))
  140. ;;;; Style mappers :: Default and Automatic layout
  141. (defun org-freemind-style-map--automatic (element info)
  142. "Return a node style corresponding to relative outline level of ELEMENT.
  143. ELEMENT can be any of the following types - `org-data',
  144. `headline' or `section'. See `org-freemind-styles' for style
  145. mappings of different outline levels."
  146. (let ((style-name
  147. (case (org-element-type element)
  148. (headline
  149. (org-export-get-relative-level element info))
  150. (section
  151. (let ((parent (org-export-get-parent-headline element)))
  152. (if (not parent) 1
  153. (1+ (org-export-get-relative-level parent info)))))
  154. (t 0))))
  155. (or (assoc-default style-name org-freemind-styles)
  156. (assoc-default 'default org-freemind-styles)
  157. "<node></node>")))
  158. (defun org-freemind-style-map--default (element info)
  159. "Return the default style for all ELEMENTs.
  160. ELEMENT can be any of the following types - `org-data',
  161. `headline' or `section'. See `org-freemind-styles' for current
  162. value of default style."
  163. (or (assoc-default 'default org-freemind-styles)
  164. "<node></node>"))
  165. ;;;; Helpers :: Retrieve, apply Freemind styles
  166. (defun org-freemind--get-node-style (element info)
  167. "Return Freemind node style applicable for HEADLINE.
  168. ELEMENT is an Org element of type `org-data', `headline' or
  169. `section'. INFO is a plist holding contextual information."
  170. (unless (fboundp org-freemind-style-map-function)
  171. (setq org-freemind-style-map-function 'org-freemind-style-map--default))
  172. (let ((style (funcall org-freemind-style-map-function element info)))
  173. ;; Sanitize node style.
  174. ;; Loop through the attributes of node element and purge those
  175. ;; attributes that look suspicious. This is an extra bit of work
  176. ;; that allows one to copy verbatim node styles from an existing
  177. ;; Freemind Mindmap file without messing with the exported data.
  178. (let* ((data (org-freemind--parse-xml style))
  179. (attributes (cadr data))
  180. (ignored-attrs '(POSITION FOLDED TEXT CREATED ID
  181. MODIFIED)))
  182. (let (attr)
  183. (while (setq attr (pop ignored-attrs))
  184. (setq attributes (assq-delete-all attr attributes))))
  185. (when data (setcar (cdr data) attributes))
  186. (org-freemind--serialize data))))
  187. (defun org-freemind--build-stylized-node (style-1 style-2 &optional contents)
  188. "Build a Freemind node with style STYLE-1 + STYLE-2 and add CONTENTS to it.
  189. STYLE-1 and STYLE-2 are Freemind node styles as a string.
  190. STYLE-1 is the base node style and STYLE-2 is the overriding
  191. style that takes precedence over STYLE-1. CONTENTS is a string.
  192. Return value is a Freemind node with following properties:
  193. 1. The attributes of \"<node ...> </node>\" element is the union
  194. of corresponding attributes of STYLE-1 and STYLE-2. When
  195. STYLE-1 and STYLE-2 specify values for the same attribute
  196. name, choose the attribute value from STYLE-2.
  197. 2. The children of \"<node ...> </node>\" element is the union of
  198. top-level children of STYLE-1 and STYLE-2 with CONTENTS
  199. appended to it. When STYLE-1 and STYLE-2 share a child
  200. element of same type, the value chosen is that from STYLE-2.
  201. For example, merging with following parameters
  202. STYLE-1 =>
  203. <node COLOR=\"#00b439\" STYLE=\"Bubble\">
  204. <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
  205. <font NAME=\"SansSerif\" SIZE=\"16\"/>
  206. </node>
  207. STYLE-2 =>
  208. <node COLOR=\"#990000\" FOLDED=\"true\">
  209. <font NAME=\"SansSerif\" SIZE=\"14\"/>
  210. </node>
  211. CONTENTS =>
  212. <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
  213. will result in following node:
  214. RETURN =>
  215. <node STYLE=\"Bubble\" COLOR=\"#990000\" FOLDED=\"true\">
  216. <edge STYLE=\"bezier\" WIDTH=\"thin\"/>
  217. <font NAME=\"SansSerif\" SIZE=\"14\"/>
  218. <attribute NAME=\"ORGTAG\" VALUE=\"@home\"/>
  219. </node>."
  220. (let* ((data1 (org-freemind--parse-xml (or style-1 "")))
  221. (data2 (org-freemind--parse-xml (or style-2 "")))
  222. (attr1 (cadr data1))
  223. (attr2 (cadr data2))
  224. (merged-attr attr2)
  225. (children1 (cddr data1))
  226. (children2 (cddr data2))
  227. (merged-children children2))
  228. (let (attr)
  229. (while (setq attr (pop attr1))
  230. (unless (assq (car attr) merged-attr)
  231. (push attr merged-attr))))
  232. (let (child)
  233. (while (setq child (pop children1))
  234. (when (or (stringp child) (not (assq (car child) merged-children)))
  235. (push child merged-children))))
  236. (let ((merged-data (nconc (list 'node merged-attr) merged-children)))
  237. (org-freemind--serialize merged-data contents))))
  238. ;;;; Helpers :: Node contents
  239. (defun org-freemind--richcontent (type contents &optional css-style)
  240. (let* ((type (case type
  241. (note "NOTE")
  242. (node "NODE")
  243. (t "NODE")))
  244. (contents (org-trim contents)))
  245. (if (string= (org-trim contents) "") ""
  246. (format "\n<richcontent TYPE=\"%s\">%s\n</richcontent>"
  247. type
  248. (format "\n<html>\n<head>%s\n</head>\n%s\n</html>"
  249. (or css-style "")
  250. (format "<body>\n%s\n</body>" contents))))))
  251. (defun org-freemind--build-node-contents (element contents info)
  252. (let* ((title (case (org-element-type element)
  253. (headline
  254. (org-element-property :title element))
  255. (org-data
  256. (plist-get info :title))
  257. (t (error "Shouldn't come here."))))
  258. (element-contents (org-element-contents element))
  259. (section (assoc 'section element-contents))
  260. (section-contents
  261. (let* ((translations
  262. (nconc (list (cons 'section
  263. (lambda (section contents info)
  264. contents)))
  265. (plist-get info :translate-alist))))
  266. (org-export-data-with-translations section translations 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. (if async
  432. (org-export-async-start
  433. (lambda (f) (org-export-add-to-stack f 'freemind))
  434. (let ((org-export-coding-system 'utf-8))
  435. `(expand-file-name
  436. (org-export-to-file
  437. 'freemind ,file ,subtreep ,visible-only ,body-only ',ext-plist))))
  438. (let ((org-export-coding-system 'utf-8))
  439. (org-export-to-file
  440. 'freemind file subtreep visible-only body-only ext-plist)))))
  441. (provide 'ox-freemind)
  442. ;; Local variables:
  443. ;; generated-autoload-file: "org-loaddefs.el"
  444. ;; End:
  445. ;;; ox-freemind.el ends here