1 ;;; org-mediawiki.el --- mediawiki backend for org-export.el
3 ;; Copyright 2010 Bastien Guerry
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-mediawiki.el
8 ;; Author: Bastien <bzg AT altern DOT org>
9 ;; Maintainer: Bastien <bzg AT altern DOT org>
12 ;; URL: [Not distributed yet]
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)
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.
30 ;; org-mediawiki.el lets you convert Org files to mediawiki files using
31 ;; the org-export.el experimental engine.
33 ;; Put this file into your load-path and the following into your ~/.emacs:
34 ;; (require 'org-mediawiki)
36 ;; You also need to fetch Org's git repository and add the EXPERIMENTAL/
37 ;; directory in your load path.
39 ;; Fetch Org's git repository:
41 ;; ~$ cd ~/install/git/
42 ;; ~$ git clone git://repo.or.cz/org-mode.git
44 ;; Put this in your .emacs.el:
46 ;; (add-to-list 'load-path "~/install/git/org-mode/EXPERIMENTAL/")
48 ;; Export Org files to mediawiki: M-x org-mw-export RET
52 ;; - handle radio links
53 ;; - support caption and attributes in tables
54 ;; - better handline of source code and examples
55 ;; - handle inline HTML
61 (defvar org-mw-emphasis-alist
62 '(("*" "'''%s'''" nil
)
66 ("=" "<tt>%s</tt>" nil
))
67 "The list of fontification expressions for mediawiki.")
69 (defvar org-mw-export-table-table-style
"")
70 (defvar org-mw-export-table-header-style
"")
71 (defvar org-mw-export-table-cell-style
"")
73 (defun org-mw-export ()
74 "Export the current buffer to Mediawiki."
76 (org-export-set-backend "mw")
77 ;; FIXME see the problem `org-mw-export-footnotes'
78 ;; (add-hook 'org-export-preprocess-final-hook 'org-mw-export-footnotes)
79 (add-hook 'org-export-preprocess-before-backend-specifics-hook
80 'org-mw-export-src-example
)
82 (remove-hook 'org-export-preprocess-final-hook
'org-mw-export-footnotes
)
83 (remove-hook 'org-export-preprocess-before-backend-specifics-hook
84 'org-mw-export-src-example
))
86 (defun org-mw-export-header ()
87 "Export the header part."
88 (let* ((p org-export-properties
)
89 (title (plist-get p
:title
))
90 (author (plist-get p
:author
))
91 (date (plist-get p
:date
)))
92 (insert (format "= %s by %s =\n\n" title author
))
93 (unless (plist-get p
:table-of-contents
)
94 (insert "__NOTOC__\n\n"))))
96 (defun org-mw-export-first-lines (first-lines)
98 (insert (org-export-render-content first-lines
) "\n")
99 (goto-char (point-max)))
101 (defun org-mw-export-heading (section-properties)
102 "Export mediawiki heading"
103 (let* ((p section-properties
)
104 (h (plist-get p
:heading
))
105 (s (make-string (1+ (plist-get p
:level
)) ?
=)))
106 (insert (format "%s %s %s\n" s h s
))))
108 (defun org-mw-export-quote-verse-center ()
109 "Export #+BEGIN_QUOTE/VERSE/CENTER environments."
111 (while (re-search-forward "^[ \t]*ORG-\\([A-Z]+\\)-\\(START\\|END\\).*$" nil t
)
112 (setq e
(if (equal (match-string 2) "END") "/" ""))
114 (cond ((equal (match-string 1) "BLOCKQUOTE") "blockquote>")
115 ((equal (match-string 1) "VERSE") "pre>")
116 ((equal (match-string 1) "CENTER") "center>")))
117 (replace-match (concat "<" e rpl
) t
))))
119 (defun org-mw-export-fonts ()
120 "Export fontification."
121 (while (re-search-forward org-emph-re nil t
)
122 (let* ((emph (assoc (match-string 3) org-mw-emphasis-alist
))
123 (beg (match-beginning 0))
124 (begs (match-string 1))
126 (ends (match-string 5))
127 (rpl (format (cadr emph
) (match-string 4))))
128 (delete-region beg end
)
129 (insert begs rpl ends
))))
131 (defun org-mw-export-links ()
132 "Replace Org links with DokiWiki links."
133 ;; FIXME: This function could be more clever, of course.
134 (while (re-search-forward org-bracket-link-analytic-regexp nil t
)
135 (cond ((and (equal (match-string 1) "file:")
137 (string-match (org-image-file-name-regexp) (match-string 3))))
139 (concat "[[Image:" (file-name-nondirectory (match-string 3)) "]]")))
142 (concat "[\\1\\3" (if (match-string 5) " \\5]" "]")))))))
144 ;; FIXME this function should test whether [1] is really a footnote.
145 ;; `org-footnote-normalize' should add properties to the normalized
146 ;; footnotes so that we can recognize them.
147 (defun org-mw-export-footnotes ()
149 (goto-char (point-min))
150 (let (refpos rpl begnote begfullnote endnote
)
151 (while (re-search-forward "\[[0-9]+\]" nil t
)
154 (goto-char (point-max))
155 (search-backward (concat (match-string 0) " ") nil t
)
156 (setq begfullnote
(match-beginning 0))
157 (setq begnote
(match-end 0))
158 (goto-char (match-end 0))
159 (re-search-forward "^\[[0-9]+\]\\|\\'" nil t
)
160 (setq endnote
(match-beginning 0))
161 (setq rpl
(replace-regexp-in-string
162 "\n" " " (buffer-substring endnote begnote
)))
163 (setq rpl
(replace-regexp-in-string "[ \t]+$" "" rpl
))
164 (delete-region begfullnote endnote
)))
165 (replace-match (concat "<ref>" rpl
"</ref>")))))
167 (defun org-mw-export-src-example ()
168 "Export #+BEGIN_EXAMPLE and #+BEGIN_SRC."
169 (goto-char (point-min))
171 (while (re-search-forward "^[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\).*\n" nil t
)
172 (setq env
(match-string 1))
175 (re-search-forward (concat "^[ \t]*#\\+END_" env
".*\n") nil t
)
177 (string-rectangle start
(point) ": ")
180 (defun org-mw-export-lists ()
182 (while (re-search-forward org-item-beginning-re nil t
)
183 (move-beginning-of-line 1)
184 (org-list-to-mw (org-list-parse-list t
))))
186 (defun org-list-to-mw (list &optional level leveluptype
)
187 "Convert LIST into a mediawiki list.
188 LIST is a list returned by `org-list-parse-list'. A second
189 optional LEVEL argument defines the level at which the parsing
190 starts. An optional third argument LEVELUPTYPE tells what type
191 of list we are in at LEVEL."
192 (let ((lvl (or level
1))
193 (ltype (cond ((eq (car list
) 'unordered
) ?
*)
194 ((eq (car list
) 'ordered
) ?
#)
195 ((eq (car list
) 'descriptive
) ?\
;)))
196 (luptype leveluptype
))
197 (dolist (item (cdr list
))
200 (setq item
(replace-regexp-in-string "\n[ \t]*" " " item
))
202 (setq item
(replace-regexp-in-string "::" ":" item
)))
203 (insert (if luptype
(make-string (1- lvl
) luptype
) "")
204 (char-to-string ltype
) " " item
"\n"))
205 (org-list-to-mw item
(1+ lvl
) ltype
)))))
207 (defun org-mw-export-tables ()
208 "Convert tables in the current buffer to mediawiki tables."
209 (while (re-search-forward "^\\([ \t]*\\)|" nil t
)
210 (org-if-unprotected-at (1- (point))
212 (let* ((beg (org-table-begin))
213 (end (org-table-end))
214 (raw-table (buffer-substring beg end
)) lines
)
215 (setq lines
(org-split-string raw-table
"\n"))
216 (apply 'delete-region
(list beg end
))
217 (when org-export-table-remove-special-lines
218 (setq lines
(org-table-clean-before-export lines
'maybe-quoted
)))
222 (or (and (string-match "[ \t]*|-+" elem
) 'hline
)
223 (org-split-string (org-trim elem
) "|")))
225 (insert (orgtbl-to-mw lines nil
))))))
227 (defun orgtbl-to-mw (table params
)
228 "Convert TABLE into a mediawiki table."
230 :tstart
(concat "{| class=\"wikitable\" "
231 org-mw-export-table-table-style
)
236 :fmt
(concat "\| " org-mw-export-table-cell-style
" | %s")
237 :hfmt
(concat "! scope=row " org-mw-export-table-header-style
" | %s")
240 (orgtbl-to-generic table
(org-combine-plists params2 params
))))
242 ;; Various empty function for org-export.el to work:
243 (defun org-mw-export-footer () "")
244 (defun org-mw-export-section-beginning (section-properties) "")
245 (defun org-mw-export-section-end (section-properties) "")