ox-confluence.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ;;; ox-confluence --- Confluence Wiki Back-End for Org Export Engine
  2. ;; Copyright (C) 2012, 2014 Sébastien Delafond
  3. ;; Author: Sébastien Delafond <sdelafond@gmail.com>
  4. ;; Keywords: outlines, confluence, wiki
  5. ;; This file is not part of GNU Emacs.
  6. ;; This program is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; ox-confluence.el lets you convert Org files to confluence files
  19. ;; using the ox.el export engine.
  20. ;;
  21. ;; Put this file into your load-path and the following into your ~/.emacs:
  22. ;; (require 'ox-confluence)
  23. ;;
  24. ;; Export Org files to confluence:
  25. ;; M-x org-confluence-export-as-confluence RET
  26. ;;
  27. ;;; Code:
  28. (require 'ox)
  29. (require 'ox-ascii)
  30. ;; Define the backend itself
  31. (org-export-define-derived-backend 'confluence 'ascii
  32. :translate-alist '((bold . org-confluence-bold)
  33. (code . org-confluence-code)
  34. (example-block . org-confluence-example-block)
  35. (fixed-width . org-confluence-fixed-width)
  36. (footnote-definition . org-confluence-empty)
  37. (footnote-reference . org-confluence-empty)
  38. (headline . org-confluence-headline)
  39. (italic . org-confluence-italic)
  40. (item . org-confluence-item)
  41. (link . org-confluence-link)
  42. (paragraph . org-confluence-paragraph)
  43. (property-drawer . org-confluence-property-drawer)
  44. (section . org-confluence-section)
  45. (src-block . org-confluence-src-block)
  46. (strike-through . org-confluence-strike-through)
  47. (table . org-confluence-table)
  48. (table-cell . org-confluence-table-cell)
  49. (table-row . org-confluence-table-row)
  50. (template . org-confluence-template)
  51. (underline . org-confluence-underline)
  52. (verbatim . org-confluence-verbatim)))
  53. (defcustom org-confluence-lang-alist
  54. '(("sh" . "bash"))
  55. "Map from org-babel language name to confluence wiki language name"
  56. :type '(alist :key-type string :value-type string))
  57. ;; All the functions we use
  58. (defun org-confluence-bold (bold contents info)
  59. (format "*%s*" contents))
  60. (defun org-confluence-empty (empty contents info)
  61. "")
  62. (defun org-confluence-example-block (example-block contents info)
  63. ;; FIXME: provide a user-controlled variable for theme
  64. (let ((content (org-export-format-code-default example-block info)))
  65. (org-confluence--block "none" "Confluence" content)))
  66. (defun org-confluence-italic (italic contents info)
  67. (format "_%s_" contents))
  68. (defun org-confluence-item (item contents info)
  69. (let* ((plain-list (org-export-get-parent item))
  70. (type (org-element-property :type plain-list))
  71. (bullet (if (eq type 'ordered) ?\# ?\-)))
  72. (concat (make-string (1+ (org-confluence--li-depth item)) bullet)
  73. " "
  74. (if (eq type 'descriptive)
  75. (concat "*"
  76. (org-export-data (org-element-property :tag item) info)
  77. "* - "))
  78. (org-trim contents))))
  79. (defun org-confluence-fixed-width (fixed-width contents info)
  80. (format "\{\{%s\}\}" contents))
  81. (defun org-confluence-verbatim (verbatim contents info)
  82. (format "\{\{%s\}\}" (org-element-property :value verbatim)))
  83. (defun org-confluence-code (code contents info)
  84. (format "\{\{%s\}\}" (org-element-property :value code)))
  85. (defun org-confluence-headline (headline contents info)
  86. (let ((low-level-rank (org-export-low-level-p headline info))
  87. (text (org-export-data (org-element-property :title headline)
  88. info))
  89. (level (org-export-get-relative-level headline info)))
  90. ;; Else: Standard headline.
  91. (format "h%s. %s\n%s" level text
  92. (if (org-string-nw-p contents) contents
  93. ""))))
  94. (defun org-confluence-link (link desc info)
  95. (let ((raw-link (org-element-property :raw-link link)))
  96. (concat "["
  97. (when (org-string-nw-p desc) (format "%s|" desc))
  98. (cond
  99. ((string-match "^confluence:" raw-link)
  100. (replace-regexp-in-string "^confluence:" "" raw-link))
  101. (t
  102. raw-link))
  103. "]")))
  104. (defun org-confluence-paragraph (paragraph contents info)
  105. "Transcode PARAGRAPH element for Confluence.
  106. CONTENTS is the paragraph contents. INFO is a plist used as
  107. a communication channel."
  108. contents)
  109. (defun org-confluence-property-drawer (property-drawer contents info)
  110. (and (org-string-nw-p contents)
  111. (format "\{\{%s\}\}" contents)))
  112. (defun org-confluence-section (section contents info)
  113. contents)
  114. (defun org-confluence-src-block (src-block contents info)
  115. ;; FIXME: provide a user-controlled variable for theme
  116. (let* ((lang (org-element-property :language src-block))
  117. (language (or (cdr (assoc lang org-confluence-lang-alist)) lang))
  118. (content (org-export-format-code-default src-block info)))
  119. (org-confluence--block language "Emacs" content)))
  120. (defun org-confluence-strike-through (strike-through contents info)
  121. (format "-%s-" contents))
  122. (defun org-confluence-table (table contents info)
  123. contents)
  124. (defun org-confluence-table-row (table-row contents info)
  125. (concat
  126. (if (org-string-nw-p contents) (format "|%s" contents)
  127. "")
  128. (when (org-export-table-row-ends-header-p table-row info)
  129. "|")))
  130. (defun org-confluence-table-cell (table-cell contents info)
  131. (let ((table-row (org-export-get-parent table-cell)))
  132. (concat
  133. (when (org-export-table-row-starts-header-p table-row info)
  134. "|")
  135. contents "|")))
  136. (defun org-confluence-template (contents info)
  137. (let ((depth (plist-get info :with-toc)))
  138. (concat (when depth "\{toc\}\n\n") contents)))
  139. (defun org-confluence-underline (underline contents info)
  140. (format "+%s+" contents))
  141. (defun org-confluence--block (language theme contents)
  142. (concat "\{code:theme=" theme
  143. (when language (format "|language=%s" language))
  144. "}\n"
  145. contents
  146. "\{code\}\n"))
  147. (defun org-confluence--li-depth (item)
  148. "Return depth of a list item; -1 means not a list item"
  149. ;; FIXME check whether it's worth it to cache depth
  150. ;; (it gets recalculated quite a few times while
  151. ;; traversing a list)
  152. (let ((depth -1)
  153. (tag))
  154. (while (and item
  155. (setq tag (car item))
  156. (or (eq tag 'item) ; list items interleave with plain-list
  157. (eq tag 'plain-list)))
  158. (when (eq tag 'item)
  159. (incf depth))
  160. (setq item (org-export-get-parent item)))
  161. depth))
  162. ;; main interactive entrypoint
  163. (defun org-confluence-export-as-confluence
  164. (&optional async subtreep visible-only body-only ext-plist)
  165. "Export current buffer to a text buffer.
  166. If narrowing is active in the current buffer, only export its
  167. narrowed part.
  168. If a region is active, export that region.
  169. A non-nil optional argument ASYNC means the process should happen
  170. asynchronously. The resulting buffer should be accessible
  171. through the `org-export-stack' interface.
  172. When optional argument SUBTREEP is non-nil, export the sub-tree
  173. at point, extracting information from the headline properties
  174. first.
  175. When optional argument VISIBLE-ONLY is non-nil, don't export
  176. contents of hidden elements.
  177. When optional argument BODY-ONLY is non-nil, strip title, table
  178. of contents and footnote definitions from output.
  179. EXT-PLIST, when provided, is a property list with external
  180. parameters overriding Org default settings, but still inferior to
  181. file-local settings.
  182. Export is done in a buffer named \"*Org CONFLUENCE Export*\", which
  183. will be displayed when `org-export-show-temporary-export-buffer'
  184. is non-nil."
  185. (interactive)
  186. (org-export-to-buffer 'confluence "*org CONFLUENCE Export*"
  187. async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
  188. (provide 'ox-confluence)