org-eldoc: Handle source blocks with empty language
[org-mode/org-tableheadings.git] / contrib / lisp / ox-confluence.el
blob4b74e75b418e6c9a11962c8a4cc341f41cd87b2e
1 ;;; ox-confluence --- Confluence Wiki Back-End for Org Export Engine
3 ;; Copyright (C) 2012, 2014 Sébastien Delafond
5 ;; Author: Sébastien Delafond <sdelafond@gmail.com>
6 ;; Keywords: outlines, confluence, wiki
8 ;; This file is not part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; ox-confluence.el lets you convert Org files to confluence files
26 ;; using the ox.el export engine.
28 ;; Put this file into your load-path and the following into your ~/.emacs:
29 ;; (require 'ox-confluence)
31 ;; Export Org files to confluence:
32 ;; M-x org-confluence-export-as-confluence RET
34 ;;; Code:
36 (require 'ox)
37 (require 'ox-ascii)
39 ;; Define the backend itself
40 (org-export-define-derived-backend 'confluence 'ascii
41 :translate-alist '((bold . org-confluence-bold)
42 (example-block . org-confluence-example-block)
43 (fixed-width . org-confluence-fixed-width)
44 (footnote-definition . org-confluence-empty)
45 (footnote-reference . org-confluence-empty)
46 (headline . org-confluence-headline)
47 (italic . org-confluence-italic)
48 (item . org-confluence-item)
49 (link . org-confluence-link)
50 (paragraph . org-confluence-paragraph)
51 (property-drawer . org-confluence-property-drawer)
52 (section . org-confluence-section)
53 (src-block . org-confluence-src-block)
54 (strike-through . org-confluence-strike-through)
55 (table . org-confluence-table)
56 (table-cell . org-confluence-table-cell)
57 (table-row . org-confluence-table-row)
58 (template . org-confluence-template)
59 (underline . org-confluence-underline)))
61 ;; All the functions we use
62 (defun org-confluence-bold (bold contents info)
63 (format "*%s*" contents))
65 (defun org-confluence-empty (empty contents info)
66 "")
68 (defun org-confluence-example-block (example-block contents info)
69 ;; FIXME: provide a user-controlled variable for theme
70 (let ((content (org-export-format-code-default example-block info)))
71 (org-confluence--block "none" "Confluence" content)))
73 (defun org-confluence-italic (italic contents info)
74 (format "_%s_" contents))
76 (defun org-confluence-item (item contents info)
77 (concat (make-string (1+ (org-confluence--li-depth item)) ?\-)
78 " "
79 (org-trim contents)))
81 (defun org-confluence-fixed-width (fixed-width contents info)
82 (format "\{\{%s\}\}" contents))
84 (defun org-confluence-headline (headline contents info)
85 (let ((low-level-rank (org-export-low-level-p headline info))
86 (text (org-export-data (org-element-property :title headline)
87 info))
88 (level (org-export-get-relative-level headline info)))
89 ;; Else: Standard headline.
90 (format "h%s. %s\n%s" level text
91 (if (org-string-nw-p contents) contents
92 ""))))
94 (defun org-confluence-link (link desc info)
95 (let ((raw-link (org-element-property :raw-link link)))
96 (concat "["
97 (when (org-string-nw-p desc) (format "%s|" desc))
98 (cond
99 ((string-match "^confluence:" raw-link)
100 (replace-regexp-in-string "^confluence:" "" raw-link))
102 raw-link))
103 "]")))
105 (defun org-confluence-paragraph (paragraph contents info)
106 "Transcode PARAGRAPH element for Confluence.
107 CONTENTS is the paragraph contents. INFO is a plist used as
108 a communication channel."
109 contents)
111 (defun org-confluence-property-drawer (property-drawer contents info)
112 (and (org-string-nw-p contents)
113 (format "\{\{%s\}\}" contents)))
115 (defun org-confluence-section (section contents info)
116 contents)
118 (defun org-confluence-src-block (src-block contents info)
119 ;; FIXME: provide a user-controlled variable for theme
120 (let* ((lang (org-element-property :language src-block))
121 (language (if (string= lang "sh") "bash" ;; FIXME: provide a mapping of some sort
122 lang))
123 (content (org-export-format-code-default src-block info)))
124 (org-confluence--block language "Emacs" content)))
126 (defun org-confluence-strike-through (strike-through contents info)
127 (format "-%s-" contents))
129 (defun org-confluence-table (table contents info)
130 contents)
132 (defun org-confluence-table-row (table-row contents info)
133 (concat
134 (if (org-string-nw-p contents) (format "|%s" contents)
136 (when (org-export-table-row-ends-header-p table-row info)
137 "|")))
139 (defun org-confluence-table-cell (table-cell contents info)
140 (let ((table-row (org-export-get-parent table-cell)))
141 (concat
142 (when (org-export-table-row-starts-header-p table-row info)
143 "|")
144 contents "|")))
146 (defun org-confluence-template (contents info)
147 (let ((depth (plist-get info :with-toc)))
148 (concat (when depth "\{toc\}\n\n") contents)))
150 (defun org-confluence-underline (underline contents info)
151 (format "+%s+" contents))
153 (defun org-confluence--block (language theme contents)
154 (concat "\{code:theme=" theme
155 (when language (format "|language=%s" language))
156 "}\n"
157 contents
158 "\{code\}\n"))
160 (defun org-confluence--li-depth (item)
161 "Return depth of a list item; -1 means not a list item"
162 ;; FIXME check whether it's worth it to cache depth
163 ;; (it gets recalculated quite a few times while
164 ;; traversing a list)
165 (let ((depth -1)
166 (tag))
167 (while (and item
168 (setq tag (car item))
169 (or (eq tag 'item) ; list items interleave with plain-list
170 (eq tag 'plain-list)))
171 (when (eq tag 'item)
172 (incf depth))
173 (setq item (org-export-get-parent item)))
174 depth))
176 ;; main interactive entrypoint
177 (defun org-confluence-export-as-confluence
178 (&optional async subtreep visible-only body-only ext-plist)
179 "Export current buffer to a text buffer.
181 If narrowing is active in the current buffer, only export its
182 narrowed part.
184 If a region is active, export that region.
186 A non-nil optional argument ASYNC means the process should happen
187 asynchronously. The resulting buffer should be accessible
188 through the `org-export-stack' interface.
190 When optional argument SUBTREEP is non-nil, export the sub-tree
191 at point, extracting information from the headline properties
192 first.
194 When optional argument VISIBLE-ONLY is non-nil, don't export
195 contents of hidden elements.
197 When optional argument BODY-ONLY is non-nil, strip title, table
198 of contents and footnote definitions from output.
200 EXT-PLIST, when provided, is a property list with external
201 parameters overriding Org default settings, but still inferior to
202 file-local settings.
204 Export is done in a buffer named \"*Org CONFLUENCE Export*\", which
205 will be displayed when `org-export-show-temporary-export-buffer'
206 is non-nil."
207 (interactive)
208 (org-export-to-buffer 'confluence "*org CONFLUENCE Export*"
209 async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
211 (provide 'ox-confluence)