Move new export framework files into core
[org-mode.git] / contrib / lisp / ox-confluence.el
blob99ef9c012388e50a78ff7fa26b2f7de3f70e3e1d
1 ;;; ox-confluence --- Confluence Wiki Back-End for Org Export Engine
3 ;; Copyright (C) 2012 Sébastien Delafond
5 ;; Author: Sébastien Delafond <sdelafond at gmx dot net>
6 ;; Keywords: outlines, confluence, wiki
8 ;; This file is not part of GNU Emacs.
10 ;; GNU Emacs 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 ;; GNU Emacs 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 (link . org-confluence-link)
49 (section . org-confluence-section)
50 (src-block . org-confluence-src-block)
51 (strike-through . org-confluence-strike-through)
52 (table . org-confluence-table)
53 (table-cell . org-confluence-table-cell)
54 (table-row . org-confluence-table-row)
55 (template . org-confluence-template)
56 (underline . org-confluence-underline)))
58 ;; All the functions we use
59 (defun org-confluence-bold (bold contents info)
60 (format "*%s*" contents))
62 (defun org-confluence-empty (empy contents info)
63 "")
65 (defun org-confluence-example-block (example-block contents info)
66 ;; FIXME: provide a user-controlled variable for theme
67 (let ((content (org-export-format-code-default example-block info)))
68 (org-confluence--block "none" "Confluence" content)))
70 (defun org-confluence-italic (italic contents info)
71 (format "_%s_" contents))
73 (defun org-confluence-fixed-width (fixed-width contents info)
74 (format "\{\{%s\}\}" contents))
76 (defun org-confluence-headline (headline contents info)
77 (let ((low-level-rank (org-export-low-level-p headline info))
78 (text (org-export-data (org-element-property :title headline)
79 info))
80 (level (org-export-get-relative-level headline info)))
81 ;; Else: Standard headline.
82 (format "h%s. %s\n%s" level text
83 (if (org-string-nw-p contents) contents
84 ""))))
86 (defun org-confluence-link (link desc info)
87 (let ((raw-link (org-element-property :raw-link link)))
88 (concat "["
89 (when (org-string-nw-p desc) (format "%s|" desc))
90 (cond
91 ((string-match "^confluence:" raw-link)
92 (replace-regexp-in-string "^confluence:" "" raw-link))
94 raw-link))
95 "]")))
96 (defun org-confluence-section (section contents info)
97 contents)
99 (defun org-confluence-src-block (src-block contents info)
100 ;; FIXME: provide a user-controlled variable for theme
101 (let* ((lang (org-element-property :language src-block))
102 (language (if (string= lang "sh") "bash" ;; FIXME: provide a mapping of some sort
103 lang))
104 (content (org-export-format-code-default src-block info)))
105 (org-confluence--block language "Emacs" content)))
107 (defun org-confluence-strike-through (strike-through contents info)
108 (format "-%s-" contents))
110 (defun org-confluence-table (table contents info)
111 contents)
113 (defun org-confluence-table-row (table-row contents info)
114 (concat
115 (if (org-string-nw-p contents) (format "|%s" contents)
117 (when (org-export-table-row-ends-header-p table-row info)
118 "|")))
120 (defun org-confluence-table-cell (table-cell contents info)
121 (let ((table-row (org-export-get-parent table-cell)))
122 (concat
123 (when (org-export-table-row-starts-header-p table-row info)
124 "|")
125 contents "|")))
127 (defun org-confluence-template (contents info)
128 (let ((depth (plist-get info :with-toc)))
129 (concat (when depth "\{toc\}\n\n") contents)))
131 (defun org-confluence-underline (underline contents info)
132 (format "+%s+" contents))
134 (defun org-confluence--block (language theme contents)
135 (concat "\{code:theme=" theme
136 (when language (format "|language=%s" language))
137 "}\n"
138 contents
139 "\{code\}\n"))
141 ;; main interactive entrypoint
142 (defun org-confluence-export-as-confluence
143 (&optional async subtreep visible-only body-only ext-plist)
144 "Export current buffer to a text buffer.
146 If narrowing is active in the current buffer, only export its
147 narrowed part.
149 If a region is active, export that region.
151 A non-nil optional argument ASYNC means the process should happen
152 asynchronously. The resulting buffer should be accessible
153 through the `org-export-stack' interface.
155 When optional argument SUBTREEP is non-nil, export the sub-tree
156 at point, extracting information from the headline properties
157 first.
159 When optional argument VISIBLE-ONLY is non-nil, don't export
160 contents of hidden elements.
162 When optional argument BODY-ONLY is non-nil, strip title, table
163 of contents and footnote definitions from output.
165 EXT-PLIST, when provided, is a property list with external
166 parameters overriding Org default settings, but still inferior to
167 file-local settings.
169 Export is done in a buffer named \"*Org E-Confluence Export*\", which
170 will be displayed when `org-export-show-temporary-export-buffer'
171 is non-nil."
172 (interactive)
173 (if async
174 (org-export-async-start
175 (lambda (output)
176 (with-current-buffer (get-buffer-create "*Org E-Confluence Export*")
177 (erase-buffer)
178 (insert output)
179 (goto-char (point-min))
180 (text-mode)
181 (org-export-add-to-stack (current-buffer) 'confluence)))
182 `(org-export-as 'confluence ,subtreep ,visible-only ,body-only
183 ',ext-plist))
184 (let ((outbuf (org-export-to-buffer
185 'confluence "*Org E-Confluence Export*"
186 subtreep visible-only body-only ext-plist)))
187 (with-current-buffer outbuf (text-mode))
188 (when org-export-show-temporary-export-buffer
189 (switch-to-buffer-other-window outbuf)))))
191 (provide 'ox-confluence)