dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ox-confluence.el (9425B)


      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   (let ((content (org-element-property :value verbatim)))
    112     (format "\{\{%s\}\}" (string-replace "{" "\\{" content))))
    113 
    114 (defun org-confluence-code (code contents info)
    115   (let ((content (org-element-property :value code)))
    116     (format "\{\{%s\}\}" (string-replace "{" "\\{" content))))
    117 
    118 (defun org-confluence-headline (headline contents info)
    119   (let* ((low-level-rank (org-export-low-level-p headline info))
    120 	 (text (org-export-data (org-element-property :title headline)
    121 				info))
    122 	 (todo (org-export-data (org-element-property :todo-keyword headline)
    123 				info))
    124 	 (level (org-export-get-relative-level headline info))
    125 	 (todo-text (if (or (not (plist-get info :with-todo-keywords))
    126 			    (string= todo ""))
    127 			""
    128 		      (format "*{{%s}}* " todo))))
    129     (format "h%s. %s%s\n%s" level todo-text text
    130             (if (org-string-nw-p contents) contents ""))))
    131 
    132 (defun org-confluence-link (link desc info)
    133   (if (string= "radio" (org-element-property :type link))
    134       desc
    135     (let ((raw-link (org-element-property :raw-link link)))
    136       (concat "["
    137               (when (org-string-nw-p desc) (format "%s|" desc))
    138               (cond
    139                ((string-match "^confluence:" raw-link)
    140 		(replace-regexp-in-string "^confluence:" "" raw-link))
    141                (t
    142 		raw-link))
    143               "]"))))
    144 
    145 (defun org-confluence-paragraph (paragraph contents info)
    146   "Transcode PARAGRAPH element for Confluence.
    147 CONTENTS is the paragraph contents.  INFO is a plist used as
    148 a communication channel."
    149   contents)
    150 
    151 (defun org-confluence-property-drawer (property-drawer contents info)
    152   (and (org-string-nw-p contents)
    153        (format "\{\{%s\}\}" contents)))
    154 
    155 (defun org-confluence-quote-block (quote-block contents info)
    156   (format "{quote}\n%s{quote}" contents))
    157 
    158 (defun org-confluence-section (section contents info)
    159   contents)
    160 
    161 (defun org-confluence-src-block (src-block contents info)
    162   ;; FIXME: provide a user-controlled variable for theme
    163   (let* ((lang (org-element-property :language src-block))
    164          (language (or (cdr (assoc lang org-confluence-lang-alist)) lang))
    165          (content (org-export-format-code-default src-block info)))
    166     (org-confluence--block language "Emacs" content)))
    167 
    168 (defun org-confluence-strike-through (strike-through contents info)
    169   (format "-%s-" contents))
    170 
    171 (defun org-confluence-table (table contents info)
    172   contents)
    173 
    174 (defun org-confluence-table-row  (table-row contents info)
    175   (concat
    176    (if (org-string-nw-p contents) (format "|%s" contents)
    177      "")
    178    (when (org-export-table-row-ends-header-p table-row info)
    179      "|")))
    180 
    181 (defun org-confluence-table-cell  (table-cell contents info)
    182   (let ((table-row (org-export-get-parent table-cell)))
    183     (concat (and (org-export-table-row-starts-header-p table-row info) "|")
    184 	    (if (= (length contents) 0) " " contents)
    185 	    "|")))
    186 
    187 (defun org-confluence-template (contents info)
    188   (let ((depth (plist-get info :with-toc)))
    189     (concat (when depth "\{toc\}\n\n") contents)))
    190 
    191 (defun org-confluence-timestamp (timestamp _contents _info)
    192   "Transcode a TIMESTAMP object from Org to Confluence.
    193 CONTENTS and INFO are ignored."
    194   (let ((translated (org-trim (org-timestamp-translate timestamp))))
    195     (if (string-prefix-p "[" translated)
    196         (concat "(" (substring translated 1 -1) ")")
    197       translated)))
    198 
    199 (defun org-confluence-underline (underline contents info)
    200   (format "+%s+" contents))
    201 
    202 (defun org-confluence--block (language theme contents)
    203   (concat "\{code:theme=" theme
    204           (when language (format "|language=%s" language))
    205           "}\n"
    206           contents
    207           "\{code\}\n"))
    208 
    209 (defun org-confluence--li-depth (item)
    210   "Return depth of a list item; -1 means not a list item"
    211   ;; FIXME check whether it's worth it to cache depth
    212   ;;       (it gets recalculated quite a few times while
    213   ;;       traversing a list)
    214   (let ((depth -1)
    215         (tag))
    216     (while (and item
    217                 (setq tag (car item))
    218                 (or (eq tag 'item) ; list items interleave with plain-list
    219                     (eq tag 'plain-list)))
    220       (when (eq tag 'item)
    221         (cl-incf depth))
    222       (setq item (org-export-get-parent item)))
    223     depth))
    224 
    225 ;; main interactive entrypoint
    226 (defun org-confluence-export-as-confluence
    227   (&optional async subtreep visible-only body-only ext-plist)
    228   "Export current buffer to a text buffer.
    229 
    230 If narrowing is active in the current buffer, only export its
    231 narrowed part.
    232 
    233 If a region is active, export that region.
    234 
    235 A non-nil optional argument ASYNC means the process should happen
    236 asynchronously.  The resulting buffer should be accessible
    237 through the `org-export-stack' interface.
    238 
    239 When optional argument SUBTREEP is non-nil, export the sub-tree
    240 at point, extracting information from the headline properties
    241 first.
    242 
    243 When optional argument VISIBLE-ONLY is non-nil, don't export
    244 contents of hidden elements.
    245 
    246 When optional argument BODY-ONLY is non-nil, strip title, table
    247 of contents and footnote definitions from output.
    248 
    249 EXT-PLIST, when provided, is a property list with external
    250 parameters overriding Org default settings, but still inferior to
    251 file-local settings.
    252 
    253 Export is done in a buffer named \"*Org CONFLUENCE Export*\", which
    254 will be displayed when `org-export-show-temporary-export-buffer'
    255 is non-nil."
    256   (interactive)
    257   (org-export-to-buffer 'confluence "*org CONFLUENCE Export*"
    258     async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
    259 
    260 (provide 'ox-confluence)