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