* lisp/ob-R.el: Added tikzDevice support.
[org-mode.git] / EXPERIMENTAL / org-mm.el
bloba7de091943f6fb1e562254e459d027d173127e4f
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
12 ;; This file is not part of GNU Emacs.
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.
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.
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.
28 ;; A portion of this code is based on org-mw.el by Bastien Guerry.
30 ;;; Commentary:
32 ;; org-mm.el lets you convert Org files to MoinMoin files using
33 ;; the org-export.el experimental engine.
35 ;; Put this file into your load-path and the following into your ~/.emacs:
36 ;; (require 'org-mm)
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
46 ;; Put this in your .emacs.el:
47 ;;
48 ;; (add-to-list 'load-path "~/install/git/org-mode/EXPERIMENTAL/")
50 ;; Export Org files to MoinMoin: M-x org-mm-export RET
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
59 ;;; Code:
61 (require 'org-export)
63 (defvar org-mm-emphasis-alist
64 '(("*" "'''%s'''" nil)
65 ("/" "''%s''" nil)
66 ("_" "__%s__" nil)
67 ("+" "--%s--" nil)
68 ("=" "`%s`" nil))
69 "The list of fontification expressions for MoinMoin.")
71 (defvar org-mm-export-table-table-style "")
72 (defvar org-mm-export-table-header-style "")
73 (defvar org-mm-export-table-cell-style "")
75 (defun org-mm-export ()
76 "Export the current buffer to MoinMoin."
77 (interactive)
78 (setq org-export-current-backend 'mm)
79 (org-export-set-backend "mm")
80 ;; FIXME see the problem `org-mm-export-footnotes'
81 ;; (add-hook 'org-export-preprocess-final-hook 'org-mm-export-footnotes)
82 (add-hook 'org-export-preprocess-before-backend-specifics-hook
83 'org-mm-export-src-example)
84 (org-export-render)
85 ;; (remove-hook 'org-export-preprocess-final-hook 'org-mm-export-footnotes)
86 (remove-hook 'org-export-preprocess-before-backend-specifics-hook
87 'org-mm-export-src-example))
89 (defun org-mm-export-header ()
90 "Export the header part."
91 (let* ((p (org-combine-plists (org-infile-export-plist)
92 org-export-properties))
93 (title (plist-get p :title))
94 (author (plist-get p :author))
95 (date (plist-get p :date))
96 (level (plist-get p :headline-levels)))
97 (insert (format "= %s by %s =\n\n" title author))
98 (if (plist-get p :table-of-contents)
99 (insert (format "<<TableOfContents(%s)>>\n" level)))))
101 (defun org-mm-export-first-lines (first-lines)
102 "Export first lines."
103 (insert (org-export-render-content first-lines) "\n")
104 (goto-char (point-max)))
106 (defun org-mm-export-heading (section-properties)
107 "Export MoinMoin heading"
108 (let* ((p section-properties)
109 (h (plist-get p :heading))
110 (s (make-string (1+ (plist-get p :level)) ?=)))
111 (insert (format "%s %s %s\n" s h s))))
113 (defun org-mm-export-quote-verse-center ()
114 "Export #+BEGIN_QUOTE/VERSE/CENTER environments."
115 (let (rpl e)
116 (while (re-search-forward "^[ \t]*ORG-\\([A-Z]+\\)-\\(START\\|END\\).*$" nil t)
117 (setq e (if (equal (match-string 2) "END") "/" ""))
118 (setq rpl
119 (cond ((equal (match-string 1) "BLOCKQUOTE") "blockquote>")
120 ((equal (match-string 1) "VERSE") "pre>")
121 ((equal (match-string 1) "CENTER") "center>")))
122 (replace-match (concat "<" e rpl) t))))
124 (defun org-mm-export-fonts ()
125 "Export fontification."
126 (while (re-search-forward org-emph-re nil t)
127 (let* ((emph (assoc (match-string 3) org-mm-emphasis-alist))
128 (beg (match-beginning 0))
129 (begs (match-string 1))
130 (end (match-end 0))
131 (ends (match-string 5))
132 (rpl (format (cadr emph) (match-string 4))))
133 (delete-region beg end)
134 (insert begs rpl ends))))
136 (defun org-mm-export-links ()
137 "Replace Org links with MoinMoin links."
138 ;; FIXME: This function could be more clever, of course.
139 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
140 (cond ((and (equal (match-string 1) "file:")
141 (save-match-data
142 (string-match (org-image-file-name-regexp) (match-string 3))))
143 (replace-match
144 (concat "{{" (file-name-nondirectory (match-string 3)) "}}")))
146 (replace-match
147 (concat "[[\\1\\3|" (if (match-string 5) "\\5]]" "]]")))))))
149 ;; FIXME this function should test whether [1] is really a footnote.
150 ;; `org-footnote-normalize' should add properties to the normalized
151 ;; footnotes so that we can recognize them.
152 (defun org-mm-export-footnotes ()
153 "Export footnotes."
154 (goto-char (point-min))
155 (let (refpos rpl begnote begfullnote endnote)
156 (while (re-search-forward "\[[0-9]+\]" nil t)
157 (save-excursion
158 (save-match-data
159 (goto-char (point-max))
160 (search-backward (concat (match-string 0) " ") nil t)
161 (setq begfullnote (match-beginning 0))
162 (setq begnote (match-end 0))
163 (goto-char (match-end 0))
164 (re-search-forward "^\[[0-9]+\]\\|\\'" nil t)
165 (setq endnote (match-beginning 0))
166 (setq rpl (replace-regexp-in-string
167 "\n" " " (buffer-substring endnote begnote)))
168 (setq rpl (replace-regexp-in-string "[ \t]+$" "" rpl))
169 (delete-region begfullnote endnote)))
170 (replace-match (concat "<ref>" rpl "</ref>")))))
172 (defun org-mm-export-src-example ()
173 "Export #+BEGIN_EXAMPLE and #+BEGIN_SRC."
174 (goto-char (point-min))
175 (let (start env)
176 (while (re-search-forward "^[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\).*\n" nil t)
177 (setq env (match-string 1))
178 (replace-match "{{{\n")
179 (setq start (point))
180 (re-search-forward (concat "^[ \t]*#\\+END_" env ".*\n") nil t)
181 (replace-match "}}}\n"))))
183 (defun org-mm-export-lists ()
184 "Export lists to MoinMoin syntax."
185 (while (re-search-forward (org-item-beginning-re) nil t)
186 (move-beginning-of-line 1)
187 (insert (org-list-to-generic
188 (org-list-parse-list t)
189 (org-combine-plists
190 '(:splice nil
191 :ostart "" :oend ""
192 :ustart "" :uend ""
193 :dstart "" :dend ""
194 :dtstart "" :dtend " "
195 :istart (concat (make-string (* 2 (1+ depth)) ? )
196 (if (eq type 'unordered)
197 "* " "# "))
198 :iend "\n"
199 :icount nil
200 :csep "\n"
201 :cbon "[X]" :cboff "[ ]"
202 :cbtrans "[-]"))))))
205 (defun org-mm-export-tables ()
206 "Convert tables in the current buffer to MoinMoin tables."
207 (while (re-search-forward "^\\([ \t]*\\)|" nil t)
208 (org-if-unprotected-at (1- (point))
209 (org-table-align)
210 (let* ((beg (org-table-begin))
211 (end (org-table-end))
212 (raw-table (buffer-substring beg end)) lines)
213 (setq lines (org-split-string raw-table "\n"))
214 (apply 'delete-region (list beg end))
215 (when org-export-table-remove-special-lines
216 (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
217 (setq lines
218 (mapcar
219 (lambda(elem)
220 (or (and (string-match "[ \t]*|-+" elem) 'hline)
221 (org-split-string (org-trim elem) "|")))
222 lines))
223 (insert (orgtbl-to-mm lines nil))))))
225 (defun orgtbl-to-mm (table params)
226 "Convert TABLE into a MoinMoin table."
227 (let ((params2 (list
228 :tstart (concat ""
229 org-mm-export-table-table-style)
230 :tend "\n"
231 :lstart "||"
232 :lend "||"
233 :sep "||"
234 :fmt (concat org-mm-export-table-cell-style " %s ")
235 :hfmt (concat org-mm-export-table-cell-style "''' %s '''")
236 :hlsep "||"
238 (orgtbl-to-generic table (org-combine-plists params2 params))))
240 ;; Various empty function for org-export.el to work:
241 (defun org-mm-export-footer () "")
242 (defun org-mm-export-section-beginning (section-properties) "")
243 (defun org-mm-export-section-end (section-properties) "")
244 (defun org-export-mm-preprocess (parameters)
245 "Do extra work for MoinMoin export."
246 nil)
248 (provide 'org-mm)