ox-confluence.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 at gmx dot net>
  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. (link . org-confluence-link)
  40. (section . org-confluence-section)
  41. (src-block . org-confluence-src-block)
  42. (strike-through . org-confluence-strike-through)
  43. (table . org-confluence-table)
  44. (table-cell . org-confluence-table-cell)
  45. (table-row . org-confluence-table-row)
  46. (template . org-confluence-template)
  47. (underline . org-confluence-underline)))
  48. ;; All the functions we use
  49. (defun org-confluence-bold (bold contents info)
  50. (format "*%s*" contents))
  51. (defun org-confluence-empty (empty contents info)
  52. "")
  53. (defun org-confluence-example-block (example-block contents info)
  54. ;; FIXME: provide a user-controlled variable for theme
  55. (let ((content (org-export-format-code-default example-block info)))
  56. (org-confluence--block "none" "Confluence" content)))
  57. (defun org-confluence-italic (italic contents info)
  58. (format "_%s_" contents))
  59. (defun org-confluence-fixed-width (fixed-width contents info)
  60. (format "\{\{%s\}\}" contents))
  61. (defun org-confluence-headline (headline contents info)
  62. (let ((low-level-rank (org-export-low-level-p headline info))
  63. (text (org-export-data (org-element-property :title headline)
  64. info))
  65. (level (org-export-get-relative-level headline info)))
  66. ;; Else: Standard headline.
  67. (format "h%s. %s\n%s" level text
  68. (if (org-string-nw-p contents) contents
  69. ""))))
  70. (defun org-confluence-link (link desc info)
  71. (let ((raw-link (org-element-property :raw-link link)))
  72. (concat "["
  73. (when (org-string-nw-p desc) (format "%s|" desc))
  74. (cond
  75. ((string-match "^confluence:" raw-link)
  76. (replace-regexp-in-string "^confluence:" "" raw-link))
  77. (t
  78. raw-link))
  79. "]")))
  80. (defun org-confluence-section (section contents info)
  81. contents)
  82. (defun org-confluence-src-block (src-block contents info)
  83. ;; FIXME: provide a user-controlled variable for theme
  84. (let* ((lang (org-element-property :language src-block))
  85. (language (if (string= lang "sh") "bash" ;; FIXME: provide a mapping of some sort
  86. lang))
  87. (content (org-export-format-code-default src-block info)))
  88. (org-confluence--block language "Emacs" content)))
  89. (defun org-confluence-strike-through (strike-through contents info)
  90. (format "-%s-" contents))
  91. (defun org-confluence-table (table contents info)
  92. contents)
  93. (defun org-confluence-table-row (table-row contents info)
  94. (concat
  95. (if (org-string-nw-p contents) (format "|%s" contents)
  96. "")
  97. (when (org-export-table-row-ends-header-p table-row info)
  98. "|")))
  99. (defun org-confluence-table-cell (table-cell contents info)
  100. (let ((table-row (org-export-get-parent table-cell)))
  101. (concat
  102. (when (org-export-table-row-starts-header-p table-row info)
  103. "|")
  104. contents "|")))
  105. (defun org-confluence-template (contents info)
  106. (let ((depth (plist-get info :with-toc)))
  107. (concat (when depth "\{toc\}\n\n") contents)))
  108. (defun org-confluence-underline (underline contents info)
  109. (format "+%s+" contents))
  110. (defun org-confluence--block (language theme contents)
  111. (concat "\{code:theme=" theme
  112. (when language (format "|language=%s" language))
  113. "}\n"
  114. contents
  115. "\{code\}\n"))
  116. ;; main interactive entrypoint
  117. (defun org-confluence-export-as-confluence
  118. (&optional async subtreep visible-only body-only ext-plist)
  119. "Export current buffer to a text buffer.
  120. If narrowing is active in the current buffer, only export its
  121. narrowed part.
  122. If a region is active, export that region.
  123. A non-nil optional argument ASYNC means the process should happen
  124. asynchronously. The resulting buffer should be accessible
  125. through the `org-export-stack' interface.
  126. When optional argument SUBTREEP is non-nil, export the sub-tree
  127. at point, extracting information from the headline properties
  128. first.
  129. When optional argument VISIBLE-ONLY is non-nil, don't export
  130. contents of hidden elements.
  131. When optional argument BODY-ONLY is non-nil, strip title, table
  132. of contents and footnote definitions from output.
  133. EXT-PLIST, when provided, is a property list with external
  134. parameters overriding Org default settings, but still inferior to
  135. file-local settings.
  136. Export is done in a buffer named \"*Org CONFLUENCE Export*\", which
  137. will be displayed when `org-export-show-temporary-export-buffer'
  138. is non-nil."
  139. (interactive)
  140. (org-export-to-buffer 'confluence "*org CONFLUENCE Export*"
  141. async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
  142. (provide 'ox-confluence)