muse-latex: Handle special characters in a better way.
[muse-el.git] / lisp / muse-latex.el
blob9cbfaa5ddefa9721f4f4043661dcca4df4275d7c
1 ;;; muse-latex.el --- Publish entries in LaTex or PDF format.
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
10 ;; version.
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 ;; for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Li Daobing (lidaobing AT gmail DOT com) provided CJK support.
28 ;;; Code:
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;; Muse LaTeX Publishing
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 (require 'muse-publish)
38 (defgroup muse-latex nil
39 "Rules for marking up a Muse file as a LaTeX article."
40 :group 'muse-publish)
42 (defcustom muse-latex-extension ".tex"
43 "Default file extension for publishing LaTeX files."
44 :type 'string
45 :group 'muse-latex)
47 (defcustom muse-latex-pdf-extension ".pdf"
48 "Default file extension for publishing LaTeX files to PDF."
49 :type 'string
50 :group 'muse-latex)
52 (defcustom muse-latex-header
53 "\\documentclass{article}
55 \\usepackage[english]{babel}
56 \\usepackage[latin1]{inputenc}
57 \\usepackage[T1]{fontenc}
58 \\usepackage{hyperref}
59 \\usepackage[pdftex]{graphicx}
61 \\begin{document}
63 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
64 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
65 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
67 \\maketitle
69 <lisp>(and muse-publish-generate-contents
70 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
71 "Header used for publishing LaTeX files. This may be text or a filename."
72 :type 'string
73 :group 'muse-latex)
75 (defcustom muse-latex-footer "\n\\end{document}\n"
76 "Footer used for publishing LaTeX files. This may be text or a filename."
77 :type 'string
78 :group 'muse-latex)
80 (defcustom muse-latexcjk-header
81 "\\documentclass{article}
83 \\usepackage{CJK}
84 \\usepackage{indentfirst}
85 \\usepackage[CJKbookmarks=true]{hyperref}
86 \\usepackage[pdftex]{graphicx}
88 \\begin{document}
89 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
91 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
92 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
93 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
95 \\maketitle
97 <lisp>(and muse-publish-generate-contents
98 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
99 "Header used for publishing LaTeX files (CJK). This may be text or a
100 filename."
101 :type 'string
102 :group 'muse-latex)
104 (defcustom muse-latexcjk-footer
105 "\n\\end{CJK*}
106 \\end{document}\n"
107 "Footer used for publishing LaTeX files (CJK). This may be text or a
108 filename."
109 :type 'string
110 :group 'muse-latex)
112 (defcustom muse-latex-markup-regexps
113 `(;; numeric ranges
114 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
116 ;; be careful of closing quote pairs
117 (10100 "\"'" 0 "\"\\\\-'")
119 ;; join together the parts of a list or table
120 (10200 ,(concat
121 "\\\\end{\\(tabular\\|description\\|itemize\\|enumerate\\)}\n+"
122 "\\\\begin{\\1}\\({[^\n}]+}\\)?\n+") 0 ""))
123 "List of markup regexps for identifying regions in a Muse page.
124 For more on the structure of this list, see `muse-publish-markup-regexps'."
125 :type '(repeat (choice
126 (list :tag "Markup rule"
127 integer
128 (choice regexp symbol)
129 integer
130 (choice string function symbol))
131 function))
132 :group 'muse-latex)
134 (defcustom muse-latex-markup-functions
135 '((anchor . muse-latex-markup-anchor)
136 (table . muse-latex-markup-table))
137 "An alist of style types to custom functions for that kind of text.
138 For more on the structure of this list, see
139 `muse-publish-markup-functions'."
140 :type '(alist :key-type symbol :value-type function)
141 :group 'muse-latex)
143 (defcustom muse-latex-markup-strings
144 '((image-with-desc . "\\includegraphics[width=\\textwidth]{%s}")
145 (image-link . "\\includegraphics[width=\\textwidth]{%s}")
146 (url-with-image . "%% %s\n\\includegraphics[width=\\textwidth]{%s}")
147 (url-link . "\\href{%s}{%s}")
148 (internal-link . "\\hyperlink{%s}{%s}")
149 (email-addr . "\\verb|%s|")
150 (emdash . "---")
151 (rule . "\\bigskip")
152 (enddots . "\\ldots{}")
153 (dots . "\\dots{}")
154 (part . "\\part{")
155 (part-end . "}")
156 (chapter . "\\chapter{")
157 (chapter-end . "}")
158 (section . "\\section{")
159 (section-end . "}")
160 (subsection . "\\subsection{")
161 (subsection-end . "}")
162 (subsubsection . "\\subsubsection{")
163 (subsubsection-end . "}")
164 (section-other . "\\paragraph{")
165 (section-other-end . "}")
166 (footnote . "\\footnote{")
167 (footnote-end . "{")
168 (footnotemark . "\\footnotemark[%d]")
169 (footnotetext . "\\footnotetext[%d]{")
170 (footnotetext-end . "}")
171 (begin-underline . "\\underline{")
172 (end-underline . "}")
173 (begin-literal . "\\texttt{")
174 (end-literal . "}")
175 (begin-emph . "\\emph{")
176 (end-emph . "}")
177 (begin-more-emph . "\\textbf{")
178 (end-more-emph . "}")
179 (begin-most-emph . "\\textbf{\\emph{")
180 (end-most-emph . "}}")
181 (begin-verse . "\\begin{verse}\n")
182 (end-verse-line . " \\\\")
183 (verse-space . "~~~~")
184 (end-verse . "\n\\end{verse}")
185 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
186 (end-example . "\\end{verbatim}\n\\end{quote}")
187 (begin-center . "\\begin{center}\n")
188 (end-center . "\n\\end{center}")
189 (begin-quote . "\\begin{quote}\n")
190 (end-quote . "\n\\end{quote}")
191 (begin-uli . "\\begin{itemize}\n\\item ")
192 (end-uli . "\n\\end{itemize}")
193 (begin-oli . "\\begin{enumerate}\n\\item ")
194 (end-oli . "\n\\end{enumerate}")
195 (begin-ddt . "\\begin{description}\n\\item[")
196 (start-dde . "] ")
197 (end-ddt . "\\end{description}"))
198 "Strings used for marking up text.
199 These cover the most basic kinds of markup, the handling of which
200 differs little between the various styles."
201 :type '(alist :key-type symbol :value-type string)
202 :group 'muse-latex)
204 (defcustom muse-latexcjk-encoding-map
205 '((utf-8 . "{UTF8}{song}")
206 (japanese-iso-8bit . "[dnp]{JIS}{min}")
207 (chinese-big5 . "{Bg5}{bsmi}")
208 (mule-utf-8 . "{UTF8}{song}")
209 (chinese-iso-8bit . "{GB}{song}")
210 (chinese-gbk . "{GBK}{song}"))
211 "An alist mapping emacs coding systems to appropriate CJK codings.
212 Use the base name of the coding system (ie, without the -unix)."
213 :type '(alist :key-type coding-system :value-type string)
214 :group 'muse-latex)
216 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
217 "The default Emacs buffer encoding to use in published files.
218 This will be used if no special characters are found."
219 :type 'string
220 :group 'muse-latex)
222 (defun muse-latexcjk-encoding ()
223 (when (boundp 'buffer-file-coding-system)
224 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
226 (defun muse-latexcjk-transform-content-type (content-type)
227 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
228 system to an associated CJK coding system."
229 (let ((match (and (fboundp 'coding-system-base)
230 (assoc (coding-system-base content-type)
231 muse-latexcjk-encoding-map))))
232 (if match
233 (cdr match)
234 muse-latexcjk-encoding-default)))
236 (defcustom muse-latex-markup-specials
237 '((?\\ . "\\\\")
238 (?\$ . "\\$")
239 (?\% . "\\%")
240 (?\_ . "\\_")
241 (?\{ . "\\{")
242 (?\} . "\\}")
243 (?\& . "\\&")
244 (?\# . "\\#"))
245 "A table of characters which must be represented specially."
246 :type '(alist :key-type character :value-type string)
247 :group 'muse-latex)
249 (defun muse-latex-insert-anchor (anchor)
250 "Insert an anchor, either around the word at point, or within a tag."
251 (skip-chars-forward muse-regexp-space)
252 (if (looking-at "<\\([^ />]+\\)>")
253 (let ((tag (match-string 1)))
254 (goto-char (match-end 0))
255 (insert "\\hypertarget{" anchor "}{")
256 (or (and (search-forward (format "</%s>" tag)
257 (muse-line-end-position) t)
258 (goto-char (match-beginning 0)))
259 (forward-word 1))
260 (insert "}"))
261 (insert "\\hypertarget{" anchor "}{")
262 (forward-word 1)
263 (insert "}")))
265 (defun muse-latex-markup-anchor ()
266 (save-match-data
267 (muse-latex-insert-anchor (match-string 1))) "")
269 (defun muse-latex-markup-table ()
270 (let* ((str (prog1
271 (match-string 1)
272 (delete-region (match-beginning 0) (match-end 0))))
273 (fields (split-string str "\\s-*|+\\s-*"))
274 ;; FIXME: `type' isn't used
275 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
276 (length (match-string 1 str)))))
277 (insert "\\begin{tabular}{" (make-string (length fields) ?l) "}\n")
278 (insert (mapconcat 'identity fields " & "))
279 (insert " \\\\\n\\end{tabular}")))
281 (defun muse-latex-fixup-dquotes ()
282 "Fixup double quotes."
283 (let ((open t))
284 (while (search-forward "\"" nil t)
285 (unless (get-text-property (match-beginning 0) 'read-only)
286 (if (and (bolp) (eq (char-before) ?\n))
287 (setq open t))
288 (if open
289 (progn
290 (replace-match "``")
291 (setq open nil))
292 (replace-match "''")
293 (setq open t))))))
295 (defun muse-latex-finalize-buffer ()
296 (goto-char (point-min))
297 (muse-latex-fixup-dquotes))
299 (defun muse-latex-pdf-browse-file (file)
300 (shell-command (concat "open " file)))
302 (defun muse-latex-pdf-generate (file output-path final-target)
303 (muse-publish-transform-output
304 file output-path final-target "PDF"
305 (function
306 (lambda (file output-path)
307 (let ((command (format "cd \"%s\"; pdflatex \"%s\"; pdflatex \"%s\""
308 (file-name-directory output-path) file file)))
309 (shell-command command))))
310 ".aux" ".toc" ".out" ".log"))
312 (unless (assoc "latex" muse-publishing-styles)
313 (muse-define-style "latex"
314 :suffix 'muse-latex-extension
315 :regexps 'muse-latex-markup-regexps
316 :functions 'muse-latex-markup-functions
317 :strings 'muse-latex-markup-strings
318 :specials 'muse-latex-markup-specials
319 :after 'muse-latex-finalize-buffer
320 :header 'muse-latex-header
321 :footer 'muse-latex-footer
322 :browser 'find-file)
324 (muse-derive-style "pdf" "latex"
325 :final 'muse-latex-pdf-generate
326 :browser 'muse-latex-pdf-browse-file
327 :link-suffix 'muse-latex-pdf-extension
328 :osuffix 'muse-latex-pdf-extension)
330 (muse-derive-style "latexcjk" "latex"
331 :header 'muse-latexcjk-header
332 :footer 'muse-latexcjk-footer)
334 (muse-derive-style "pdfcjk" "latexcjk"
335 :final 'muse-latex-pdf-generate
336 :browser 'muse-latex-pdf-browse-file
337 :link-suffix 'muse-latex-pdf-extension
338 :osuffix 'muse-latex-pdf-extension))
340 (provide 'muse-latex)
342 ;;; muse-latex.el ends here