org-mm.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ;;; org-mm.el --- MoinMoin backend for org-export.el
  2. ;;
  3. ;; Copyright 2010, 2011 Puneeth Chaganti
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-mm.el
  7. ;; Version: 0.2
  8. ;; Author: Puneeth Chaganti <punchagan [at] gmail [dot] com>
  9. ;; Keywords: MoinMoin Org export
  10. ;; Description: MoinMoin exporter for Org
  11. ;;
  12. ;; This program is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;;
  17. ;; This program is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;;
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with this program; if not, write to the Free Software
  24. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. ;;
  26. ;; A portion of this code is based on org-mw.el by Bastien Guerry.
  27. ;;
  28. ;;; Commentary:
  29. ;;
  30. ;; org-mm.el lets you convert Org files to MoinMoin files using
  31. ;; the org-export.el experimental engine.
  32. ;;
  33. ;; Put this file into your load-path and the following into your ~/.emacs:
  34. ;; (require 'org-mm)
  35. ;;
  36. ;; You also need to fetch Org's git repository and add the EXPERIMENTAL/
  37. ;; directory in your load path.
  38. ;;
  39. ;; Fetch Org's git repository:
  40. ;;
  41. ;; ~$ cd ~/install/git/
  42. ;; ~$ git clone git://repo.or.cz/org-mode.git
  43. ;;
  44. ;; Put this in your .emacs.el:
  45. ;;
  46. ;; (add-to-list 'load-path "~/install/git/org-mode/EXPERIMENTAL/")
  47. ;;
  48. ;; Export Org files to MoinMoin: M-x org-mm-export RET
  49. ;;
  50. ;;; Todo:
  51. ;;
  52. ;; - handle radio links
  53. ;; - support caption and attributes in tables
  54. ;; - better handline of source code and examples
  55. ;; - handle inline HTML
  56. ;;
  57. ;;; Code:
  58. (require 'org-export)
  59. (defvar org-mm-emphasis-alist
  60. '(("*" "'''%s'''" nil)
  61. ("/" "''%s''" nil)
  62. ("_" "__%s__" nil)
  63. ("+" "--%s--" nil)
  64. ("=" "`%s`" nil))
  65. "The list of fontification expressions for MoinMoin.")
  66. (defvar org-mm-export-table-table-style "")
  67. (defvar org-mm-export-table-header-style "")
  68. (defvar org-mm-export-table-cell-style "")
  69. (defun org-mm-export ()
  70. "Export the current buffer to MoinMoin."
  71. (interactive)
  72. (setq org-export-current-backend 'mm)
  73. (org-export-set-backend "mm")
  74. ;; FIXME see the problem `org-mm-export-footnotes'
  75. ;; (add-hook 'org-export-preprocess-final-hook 'org-mm-export-footnotes)
  76. (add-hook 'org-export-preprocess-before-backend-specifics-hook
  77. 'org-mm-export-src-example)
  78. (org-export-render)
  79. ;; (remove-hook 'org-export-preprocess-final-hook 'org-mm-export-footnotes)
  80. (remove-hook 'org-export-preprocess-before-backend-specifics-hook
  81. 'org-mm-export-src-example))
  82. (defun org-mm-export-header ()
  83. "Export the header part."
  84. (let* ((p (org-combine-plists (org-infile-export-plist)
  85. org-export-properties))
  86. (title (plist-get p :title))
  87. (author (plist-get p :author))
  88. (date (plist-get p :date))
  89. (level (plist-get p :headline-levels)))
  90. (insert (format "= %s by %s =\n\n" title author))
  91. (if (plist-get p :table-of-contents)
  92. (insert (format "<<TableOfContents(%s)>>\n" level)))))
  93. (defun org-mm-export-first-lines (first-lines)
  94. "Export first lines."
  95. (insert (org-export-render-content first-lines) "\n")
  96. (goto-char (point-max)))
  97. (defun org-mm-export-heading (section-properties)
  98. "Export MoinMoin heading"
  99. (let* ((p section-properties)
  100. (h (plist-get p :heading))
  101. (s (make-string (1+ (plist-get p :level)) ?=)))
  102. (insert (format "%s %s %s\n" s h s))))
  103. (defun org-mm-export-quote-verse-center ()
  104. "Export #+BEGIN_QUOTE/VERSE/CENTER environments."
  105. (let (rpl e)
  106. (while (re-search-forward "^[ \t]*ORG-\\([A-Z]+\\)-\\(START\\|END\\).*$" nil t)
  107. (setq e (if (equal (match-string 2) "END") "/" ""))
  108. (setq rpl
  109. (cond ((equal (match-string 1) "BLOCKQUOTE") "blockquote>")
  110. ((equal (match-string 1) "VERSE") "pre>")
  111. ((equal (match-string 1) "CENTER") "center>")))
  112. (replace-match (concat "<" e rpl) t))))
  113. (defun org-mm-export-fonts ()
  114. "Export fontification."
  115. (while (re-search-forward org-emph-re nil t)
  116. (let* ((emph (assoc (match-string 3) org-mm-emphasis-alist))
  117. (beg (match-beginning 0))
  118. (begs (match-string 1))
  119. (end (match-end 0))
  120. (ends (match-string 5))
  121. (rpl (format (cadr emph) (match-string 4))))
  122. (delete-region beg end)
  123. (insert begs rpl ends))))
  124. (defun org-mm-export-links ()
  125. "Replace Org links with MoinMoin links."
  126. ;; FIXME: This function could be more clever, of course.
  127. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  128. (cond ((and (equal (match-string 1) "file:")
  129. (save-match-data
  130. (string-match (org-image-file-name-regexp) (match-string 3))))
  131. (replace-match
  132. (concat "{{" (file-name-nondirectory (match-string 3)) "}}")))
  133. (t
  134. (replace-match
  135. (concat "[[\\1\\3|" (if (match-string 5) "\\5]]" "]]")))))))
  136. ;; FIXME this function should test whether [1] is really a footnote.
  137. ;; `org-footnote-normalize' should add properties to the normalized
  138. ;; footnotes so that we can recognize them.
  139. (defun org-mm-export-footnotes ()
  140. "Export footnotes."
  141. (goto-char (point-min))
  142. (let (refpos rpl begnote begfullnote endnote)
  143. (while (re-search-forward "\[[0-9]+\]" nil t)
  144. (save-excursion
  145. (save-match-data
  146. (goto-char (point-max))
  147. (search-backward (concat (match-string 0) " ") nil t)
  148. (setq begfullnote (match-beginning 0))
  149. (setq begnote (match-end 0))
  150. (goto-char (match-end 0))
  151. (re-search-forward "^\[[0-9]+\]\\|\\'" nil t)
  152. (setq endnote (match-beginning 0))
  153. (setq rpl (replace-regexp-in-string
  154. "\n" " " (buffer-substring endnote begnote)))
  155. (setq rpl (replace-regexp-in-string "[ \t]+$" "" rpl))
  156. (delete-region begfullnote endnote)))
  157. (replace-match (concat "<ref>" rpl "</ref>")))))
  158. (defun org-mm-export-src-example ()
  159. "Export #+BEGIN_EXAMPLE and #+BEGIN_SRC."
  160. (goto-char (point-min))
  161. (let (start env)
  162. (while (re-search-forward "^[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\).*\n" nil t)
  163. (setq env (match-string 1))
  164. (replace-match "{{{\n")
  165. (setq start (point))
  166. (re-search-forward (concat "^[ \t]*#\\+END_" env ".*\n") nil t)
  167. (replace-match "}}}\n"))))
  168. (defun org-mm-export-lists ()
  169. "Export lists to MoinMoin syntax."
  170. (while (re-search-forward (org-item-beginning-re) nil t)
  171. (move-beginning-of-line 1)
  172. (insert (org-list-to-generic
  173. (org-list-parse-list t)
  174. (org-combine-plists
  175. '(:splice nil
  176. :ostart "" :oend ""
  177. :ustart "" :uend ""
  178. :dstart "" :dend ""
  179. :dtstart "" :dtend " "
  180. :istart (concat (make-string (* 2 (1+ depth)) ? )
  181. (if (eq type 'unordered)
  182. "* " "# "))
  183. :iend "\n"
  184. :icount nil
  185. :csep "\n"
  186. :cbon "[X]" :cboff "[ ]"
  187. :cbtrans "[-]"))))))
  188. (defun org-mm-export-tables ()
  189. "Convert tables in the current buffer to MoinMoin tables."
  190. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  191. (org-if-unprotected-at (1- (point))
  192. (org-table-align)
  193. (let* ((beg (org-table-begin))
  194. (end (org-table-end))
  195. (raw-table (buffer-substring beg end)) lines)
  196. (setq lines (org-split-string raw-table "\n"))
  197. (apply 'delete-region (list beg end))
  198. (when org-export-table-remove-special-lines
  199. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  200. (setq lines
  201. (mapcar
  202. (lambda(elem)
  203. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  204. (org-split-string (org-trim elem) "|")))
  205. lines))
  206. (insert (orgtbl-to-mm lines nil))))))
  207. (defun orgtbl-to-mm (table params)
  208. "Convert TABLE into a MoinMoin table."
  209. (let ((params2 (list
  210. :tstart (concat ""
  211. org-mm-export-table-table-style)
  212. :tend "\n"
  213. :lstart "||"
  214. :lend "||"
  215. :sep "||"
  216. :fmt (concat org-mm-export-table-cell-style " %s ")
  217. :hfmt (concat org-mm-export-table-cell-style "''' %s '''")
  218. :hlsep "||"
  219. )))
  220. (orgtbl-to-generic table (org-combine-plists params2 params))))
  221. ;; Various empty function for org-export.el to work:
  222. (defun org-mm-export-footer () "")
  223. (defun org-mm-export-section-beginning (section-properties) "")
  224. (defun org-mm-export-section-end (section-properties) "")
  225. (defun org-export-mm-preprocess (parameters)
  226. "Do extra work for MoinMoin export."
  227. nil)
  228. (provide 'org-mm)