forked from polymode/polymode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymode-export.el
321 lines (295 loc) · 14.8 KB
/
polymode-export.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
(require 'polymode-common)
(require 'polymode-classes)
(defgroup polymode-export nil
"Polymode Exporters"
:group 'polymode)
(defcustom polymode-exporter-output-file-format "%s[exported]"
"Format of the exported files.
%s is substituted with the current file name sans extension."
:group 'polymode-export
:type 'string)
(defclass pm-exporter (pm-root)
((from
:initarg :from
:initform '()
:type list
:custom list
:documentation
"Input specifications. A list of lists of the form (id reg doc commmand).
ID is the unique identifier of the spec. REG is a regexp
that is used to identify if current file can be exported
with this spec. DOC is a short help string shown during
interactive export. COMMMAND is the actual exporter
specific command. It can contain the following format
specs:
%i - replaced with the input file
%o - replaced with the ouput file
%O - replaced with the base output file name (no dir, no extension)
%t - replaced with the 4th element of the :to spec.
")
(to
:initarg :to
:initform '()
:type list
:custom list
:documentation
"Output specifications. A list of the list of the form (id ext doc %t-spec).
EXT is the *exact* extension (not a regexp) of the ouput
file. %t-spec is a string what is used to format :from
spec commmand.")
(function
:initarg :function
:initform (lambda (command from to)
(error "Function not defined for this exporter"))
:type (or symbol function)
:documentation
"Function to process the commmand. Must take 3 arguments
COMMAND, FROM-ID and TO-ID. COMMAND is the 4th argument
of :from spec with all the formats substituted. FROM-ID is
the id of requested :from spec, TO-ID is the id of the :to
spec."))
"Root exporter class.")
(defclass pm-callback-exporter (pm-exporter)
((callback
:initarg :callback
:initform (lambda (&optional rest)
(error "No callback defined for this exporter."))
:type (or symbol function)
:documentation
"Callback function to be called by :function when a shell
call is involved. There is no default callback."))
"Class to represent exporters that call processes spanned by
emacs. Callback should return the output file name.")
(defclass pm-shell-exporter (pm-exporter)
((function
:initform 'pm-default-shell-export-function)
(sentinel
:initarg :sentinel
:initform 'pm-default-export-sentinel
:type (or symbol function)
:documentation
"Sentinel function to be called by :function when a shell
call is involved. Sentinel should return the output file
name."))
"Class to represent exporters that call external processes.")
;;; METHODS
(defgeneric pm-export (exporter from to &optional ifile)
"Weave current FILE with EXPORTER.")
(defmethod pm-export ((exporter pm-exporter) from to &optional ifile)
(let* ((from-spec (assoc from (oref exporter :from)))
(to-spec (assoc to (oref exporter :to)))
(ifile (or ifile (file-name-nondirectory buffer-file-name)))
(ofile (concat (format polymode-exporter-output-file-format
(file-name-base ifile))
"." (nth 1 to-spec)))
(command (format-spec (nth 3 from-spec)
(list (cons ?i ifile)
(cons ?o ofile)
(cons ?O (file-name-base ofile))
(cons ?t (nth 3 to-spec))))))
(unless to-spec
(error "'to' spec %s is not defined for this exporter '%s'"
to (pm--object-name exporter)))
;; compunicate with sentinel and callback with local vars in order to
;; avoid needless clutter
;; fixme: use -hist
(set (make-local-variable 'pm--output-file) ofile)
(set (make-local-variable 'pm--input-file) ifile)
(message "Exporting '%s' with '%s' exporter ..."
(file-name-nondirectory ifile) (pm--object-name exporter))
(let ((ofile (funcall (oref exporter :function) command from to)))
(and ofile (stringp ofile) (pm--display-file ofile)))))
(defmacro pm--export-wrap-callback (slot)
;; replace exporter's :sentinel or :callback temporally in order to display
;; the output buffer
`(let ((sentinel1 (oref exporter ,slot)))
(condition-case err
(let ((sentinel2 `(lambda (proc name)
(let ((efile (,sentinel1 proc name)))
(pm--display-file efile)
efile))))
(oset exporter ,slot sentinel2)
(call-next-method exporter from to ifile))
(error (oset exporter ,slot sentinel1)
(signal (car err) (cdr err))))
(oset exporter ,slot sentinel1)))
(defmethod pm-export ((exporter pm-callback-exporter) from to &optional ifile)
(pm--export-wrap-callback :callback))
(defmethod pm-export ((exporter pm-shell-exporter) from to &optional ifile)
(pm--export-wrap-callback :sentinel))
;; UI
(defvar pm--exporter-hist nil)
(defvar pm--export:from-hist nil)
(defvar pm--export:to-hist nil)
(declare-function polymode-set-weaver "polymode-weave")
(declare-function pm-weave "polymode-weave")
(defun polymode-export (&optional from to)
"Export current file.
FROM and TO are the from-id and to-id in the definition of the
current exporter. If current exporter hasn't been set yet, call
`polymode-set-exporter' before exporting.
If called interactively with C-u argument, ask for the FROM type
interactively, otherwise FROM and TO are determined automatically
from the current exporter specification and current file
extension. See `pm-exporter' for the precise specification.
If called interactively with C-u C-u argument, set new exporter
first with `polymode-set-exporter'."
(interactive "P")
;; todo: '(16) should allow for edditing :from command
(let* ((exporter (symbol-value
(if (equal from '(16))
(polymode-set-exporter)
(or (oref pm/polymode :exporter)
(polymode-set-exporter)))))
(fname (file-name-nondirectory buffer-file-name))
(e:from (oref exporter :from))
(e:to (oref exporter :to))
(from-opts (mapcar (lambda (el)
(propertize (nth 2 el) :id (car el)))
(oref exporter :from)))
(to-opts (mapcar (lambda (el)
(propertize (format "%s (%s)" (nth 2 el) (nth 1 el))
:id (car el)))
(oref exporter :to)))
(from
(cond ((or (null from) (equal from '(16)))
(let ((case-fold-search t))
(or (and (pm--get-hist :export-from)
(get-text-property 0 :id (pm--get-hist :export-from)))
(car (cl-rassoc-if (lambda (el)
(string-match-p (car el) fname))
e:from))
;; guess from weaver and return a cons of (weaver-id . exporter-id)
(let ((weaver (symbol-value (or (oref pm/polymode :weaver)
(polymode-set-weaver)))))
(cl-loop for w in (oref weaver :from-to)
;; weaver intput extension matches the filename
if (string-match-p (nth 1 w) fname)
return (cl-loop for e in e:from
;; input exporter extensnion matches weaver output extension
if (string-match-p (nth 1 e) (concat "dummy." (nth 2 w)))
return (cons (car w) (car e)))))
(let* ((prompt (format "No input spec for extension '.%s' in '%s' exporter. Choose one: "
(file-name-extension fname)
(pm--object-name exporter)))
(sel (ido-completing-read prompt from-opts nil t nil
'pm--export:from-hist
(pm--get-hist :export-from))))
(pm--put-hist :export-from sel)
(get-text-property 0 :id sel)))))
;; C-u, force a :from spec
((equal from '(4))
(let ((sel (ido-completing-read "Input type: " from-opts nil t nil
'pm--export:from-hist (pm--get-hist :export-from)) ))
(pm--put-hist :export-from sel)
(get-text-property 1 :id sel)))
((stringp from)
(if (assoc from e:from)
from
(error "Cannot find input spec '%s' in %s exporter"
from (pm--object-name exporter))))
(t (error "'from' argument must be nil, universal argument or a string"))
))
(to
(cond ((null to)
(let ((sel (ido-completing-read "Export to: " to-opts nil t nil
'pm--export:to-hist (pm--get-hist :export-to))))
(pm--put-hist :export-to sel)
(get-text-property 1 :id sel)))
((stringp to)
(if (assoc to e:to)
to
(error "Cannot find output spec '%s' in %s exporter"
to (pm--object-name exporter))))
(t (error "'to' argument must be nil or a string")))))
(if (consp from)
;; run through weaver
(pm-weave (symbol-value (oref pm/polymode :weaver)) (car from) (cons (cdr from) to))
(pm-export exporter from to))))
(defmacro polymode-register-exporter (exporter default? &rest configs)
"Add EXPORTER to :exporters slot of all config objects in CONFIGS.
When DEFAULT? is non-nil, also make EXPORTER the default exporter
for each polymode in CONFIGS."
`(dolist (pm ',configs)
(object-add-to-list (symbol-value pm) :exporters ',exporter)
(when ,default? (oset (symbol-value pm) :exporter ',exporter))))
(defun polymode-set-exporter ()
(interactive)
(unless pm/polymode
(error "No pm/polymode object found. Not in polymode buffer?"))
(let* ((exporters (pm--abrev-names
(delete-dups (pm--oref-with-parents pm/polymode :exporters))
"pm-exporter/"))
(sel (ido-completing-read "No default exporter. Choose one: " exporters nil t nil
'pm--exporter-hist (car pm--exporter-hist)))
(out (intern (get-text-property 0 :orig sel))))
(oset pm/polymode :exporter out)
out))
;;; UTILS
(defun pm-default-export-sentinel (process name)
"Default exporter sentinel."
(pm--run-command-sentinel process name "exporting"))
(defun pm-default-shell-export-function (command from to)
"Run exporting command interactively.
Run command in a buffer (in comint-shell-mode) so that it accepts
user interaction. This is a default function in all exporters
that call a shell command"
(pm--run-command command
(oref (symbol-value (oref pm/polymode :exporter)) :sentinel)
"*polymode export*"
(concat "Exporting " from "-->" to
" with command:\n " command "\n")))
;;; GLOBAL EXPORTERS
(defcustom pm-exporter/pandoc
(pm-shell-exporter "pandoc"
:from
'(;; ("json" "\\.json\\'" "JSON native AST" "pandoc %i -f json -t %t -o %o")
("markdown" "\\.md\\'" "pandoc's markdown" "pandoc %i -f markdown -t %t -o %o")
("markdown_strict" "\\.md\\'" "original markdown" "pandoc %i -f markdown_strict -t %t -o %o")
("markdown_phpextra" "\\.md\\'" "PHP markdown" "pandoc %i -f markdown_phpextra -t %t -o %o")
("markdown_phpextra" "\\.md\\'" "github markdown" "pandoc %i -f markdown_phpextra -t %t -o %o")
("textile" "\\.textile\\'" "Textile" "pandoc %i -f textile -t %t -o %o")
("rst" "\\.rst\\'" "reStructuredText" "pandoc %i -f rst -t %t -o %o")
("html" "\\.x?html?\\'" "HTML" "pandoc %i -f html -t %t -o %o")
("doocbook" "\\.xml\\'" "DocBook" "pandoc %i -f doocbook -t %t -o %o")
("mediawiki" "\\.wiki\\'" "MediaWiki" "pandoc %i -f mediawiki -t %t -o %o")
("latex" "\\.tex\\'" "LaTeX" "pandoc %i -f latex -t %t -o %o")
)
:to
'(;; ("json" "json" "JSON version of native AST" "json")
("plain" "txt" "plain text" "plain")
("markdown" "md" "pandoc's extended markdown" "markdown")
("markdown_strict" "md" "original markdown" "markdown_strict")
("markdown_phpextra" "md" "PHP extended markdown" "markdown_phpextra")
("markdown_github" "md" "github extended markdown" "markdown_github")
("rst" "rst" "reStructuredText" "rst")
("html" "html" "XHTML 1" "html")
("html5" "html" "HTML 5" "html5")
("latex" "tex" "LaTeX" "latex")
("beamer" "tex" "LaTeX beamer" "beamer")
("context" "tex" "ConTeXt" "context")
("man" "man" "groff man" "man")
("mediawiki" "wiki" "MediaWiki markup" "mediawiki")
("textile" "textile" "Textile" "textile")
("org" "org" "Emacs Org-Mode" "org")
("texinfo" "info" "GNU Texinfo" "texinfo")
("docbook" "xml" "DocBook XML" "docbook")
("opendocument" "xml" "OpenDocument XML" "opendocument")
("odt" "odt" "OpenOffice text document" "odt")
("docx" "docx" "Word docx" "docx")
("epub" "epub" "EPUB book" "epub")
("epub3" "epub" "EPUB v3" "epub3")
("fb2" "fb" "FictionBook2 e-book" "fb2")
("asciidoc" "txt" "AsciiDoc" "asciidoc")
("slidy" "html" "Slidy HTML slide show" "slidy")
("slideous" "html" "Slideous HTML slide show" "slideous")
("dzslides" "html" "HTML5 slide show" "dzslides")
("s5" "html" "S5 HTML slide show" "s5")
("rtf" "rtf" "rich text format" "rtf")
)
:function 'pm-default-shell-export-function
:sentinel 'pm-default-export-sentinel)
"Pandoc exporter"
:group 'polymode-export
:type 'object)
(provide 'polymode-export)