1 ;;; org-mw.el --- Mediawiki backend for org-export.el
3 ;; Copyright 2010-2011 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
8 ;; Author: Bastien <bzg AT altern DOT org>
9 ;; Maintainer: Bastien <bzg AT altern DOT org>
10 ;; Keywords: Mediawiki Org export
11 ;; Description: Mediawiki exporter for Org
12 ;; URL: [Not distributed yet]
14 ;; This file is not part of GNU Emacs.
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 3, or (at your option)
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program; if not, write to the Free Software
28 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 ;; org-mw.el lets you convert Org files to mediawiki files using
33 ;; the org-export.el experimental engine.
35 ;; Put this file into your load-path and the following into your ~/.emacs:
38 ;; You also need to fetch Org's git repository and add the EXPERIMENTAL/
39 ;; directory in your load path.
41 ;; Fetch Org's git repository:
43 ;; ~$ cd ~/install/git/
44 ;; ~$ git clone git://repo.or.cz/org-mode.git
46 ;; Put this in your .emacs.el:
48 ;; (add-to-list 'load-path "~/install/git/org-mode/EXPERIMENTAL/")
50 ;; Export Org files to mediawiki: M-x org-mw-export RET
54 ;; - handle radio links
55 ;; - support caption and attributes in tables
56 ;; - better handline of source code and examples
57 ;; - handle inline HTML
63 (defvar org-mw-emphasis-alist
64 '(("*" "'''%s'''" nil
)
68 ("=" "<tt>%s</tt>" nil
))
69 "The list of fontification expressions for mediawiki.")
71 (defvar org-mw-export-table-table-style
"")
72 (defvar org-mw-export-table-header-style
"")
73 (defvar org-mw-export-table-cell-style
"")
75 (defun org-mw-export ()
76 "Export the current buffer to Mediawiki."
78 (setq org-export-current-backend
'mw
)
79 (org-export-set-backend "mw")
80 ;; FIXME see the problem `org-mw-export-footnotes'
81 ;; (add-hook 'org-export-preprocess-final-hook 'org-mw-export-footnotes)
82 (add-hook 'org-export-preprocess-before-backend-specifics-hook
83 'org-mw-export-src-example
)
85 ;; (remove-hook 'org-export-preprocess-final-hook 'org-mw-export-footnotes)
86 (remove-hook 'org-export-preprocess-before-backend-specifics-hook
87 'org-mw-export-src-example
))
89 (defun org-mw-export-header ()
90 "Export the header part."
91 (let* ((p org-export-properties
)
92 (title (plist-get p
:title
))
93 (author (plist-get p
:author
))
94 (date (plist-get p
:date
)))
95 (insert (format "= %s by %s =\n\n" title author
))
96 (unless (plist-get p
:table-of-contents
)
97 (insert "__NOTOC__\n\n"))))
99 (defun org-mw-export-first-lines (first-lines)
100 "Export first lines."
101 (insert (org-export-render-content first-lines
) "\n")
102 (goto-char (point-max)))
104 (defun org-mw-export-heading (section-properties)
105 "Export mediawiki heading"
106 (let* ((p section-properties
)
107 (h (plist-get p
:heading
))
108 (s (make-string (1+ (plist-get p
:level
)) ?
=)))
109 (insert (format "%s %s %s\n" s h s
))))
111 (defun org-mw-export-quote-verse-center ()
112 "Export #+BEGIN_QUOTE/VERSE/CENTER environments."
114 (while (re-search-forward "^[ \t]*ORG-\\([A-Z]+\\)-\\(START\\|END\\).*$" nil t
)
115 (setq e
(if (equal (match-string 2) "END") "/" ""))
117 (cond ((equal (match-string 1) "BLOCKQUOTE") "blockquote>")
118 ((equal (match-string 1) "VERSE") "pre>")
119 ((equal (match-string 1) "CENTER") "center>")))
120 (replace-match (concat "<" e rpl
) t
))))
122 (defun org-mw-export-fonts ()
123 "Export fontification."
124 (while (re-search-forward org-emph-re nil t
)
125 (let* ((emph (assoc (match-string 3) org-mw-emphasis-alist
))
126 (beg (match-beginning 0))
127 (begs (match-string 1))
129 (ends (match-string 5))
130 (rpl (format (cadr emph
) (match-string 4))))
131 (delete-region beg end
)
132 (insert begs rpl ends
))))
134 (defun org-mw-export-links ()
135 "Replace Org links with DokiWiki links."
136 ;; FIXME: This function could be more clever, of course.
137 (while (re-search-forward org-bracket-link-analytic-regexp nil t
)
138 (cond ((and (equal (match-string 1) "file:")
140 (string-match (org-image-file-name-regexp) (match-string 3))))
142 (concat "[[Image:" (file-name-nondirectory (match-string 3)) "]]")))
145 (concat "[\\1\\3" (if (match-string 5) " \\5]" "]")))))))
147 ;; FIXME this function should test whether [1] is really a footnote.
148 ;; `org-footnote-normalize' should add properties to the normalized
149 ;; footnotes so that we can recognize them.
150 (defun org-mw-export-footnotes ()
152 (goto-char (point-min))
153 (let (refpos rpl begnote begfullnote endnote
)
154 (while (re-search-forward "\[[0-9]+\]" nil t
)
157 (goto-char (point-max))
158 (search-backward (concat (match-string 0) " ") nil t
)
159 (setq begfullnote
(match-beginning 0))
160 (setq begnote
(match-end 0))
161 (goto-char (match-end 0))
162 (re-search-forward "^\[[0-9]+\]\\|\\'" nil t
)
163 (setq endnote
(match-beginning 0))
164 (setq rpl
(replace-regexp-in-string
165 "\n" " " (buffer-substring endnote begnote
)))
166 (setq rpl
(replace-regexp-in-string "[ \t]+$" "" rpl
))
167 (delete-region begfullnote endnote
)))
168 (replace-match (concat "<ref>" rpl
"</ref>")))))
170 (defun org-mw-export-src-example ()
171 "Export #+BEGIN_EXAMPLE and #+BEGIN_SRC."
172 (goto-char (point-min))
174 (while (re-search-forward "^[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\).*\n" nil t
)
175 (setq env
(match-string 1))
178 (re-search-forward (concat "^[ \t]*#\\+END_" env
".*\n") nil t
)
180 (string-rectangle start
(point) ": ")
183 (defun org-mw-export-lists ()
184 "Export lists to mediawiki 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
)
194 :dtstart
"" :dtend
" "
197 (1+ depth
) (if (eq type
'unordered
) ?
* ?
#)) " ")
202 :cbon
"[X]" :cboff
"[ ]"
205 (defun org-mw-export-tables ()
206 "Convert tables in the current buffer to mediawiki tables."
207 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
208 (org-if-unprotected-at (1- (point))
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
)))
220 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
221 (org-split-string (org-trim elem
) "|")))
223 (insert (orgtbl-to-mw lines nil
))))))
225 (defun orgtbl-to-mw (table params
)
226 "Convert TABLE into a mediawiki table."
228 :tstart
(concat "{| class=\"wikitable\" "
229 org-mw-export-table-table-style
)
234 :fmt
(concat "\| " org-mw-export-table-cell-style
" | %s")
235 :hfmt
(concat "! scope=row " org-mw-export-table-header-style
" | %s")
238 (orgtbl-to-generic table
(org-combine-plists params2 params
))))
240 ;; Various empty function for org-export.el to work:
241 (defun org-mw-export-footer () "")
242 (defun org-mw-export-section-beginning (section-properties) "")
243 (defun org-mw-export-section-end (section-properties) "")
244 (defun org-export-mw-preprocess (parameters)
245 "Do extra work for Mediawiki export."