org-mw.el 8.2 KB

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