ox-confluence.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. (example-block . org-confluence-example-block)
  34. (fixed-width . org-confluence-fixed-width)
  35. (footnote-definition . org-confluence-empty)
  36. (footnote-reference . org-confluence-empty)
  37. (headline . org-confluence-headline)
  38. (italic . org-confluence-italic)
  39. (item . org-confluence-item)
  40. (link . org-confluence-link)
  41. (paragraph . org-confluence-paragraph)
  42. (property-drawer . org-confluence-property-drawer)
  43. (section . org-confluence-section)
  44. (src-block . org-confluence-src-block)
  45. (strike-through . org-confluence-strike-through)
  46. (table . org-confluence-table)
  47. (table-cell . org-confluence-table-cell)
  48. (table-row . org-confluence-table-row)
  49. (template . org-confluence-template)
  50. (timestamp . org-confluence-timestamp)
  51. (underline . org-confluence-underline)))
  52. (defcustom org-confluence-lang-alist
  53. '(("sh" . "bash"))
  54. "Map from org-babel language name to confluence wiki language name"
  55. :type '(alist :key-type string :value-type string))
  56. ;; All the functions we use
  57. (defun org-confluence-bold (bold contents info)
  58. (format "*%s*" contents))
  59. (defun org-confluence-empty (empty contents info)
  60. "")
  61. (defun org-confluence-example-block (example-block contents info)
  62. ;; FIXME: provide a user-controlled variable for theme
  63. (let ((content (org-export-format-code-default example-block info)))
  64. (org-confluence--block "none" "Confluence" content)))
  65. (defun org-confluence-italic (italic contents info)
  66. (format "_%s_" contents))
  67. (defun org-confluence-item (item contents info)
  68. (concat (make-string (1+ (org-confluence--li-depth item)) ?\-)
  69. " "
  70. (org-trim contents)))
  71. (defun org-confluence-fixed-width (fixed-width contents info)
  72. (format "\{\{%s\}\}" contents))
  73. (defun org-confluence-headline (headline contents info)
  74. (let* ((low-level-rank (org-export-low-level-p headline info))
  75. (text (org-export-data (org-element-property :title headline)
  76. info))
  77. (todo (org-export-data (org-element-property :todo-keyword headline)
  78. info))
  79. (level (org-export-get-relative-level headline info))
  80. (todo-text (if (or (not (plist-get info :with-todo-keywords))
  81. (string= todo ""))
  82. ""
  83. (format "*{{%s}}* " todo))))
  84. ;; Else: Standard headline.
  85. (format "h%s. %s%s\n%s" level todo-text text
  86. (if (org-string-nw-p contents) contents ""))))
  87. (defun org-confluence-link (link desc info)
  88. (let ((raw-link (org-element-property :raw-link link)))
  89. (concat "["
  90. (when (org-string-nw-p desc) (format "%s|" desc))
  91. (cond
  92. ((string-match "^confluence:" raw-link)
  93. (replace-regexp-in-string "^confluence:" "" raw-link))
  94. (t
  95. raw-link))
  96. "]")))
  97. (defun org-confluence-paragraph (paragraph contents info)
  98. "Transcode PARAGRAPH element for Confluence.
  99. CONTENTS is the paragraph contents. INFO is a plist used as
  100. a communication channel."
  101. contents)
  102. (defun org-confluence-property-drawer (property-drawer contents info)
  103. (and (org-string-nw-p contents)
  104. (format "\{\{%s\}\}" contents)))
  105. (defun org-confluence-section (section contents info)
  106. contents)
  107. (defun org-confluence-src-block (src-block contents info)
  108. ;; FIXME: provide a user-controlled variable for theme
  109. (let* ((lang (org-element-property :language src-block))
  110. (language (or (cdr (assoc lang org-confluence-lang-alist)) lang))
  111. (content (org-export-format-code-default src-block info)))
  112. (org-confluence--block language "Emacs" content)))
  113. (defun org-confluence-strike-through (strike-through contents info)
  114. (format "-%s-" contents))
  115. (defun org-confluence-table (table contents info)
  116. contents)
  117. (defun org-confluence-table-row (table-row contents info)
  118. (concat
  119. (if (org-string-nw-p contents) (format "|%s" contents)
  120. "")
  121. (when (org-export-table-row-ends-header-p table-row info)
  122. "|")))
  123. (defun org-confluence-table-cell (table-cell contents info)
  124. (let ((table-row (org-export-get-parent table-cell)))
  125. (concat (and (org-export-table-row-starts-header-p table-row info) "|")
  126. (if (= (length contents) 0) " " contents)
  127. "|")))
  128. (defun org-confluence-template (contents info)
  129. (let ((depth (plist-get info :with-toc)))
  130. (concat (when depth "\{toc\}\n\n") contents)))
  131. (defun org-confluence-timestamp (timestamp _contents _info)
  132. "Transcode a TIMESTAMP object from Org to Confluence.
  133. CONTENTS and INFO are ignored."
  134. (let ((translated (org-timestamp-translate timestamp)))
  135. (if (string-prefix-p "[" translated)
  136. (concat "(" (substring translated 1 -1) ")")
  137. translated)))
  138. (defun org-confluence-underline (underline contents info)
  139. (format "+%s+" contents))
  140. (defun org-confluence--block (language theme contents)
  141. (concat "\{code:theme=" theme
  142. (when language (format "|language=%s" language))
  143. "}\n"
  144. contents
  145. "\{code\}\n"))
  146. (defun org-confluence--li-depth (item)
  147. "Return depth of a list item; -1 means not a list item"
  148. ;; FIXME check whether it's worth it to cache depth
  149. ;; (it gets recalculated quite a few times while
  150. ;; traversing a list)
  151. (let ((depth -1)
  152. (tag))
  153. (while (and item
  154. (setq tag (car item))
  155. (or (eq tag 'item) ; list items interleave with plain-list
  156. (eq tag 'plain-list)))
  157. (when (eq tag 'item)
  158. (incf depth))
  159. (setq item (org-export-get-parent item)))
  160. depth))
  161. ;; main interactive entrypoint
  162. (defun org-confluence-export-as-confluence
  163. (&optional async subtreep visible-only body-only ext-plist)
  164. "Export current buffer to a text buffer.
  165. If narrowing is active in the current buffer, only export its
  166. narrowed part.
  167. If a region is active, export that region.
  168. A non-nil optional argument ASYNC means the process should happen
  169. asynchronously. The resulting buffer should be accessible
  170. through the `org-export-stack' interface.
  171. When optional argument SUBTREEP is non-nil, export the sub-tree
  172. at point, extracting information from the headline properties
  173. first.
  174. When optional argument VISIBLE-ONLY is non-nil, don't export
  175. contents of hidden elements.
  176. When optional argument BODY-ONLY is non-nil, strip title, table
  177. of contents and footnote definitions from output.
  178. EXT-PLIST, when provided, is a property list with external
  179. parameters overriding Org default settings, but still inferior to
  180. file-local settings.
  181. Export is done in a buffer named \"*Org CONFLUENCE Export*\", which
  182. will be displayed when `org-export-show-temporary-export-buffer'
  183. is non-nil."
  184. (interactive)
  185. (org-export-to-buffer 'confluence "*org CONFLUENCE Export*"
  186. async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
  187. (provide 'ox-confluence)