ox-confluence.el 9.1 KB

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