-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathox-tiddly.el
230 lines (190 loc) · 7.63 KB
/
ox-tiddly.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
;;; ox-tiddly.el --- Org TiddlyWiki exporter
;; Copyright (C) 2013 Derek Feichtinger
;; Author: Derek Feichtinger <[email protected]>
;; Keywords: org
;; Homepage: https://github.com/dfeich/org8-wikiexporters
;; Package-Requires: ((org "8") (emacs "24.4"))
;; Version: 0.1.20200927
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; ox-tiddly.el lets you convert Org files to tiddly buffers
;; using the ox.el export engine.
;;
;; Put this file into your load-path and the following into your ~/.emacs:
;; (require 'ox-tiddly)
;;
;; Export Org files to tiddly:
;; M-x org-tiddly-export-as-tiddly RET
;;
;; based on ox-confluence by Sébastien Delafond
;;; Code:
(require 'ox)
(require 'ox-ascii)
(require 'cl-lib)
;; Define the backend itself
(org-export-define-derived-backend 'tiddly 'ascii
:translate-alist '((bold . org-tiddly-bold)
(example-block . org-tiddly-example-block)
(export-block . org-tiddly-export-block)
(fixed-width . org-tiddly-fixed-width)
(footnote-definition . org-tiddly-empty)
(footnote-reference . org-tiddly-empty)
(headline . org-tiddly-headline)
(italic . org-tiddly-italic)
(item . org-tiddly-item)
(link . org-tiddly-link)
(paragraph . org-tiddly-paragraph)
;; (plain-list . org-tiddly-plain-list)
(section . org-tiddly-section)
(src-block . org-tiddly-src-block)
(strike-through . org-tiddly-strike-through)
(table . org-tiddly-table)
(table-cell . org-tiddly-table-cell)
(table-row . org-tiddly-table-row)
(template . org-tiddly-template)
(underline . org-tiddly-underline)
(verbatim . org-tiddly-verbatim))
;; :menu-entry '(?w "Export to Wiki"
;; ((?t "As TiddlyWiki buffer" org-tiddly-export-as-tiddly)))
)
;;;;;;;;;;
;; debugging helpers
(defun ox-tiddly---enclose-element-property (plist property tag)
(format "<%s>%s</%s>" tag (org-element-property property plist) tag))
(defun ox-tiddly--plist-get-keys (pl)
(let (result)
(cl-loop for (key val) on pl by #'cddr
do (push key result))
result))
;;;;;;;;;;
;; All the functions we use
(defun org-tiddly-bold (bold contents info)
(format "''%s''" contents))
(defun org-tiddly-empty (empty contents info)
"")
(defun org-tiddly-plain-list (plain-list contents info)
contents)
(defun org-tiddly-item (item contents info)
(let* ((beg (org-element-property :begin item))
(struct (org-element-property :structure item))
(itemstruct (assoc beg struct))
(parent (org-element-property :parent item))
(ltype (org-element-property :type parent))
(indices (org-list-get-item-number
(org-element-property :begin item)
struct
(org-list-prevs-alist struct)
(org-list-parents-alist struct))))
(concat
(if (eq ltype 'ordered) (make-string (length indices) ?#)
(make-string (length indices) ?*))
" " contents)))
(defun org-tiddly-example-block (example-block contents info)
(format "\n\{\{\{\n%s\}\}\}\n"
(org-export-format-code-default example-block info)))
(defun org-tiddly-export-block (export-block contents info)
"Transcode a EXPORT-BLOCK element from Org to HTML.
CONTENTS is nil. INFO is a plist used as a communication
channel."
(when (string= (org-element-property :type export-block) "HTML")
(org-element-property :value export-block)))
(defun org-tiddly-italic (italic contents info)
(format "//%s//" contents))
(defun org-tiddly-fixed-width (fixed-width contents info)
(format "\{\{\{\n%s\}\}\}"
(org-element-property :value fixed-width)))
(defun org-tiddly-verbatim (verbatim contents info)
(format "\{\{\{%s\}\}\}" (org-element-property :value verbatim)))
(defun org-tiddly-headline (headline contents info)
(let ((low-level-rank (org-export-low-level-p headline info))
(text (org-export-data (org-element-property :title headline)
info))
(level (org-export-get-relative-level headline info)))
;; Else: Standard headline.
(format "%s %s\n%s" (make-string level ?\!) text
(if (org-string-nw-p contents) contents
""))))
;; TODO: if there is whitespace in desc, the line gets sometimes broken apart. Why?
(defun org-tiddly-link (link desc info)
(let ((raw-link (org-element-property :raw-link link)))
(concat "[["
(when (org-string-nw-p desc) (format "%s|" desc))
raw-link
"]]")))
;; replace all newlines in paragraphs (includes list item text, which
;; also constitutes a paragraph
(defun org-tiddly-paragraph (paragraph contents info)
(replace-regexp-in-string "\n" " " contents))
(defun org-tiddly-section (section contents info)
contents)
(defun org-tiddly-src-block (src-block contents info)
(org-tiddly-example-block src-block contents info))
(defun org-tiddly-strike-through (strike-through contents info)
(format "==%s==" contents))
(defun org-tiddly-table (table contents info)
contents)
(defun org-tiddly-table-row (table-row contents info)
(concat
(if (org-string-nw-p contents) (format "|%s" contents)
"")
(when (org-export-table-row-ends-header-p table-row info)
"|")))
(defun org-tiddly-table-cell (table-cell contents info)
(let ((table-row (org-export-get-parent table-cell)))
(concat
(when (org-export-table-row-starts-header-p table-row info)
"|")
contents "|")))
(defun org-tiddly-template (contents info)
(let ((depth (plist-get info :with-toc)))
(concat (when depth "<<ToC>>\n\n") contents)))
(defun org-tiddly-underline (underline contents info)
(format "__%s__" contents))
;; (defun org-tiddly--block (language theme contents)
;; (concat "\{code:theme=" theme
;; (when language (format "|language=%s" language))
;; "}\n"
;; contents
;; "\{code\}\n"))
;; main interactive entrypoint
;;;###autoload
(defun org-tiddly-export-as-tiddly
(&optional async subtreep visible-only body-only ext-plist)
"Export current buffer to a text buffer.
If narrowing is active in the current buffer, only export its
narrowed part.
If a region is active, export that region.
A non-nil optional argument ASYNC means the process should happen
asynchronously. The resulting buffer should be accessible
through the `org-export-stack' interface.
When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.
When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.
When optional argument BODY-ONLY is non-nil, strip title, table
of contents and footnote definitions from output.
EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.
Export is done in a buffer named \"*Org Tiddly Export*\", which
will be displayed when `org-export-show-temporary-export-buffer'
is non-nil."
(interactive)
(org-export-to-buffer 'tiddly "*Org Tiddly Export*"
async subtreep visible-only body-only ext-plist (lambda () (text-mode))))
(provide 'ox-tiddly)
;;; ox-tiddly.el ends here