-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathox-word.el
215 lines (181 loc) · 7.44 KB
/
ox-word.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
;;; ox-word.el --- Collection of functions to export to MS Word documents
;; Collection of ideas to get Word documents from org-documents
;;
;;; Commentary:
;;
;; * Using pandoc via LaTeX
;; In this approach we export the org-document to latex, and use pandoc to
;; convert to Word. This leverages Pandoc's ability to get pretty reasonable
;; bibliographies and citations using CSL. The Pandoc I have tested with does
;; not do a good job with cross-references (figures and tables are linked ,but
;; not numbered), so here we post-process the tex file to hard code figure and
;; table numbers in it, and to replace references to them in the text. This has
;; the benefit that they are numbered, but they are not links in the word
;; document, so they will not be updatable in the Word document.
;; Use #+PANDOC-CSL: /full/path/to/some/style.csl to set
;; the bibliography style. You can get the csl files from
;; https://www.zotero.org/styles
;;; Code:
(defun ox-export-get-pandoc-version ()
"Returns the major version of pandoc."
(string-to-number
(substring (shell-command-to-string "pandoc --version") 7 8)))
(defun ox-export-call-pandoc-tex-to-docx (biboption csl tex-file docx-file)
"Run pandoc to convert the exported tex file to docx."
(let* ((pandoc-version (ox-export-get-pandoc-version))
(pandoc-command
(if (>= pandoc-version 2)
"pandoc -s %s%s\"%s\" --to=docx+smart -o \"%s\""
"pandoc -s -S %s%s\"%s\" -o \"%s\"")))
(shell-command (format pandoc-command biboption csl tex-file docx-file))))
(defun ox-export-call-pandoc-tex-to-html (biboption csl tex-file html-file)
"Run pandoc to convert the exported tex file to html."
(let* ((pandoc-version (ox-export-get-pandoc-version))
(pandoc-command
(if (>= pandoc-version 2)
"pandoc -s %s%s\"%s\" --to=html+smart -o \"%s\""
"pandoc -s -S %s%s\"%s\" -o \"%s\"")))
(shell-command (format pandoc-command biboption csl tex-file html-file))))
(defun ox-export-via-latex-pandoc-to-docx-and-open (&optional async subtreep visible-only body-only options)
"Export the current org file as a docx via LaTeX."
(interactive)
(let* ((bibfiles (mapcar 'expand-file-name (org-ref-find-bibliography)))
(temp-bib)
(bibtex-entries)
biboption
csl
;; this is probably a full path
(current-file (buffer-file-name))
(basename (file-name-sans-extension current-file))
(tex-file (concat basename ".tex"))
(docx-file (concat basename ".docx")))
(save-buffer)
;; I make a temp bibfile because my big one causes pandoc to choke. This
;; should only create a file with the required entries.
(when bibfiles
(setq bibtex-entries (let* ((bibtex-files bibfiles)
(keys (reverse (org-ref-get-bibtex-keys)))
(bibtex-entry-kill-ring-max (length keys))
(bibtex-entry-kill-ring '()))
(save-window-excursion
(cl-loop for key in keys
do
(bibtex-search-entry key t)
(bibtex-kill-entry t)))
(mapconcat
'identity
bibtex-entry-kill-ring
"\n\n"))
temp-bib (make-temp-file "ox-word-" nil ".bib")
biboption (format " --bibliography=%s " temp-bib))
(with-temp-file temp-bib
(insert bibtex-entries)))
(setq csl (cdr (assoc "PANDOC-CSL"
(org-element-map (org-element-parse-buffer) 'keyword
(lambda (key) (cons
(org-element-property :key key)
(org-element-property :value key)))))))
(if csl (setq csl (format " --csl=%s " csl))
(setq csl " "))
(org-latex-export-to-latex async subtreep visible-only body-only options)
;; Now we do some post-processing on the tex-file
;; Tables first.
(let* ((table-regex "\\\\begin{table}.*
\\\\caption{\\(?1:\\(?2:.*\\)\\\\label{\\(?3:.*\\)}\\)}")
(buf (find-file-noselect tex-file))
(i 0)
labels)
(with-current-buffer buf
(goto-char (point-min))
(while (re-search-forward table-regex nil t)
(incf i)
(push (cons (match-string 3) i) labels)
(replace-match (format "Table %d. \\2" i) nil nil nil 1))
;; Now replace the refs.
(goto-char (point-min))
(while (re-search-forward "\\\\ref{\\(?1:.*?\\)}" nil t)
(when (cdr (assoc (match-string 1) labels))
(replace-match (format "%d" (cdr (assoc (match-string 1) labels))))))
(save-buffer))
(message "done with tables."))
;; Now figures
(let* ((fig-regex "\\includegraphics.*
\\\\caption{\\(?3:\\(?1:.*\\)\\\\label{\\(?2:.*\\)}\\)}")
(buf (find-file-noselect tex-file))
(i 0)
labels)
(with-current-buffer buf
(goto-char (point-min))
(while (re-search-forward fig-regex nil t)
(incf i)
(push (cons (match-string 2) i) labels)
(replace-match (format "Figure %d. \\1." i) nil nil nil 3))
;; Now replace the refs.
(goto-char (point-min))
(while (re-search-forward "\\\\ref{\\(?1:.*?\\)}" nil t)
(when (cdr (assoc (match-string 1) labels))
(replace-match (format "%d" (cdr (assoc (match-string 1) labels))))))
(save-buffer)
(kill-buffer buf)))
(when (file-exists-p docx-file) (delete-file docx-file))
(ox-export-call-pandoc-tex-to-docx biboption csl tex-file docx-file)
(when (file-exists-p temp-bib)
(delete-file temp-bib))
(org-open-file docx-file '(16))))
(defun ox-export-via-latex-pandoc-to-html-and-open (&optional async subtreep visible-only body-only options)
"Export the current org file as a html via LaTeX."
(interactive)
(let* ((bibfile (expand-file-name (car (org-ref-find-bibliography))))
(temp-bib)
(bibtex-entries)
biboption
csl
;; this is probably a full path
(current-file (buffer-file-name))
(basename (file-name-sans-extension current-file))
(tex-file (concat basename ".tex"))
(html-file (concat basename ".html")))
(save-buffer)
;; I make a temp bibfile because my big one causes pandoc to choke. This
;; should only create a file with the required entries.
(when bibfile
(setq bibtex-entries (let* ((bibtex-files (org-ref-find-bibliography))
(keys (reverse (org-ref-get-bibtex-keys)))
(bibtex-entry-kill-ring-max (length keys))
(bibtex-entry-kill-ring '()))
(save-window-excursion
(cl-loop for key in keys
do
(bibtex-search-entry key t)
(bibtex-kill-entry t)))
(mapconcat
'identity
bibtex-entry-kill-ring
"\n\n"))
temp-bib (make-temp-file "ox-html-" nil ".bib")
biboption (format " --bibliography=%s " temp-bib))
(with-temp-file temp-bib
(insert bibtex-entries)))
(setq csl (cdr (assoc "PANDOC-CSL"
(org-element-map (org-element-parse-buffer) 'keyword
(lambda (key) (cons
(org-element-property :key key)
(org-element-property :value key)))))))
(if csl (setq csl (format " --csl=%s " csl))
(setq csl " "))
(org-latex-export-to-latex async subtreep visible-only body-only options)
(when (file-exists-p html-file) (delete-file html-file))
(ox-export-call-pandoc-tex-to-html biboption csl tex-file html-file)
(when (file-exists-p temp-bib)
(delete-file temp-bib))
(browse-url html-file)))
(org-export-define-derived-backend 'MSWord 'latex
:menu-entry
'(?w "Export to MS Word"
((?p "via Pandoc/LaTeX" ox-export-via-latex-pandoc-to-docx-and-open))))
(org-export-define-derived-backend 'pandoc-html 'latex
:menu-entry
'(?h "Export to HTML"
((?p "via Pandoc/LaTeX" ox-export-via-latex-pandoc-to-html-and-open))))
(provide 'ox-word)
;;; ox-word.el ends here