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