org-mm.el 8.3 KB

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