ox-freemind.el 19 KB

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