org-mediawiki.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ;;; org-mediawiki.el --- mediawiki backend for org-export.el
  2. ;;
  3. ;; Copyright 2010 Bastien Guerry
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-mediawiki.el
  7. ;; Version: 0.3a
  8. ;; Author: Bastien <bzg AT altern DOT org>
  9. ;; Maintainer: Bastien <bzg AT altern DOT org>
  10. ;; Keywords:
  11. ;; Description:
  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-mediawiki.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-mediawiki)
  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. (org-export-set-backend "mw")
  73. ;; FIXME see the problem `org-mw-export-footnotes'
  74. ;; (add-hook 'org-export-preprocess-final-hook 'org-mw-export-footnotes)
  75. (add-hook 'org-export-preprocess-before-backend-specifics-hook
  76. 'org-mw-export-src-example)
  77. (org-export-render)
  78. (remove-hook 'org-export-preprocess-final-hook 'org-mw-export-footnotes)
  79. (remove-hook 'org-export-preprocess-before-backend-specifics-hook
  80. 'org-mw-export-src-example))
  81. (defun org-mw-export-header ()
  82. "Export the header part."
  83. (let* ((p org-export-properties)
  84. (title (plist-get p :title))
  85. (author (plist-get p :author))
  86. (date (plist-get p :date)))
  87. (insert (format "= %s by %s =\n\n" title author))
  88. (unless (plist-get p :table-of-contents)
  89. (insert "__NOTOC__\n\n"))))
  90. (defun org-mw-export-first-lines (first-lines)
  91. "Export first lines."
  92. (insert (org-export-render-content first-lines) "\n")
  93. (goto-char (point-max)))
  94. (defun org-mw-export-heading (section-properties)
  95. "Export mediawiki heading"
  96. (let* ((p section-properties)
  97. (h (plist-get p :heading))
  98. (s (make-string (1+ (plist-get p :level)) ?=)))
  99. (insert (format "%s %s %s\n" s h s))))
  100. (defun org-mw-export-quote-verse-center ()
  101. "Export #+BEGIN_QUOTE/VERSE/CENTER environments."
  102. (let (rpl e)
  103. (while (re-search-forward "^[ \t]*ORG-\\([A-Z]+\\)-\\(START\\|END\\).*$" nil t)
  104. (setq e (if (equal (match-string 2) "END") "/" ""))
  105. (setq rpl
  106. (cond ((equal (match-string 1) "BLOCKQUOTE") "blockquote>")
  107. ((equal (match-string 1) "VERSE") "pre>")
  108. ((equal (match-string 1) "CENTER") "center>")))
  109. (replace-match (concat "<" e rpl) t))))
  110. (defun org-mw-export-fonts ()
  111. "Export fontification."
  112. (while (re-search-forward org-emph-re nil t)
  113. (let* ((emph (assoc (match-string 3) org-mw-emphasis-alist))
  114. (beg (match-beginning 0))
  115. (begs (match-string 1))
  116. (end (match-end 0))
  117. (ends (match-string 5))
  118. (rpl (format (cadr emph) (match-string 4))))
  119. (delete-region beg end)
  120. (insert begs rpl ends))))
  121. (defun org-mw-export-links ()
  122. "Replace Org links with DokiWiki links."
  123. ;; FIXME: This function could be more clever, of course.
  124. (while (re-search-forward org-bracket-link-analytic-regexp nil t)
  125. (cond ((and (equal (match-string 1) "file:")
  126. (save-match-data
  127. (string-match (org-image-file-name-regexp) (match-string 3))))
  128. (replace-match
  129. (concat "[[Image:" (file-name-nondirectory (match-string 3)) "]]")))
  130. (t
  131. (replace-match
  132. (concat "[\\1\\3" (if (match-string 5) " \\5]" "]")))))))
  133. ;; FIXME this function should test whether [1] is really a footnote.
  134. ;; `org-footnote-normalize' should add properties to the normalized
  135. ;; footnotes so that we can recognize them.
  136. (defun org-mw-export-footnotes ()
  137. "Export footnotes."
  138. (goto-char (point-min))
  139. (let (refpos rpl begnote begfullnote endnote)
  140. (while (re-search-forward "\[[0-9]+\]" nil t)
  141. (save-excursion
  142. (save-match-data
  143. (goto-char (point-max))
  144. (search-backward (concat (match-string 0) " ") nil t)
  145. (setq begfullnote (match-beginning 0))
  146. (setq begnote (match-end 0))
  147. (goto-char (match-end 0))
  148. (re-search-forward "^\[[0-9]+\]\\|\\'" nil t)
  149. (setq endnote (match-beginning 0))
  150. (setq rpl (replace-regexp-in-string
  151. "\n" " " (buffer-substring endnote begnote)))
  152. (setq rpl (replace-regexp-in-string "[ \t]+$" "" rpl))
  153. (delete-region begfullnote endnote)))
  154. (replace-match (concat "<ref>" rpl "</ref>")))))
  155. (defun org-mw-export-src-example ()
  156. "Export #+BEGIN_EXAMPLE and #+BEGIN_SRC."
  157. (goto-char (point-min))
  158. (let (start env)
  159. (while (re-search-forward "^[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\).*\n" nil t)
  160. (setq env (match-string 1))
  161. (replace-match "")
  162. (setq start (point))
  163. (re-search-forward (concat "^[ \t]*#\\+END_" env ".*\n") nil t)
  164. (replace-match "")
  165. (string-rectangle start (point) ": ")
  166. (delete-char 1))))
  167. (defun org-mw-export-lists ()
  168. "Export lists"
  169. (while (re-search-forward org-item-beginning-re nil t)
  170. (move-beginning-of-line 1)
  171. (org-list-to-mw (org-list-parse-list t))))
  172. (defun org-list-to-mw (list &optional level leveluptype)
  173. "Convert LIST into a mediawiki list.
  174. LIST is a list returned by `org-list-parse-list'. A second
  175. optional LEVEL argument defines the level at which the parsing
  176. starts. An optional third argument LEVELUPTYPE tells what type
  177. of list we are in at LEVEL."
  178. (let ((lvl (or level 1))
  179. (ltype (cond ((eq (car list) 'unordered) ?*)
  180. ((eq (car list) 'ordered) ?#)
  181. ((eq (car list) 'descriptive) ?\;)))
  182. (luptype leveluptype))
  183. (dolist (item (cdr list))
  184. (if (stringp item)
  185. (progn
  186. (setq item (replace-regexp-in-string "\n[ \t]*" " " item))
  187. (when (eq ltype ?\;)
  188. (setq item (replace-regexp-in-string "::" ":" item)))
  189. (insert (if luptype (make-string (1- lvl) luptype) "")
  190. (char-to-string ltype) " " item "\n"))
  191. (org-list-to-mw item (1+ lvl) ltype)))))
  192. (defun org-mw-export-tables ()
  193. "Convert tables in the current buffer to mediawiki tables."
  194. (while (re-search-forward "^\\([ \t]*\\)|" nil t)
  195. (org-if-unprotected-at (1- (point))
  196. (org-table-align)
  197. (let* ((beg (org-table-begin))
  198. (end (org-table-end))
  199. (raw-table (buffer-substring beg end)) lines)
  200. (setq lines (org-split-string raw-table "\n"))
  201. (apply 'delete-region (list beg end))
  202. (when org-export-table-remove-special-lines
  203. (setq lines (org-table-clean-before-export lines 'maybe-quoted)))
  204. (setq lines
  205. (mapcar
  206. (lambda(elem)
  207. (or (and (string-match "[ \t]*|-+" elem) 'hline)
  208. (org-split-string (org-trim elem) "|")))
  209. lines))
  210. (insert (orgtbl-to-mw lines nil))))))
  211. (defun orgtbl-to-mw (table params)
  212. "Convert TABLE into a mediawiki table."
  213. (let ((params2 (list
  214. :tstart (concat "{| class=\"wikitable\" "
  215. org-mw-export-table-table-style)
  216. :tend "|}\n"
  217. :lstart "|-\n"
  218. :lend ""
  219. :sep "\n"
  220. :fmt (concat "\| " org-mw-export-table-cell-style " | %s")
  221. :hfmt (concat "! scope=row " org-mw-export-table-header-style " | %s")
  222. :hlsep "\n"
  223. )))
  224. (orgtbl-to-generic table (org-combine-plists params2 params))))
  225. ;; Various empty function for org-export.el to work:
  226. (defun org-mw-export-footer () "")
  227. (defun org-mw-export-section-beginning (section-properties) "")
  228. (defun org-mw-export-section-end (section-properties) "")