Fix link-following bug with invalid relative links
[muse-el.git] / lisp / muse-latex.el
blob038a323f2ca30a34b7698d1f98941bf66da8e76e
1 ;;; muse-latex.el --- publish entries in LaTex or PDF format
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; 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 ;; Trent Buck (trentbuck AT gmail DOT com) gave valuable advice for
29 ;; how to treat LaTeX specials and the like.
31 ;; Matthias Kegelmann (mathias DOT kegelmann AT sdm DOT de) provided a
32 ;; scenario where we would need to respect the <contents> tag.
34 ;;; Code:
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;; Muse LaTeX Publishing
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 (require 'muse-publish)
44 (defgroup muse-latex nil
45 "Rules for marking up a Muse file as a LaTeX article."
46 :group 'muse-publish)
48 (defcustom muse-latex-extension ".tex"
49 "Default file extension for publishing LaTeX files."
50 :type 'string
51 :group 'muse-latex)
53 (defcustom muse-latex-pdf-extension ".pdf"
54 "Default file extension for publishing LaTeX files to PDF."
55 :type 'string
56 :group 'muse-latex)
58 (defcustom muse-latex-header
59 "\\documentclass{article}
61 \\usepackage[english]{babel}
62 \\usepackage[latin1]{inputenc}
63 \\usepackage[T1]{fontenc}
64 \\usepackage{hyperref}
65 \\usepackage[pdftex]{graphicx}
67 \\newcommand{\\comment}[1]{}
69 \\begin{document}
71 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
72 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
73 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
75 \\maketitle
77 <lisp>(and muse-publish-generate-contents
78 (not muse-latex-permit-contents-tag)
79 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
80 "Header used for publishing LaTeX files. This may be text or a filename."
81 :type 'string
82 :group 'muse-latex)
84 (defcustom muse-latex-footer "\n\\end{document}\n"
85 "Footer used for publishing LaTeX files. This may be text or a filename."
86 :type 'string
87 :group 'muse-latex)
89 (defcustom muse-latexcjk-header
90 "\\documentclass{article}
92 \\usepackage{CJK}
93 \\usepackage{indentfirst}
94 \\usepackage[CJKbookmarks=true]{hyperref}
95 \\usepackage[pdftex]{graphicx}
97 \\newcommand{\\comment}[1]{}
99 \\begin{document}
100 \\begin{CJK*}<lisp>(muse-latexcjk-encoding)</lisp>
102 \\title{<lisp>(muse-publishing-directive \"title\")</lisp>}
103 \\author{<lisp>(muse-publishing-directive \"author\")</lisp>}
104 \\date{<lisp>(muse-publishing-directive \"date\")</lisp>}
106 \\maketitle
108 <lisp>(and muse-publish-generate-contents
109 (not muse-latex-permit-contents-tag)
110 \"\\\\tableofcontents\n\\\\newpage\")</lisp>\n\n"
111 "Header used for publishing LaTeX files (CJK). This may be text or a
112 filename."
113 :type 'string
114 :group 'muse-latex)
116 (defcustom muse-latexcjk-footer
117 "\n\\end{CJK*}
118 \\end{document}\n"
119 "Footer used for publishing LaTeX files (CJK). This may be text or a
120 filename."
121 :type 'string
122 :group 'muse-latex)
124 (defcustom muse-latex-markup-regexps
125 `(;; numeric ranges
126 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
128 ;; be careful of closing quote pairs
129 (10100 "\"'" 0 "\"\\\\-'"))
130 "List of markup regexps for identifying regions in a Muse page.
131 For more on the structure of this list, see `muse-publish-markup-regexps'."
132 :type '(repeat (choice
133 (list :tag "Markup rule"
134 integer
135 (choice regexp symbol)
136 integer
137 (choice string function symbol))
138 function))
139 :group 'muse-latex)
141 (defcustom muse-latex-markup-functions
142 '((table . muse-latex-markup-table))
143 "An alist of style types to custom functions for that kind of text.
144 For more on the structure of this list, see
145 `muse-publish-markup-functions'."
146 :type '(alist :key-type symbol :value-type function)
147 :group 'muse-latex)
149 (defcustom muse-latex-markup-strings
150 '((image-with-desc . "\\begin{figure}[h]
151 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
152 \\caption{%s}
153 \\end{figure}")
154 (image . "\\begin{figure}[h]
155 \\centering\\includegraphics[width=0.75\\textwidth]{%s.%s}
156 \\end{figure}")
157 (image-link . "%% %s
158 \\includegraphics[width=0.75\\textwidth]{%s.%s}")
159 (anchor-ref . "\\ref{%s}")
160 (url . "\\url{%s}")
161 (url-and-desc . "\\href{%s}{%s}\\footnote{%1%}")
162 (link . "\\href{%s}{%s}\\footnote{%1%}")
163 (link-and-anchor . "\\href{%1%}{%3%}\\footnote{%1%}")
164 (email-addr . "\\verb|%s|")
165 (anchor . "\\label{%s}")
166 (emdash . "---")
167 (comment-begin . "\\comment{")
168 (comment-end . "}")
169 (rule . "\\bigskip")
170 (no-break-space . "~")
171 (enddots . "\\ldots{}")
172 (dots . "\\dots{}")
173 (part . "\\part{")
174 (part-end . "}")
175 (chapter . "\\chapter{")
176 (chapter-end . "}")
177 (section . "\\section{")
178 (section-end . "}")
179 (subsection . "\\subsection{")
180 (subsection-end . "}")
181 (subsubsection . "\\subsubsection{")
182 (subsubsection-end . "}")
183 (section-other . "\\paragraph{")
184 (section-other-end . "}")
185 (footnote . "\\footnote{")
186 (footnote-end . "}")
187 (footnotetext . "\\footnotetext[%d]{")
188 (begin-underline . "\\underline{")
189 (end-underline . "}")
190 (begin-literal . "\\texttt{")
191 (end-literal . "}")
192 (begin-emph . "\\emph{")
193 (end-emph . "}")
194 (begin-more-emph . "\\textbf{")
195 (end-more-emph . "}")
196 (begin-most-emph . "\\textbf{\\emph{")
197 (end-most-emph . "}}")
198 (begin-verse . "\\begin{verse}\n")
199 (end-verse-line . " \\\\")
200 (verse-space . "~~~~")
201 (end-verse . "\n\\end{verse}")
202 (begin-example . "\\begin{quote}\n\\begin{verbatim}")
203 (end-example . "\\end{verbatim}\n\\end{quote}")
204 (begin-center . "\\begin{center}\n")
205 (end-center . "\n\\end{center}")
206 (begin-quote . "\\begin{quote}\n")
207 (end-quote . "\n\\end{quote}")
208 (begin-uli . "\\begin{itemize}\n")
209 (end-uli . "\n\\end{itemize}")
210 (begin-uli-item . "\\item ")
211 (begin-oli . "\\begin{enumerate}\n")
212 (end-oli . "\n\\end{enumerate}")
213 (begin-oli-item . "\\item ")
214 (begin-dl . "\\begin{description}\n")
215 (end-dl . "\n\\end{description}")
216 (begin-ddt . "\\item[")
217 (end-ddt . "] "))
218 "Strings used for marking up text.
219 These cover the most basic kinds of markup, the handling of which
220 differs little between the various styles."
221 :type '(alist :key-type symbol :value-type string)
222 :group 'muse-latex)
224 (defcustom muse-latexcjk-encoding-map
225 '((utf-8 . "{UTF8}{song}")
226 (japanese-iso-8bit . "[dnp]{JIS}{min}")
227 (chinese-big5 . "{Bg5}{bsmi}")
228 (mule-utf-8 . "{UTF8}{song}")
229 (chinese-iso-8bit . "{GB}{song}")
230 (chinese-gbk . "{GBK}{song}"))
231 "An alist mapping emacs coding systems to appropriate CJK codings.
232 Use the base name of the coding system (ie, without the -unix)."
233 :type '(alist :key-type coding-system :value-type string)
234 :group 'muse-latex)
236 (defcustom muse-latexcjk-encoding-default "{GB}{song}"
237 "The default Emacs buffer encoding to use in published files.
238 This will be used if no special characters are found."
239 :type 'string
240 :group 'muse-latex)
242 (defun muse-latexcjk-encoding ()
243 (when (boundp 'buffer-file-coding-system)
244 (muse-latexcjk-transform-content-type buffer-file-coding-system)))
246 (defun muse-latexcjk-transform-content-type (content-type)
247 "Using `muse-cjklatex-encoding-map', try and resolve an emacs coding
248 system to an associated CJK coding system."
249 (let ((match (and (fboundp 'coding-system-base)
250 (assoc (coding-system-base content-type)
251 muse-latexcjk-encoding-map))))
252 (if match
253 (cdr match)
254 muse-latexcjk-encoding-default)))
256 (defcustom muse-latex-markup-specials-document
257 '((?\\ . "\\textbackslash{}")
258 (?\_ . "\\textunderscore{}")
259 (?\< . "\\textless{}")
260 (?\> . "\\textgreater{}")
261 (?^ . "\\^{}")
262 (?\~ . "\\~{}")
263 (?\@ . "\\@")
264 (?\$ . "\\$")
265 (?\% . "\\%")
266 (?\{ . "\\{")
267 (?\} . "\\}")
268 (?\& . "\\&")
269 (?\# . "\\#"))
270 "A table of characters which must be represented specially.
271 These are applied to the entire document, sans already-escaped
272 regions."
273 :type '(alist :key-type character :value-type string)
274 :group 'muse-latex)
276 (defcustom muse-latex-markup-specials-example
278 "A table of characters which must be represented specially.
279 These are applied to <example> regions.
281 With the default interpretation of <example> regions, no specials
282 need to be escaped."
283 :type '(alist :key-type character :value-type string)
284 :group 'muse-latex)
286 (defcustom muse-latex-markup-specials-literal
287 '((?\n . "\\\n")
288 (?_ . "\\textunderscore{}")
289 (?\< . "\\textless{}")
290 (?\> . "\\textgreater{}")
291 (?^ . "\\^{}")
292 (?\~ . "\\~{}")
293 (?\$ . "\\$")
294 (?\% . "\\%")
295 (?\{ . "\\{")
296 (?\} . "\\}")
297 (?\& . "\\&")
298 (?\# . "\\#"))
299 "A table of characters which must be represented specially.
300 This applies to =monospaced text= and <code> regions."
301 :type '(alist :key-type character :value-type string)
302 :group 'muse-latex)
304 (defcustom muse-latex-markup-specials-url
305 '((?\\ . "\\textbackslash{}")
306 (?\_ . "\\_")
307 (?\< . "\\<")
308 (?\> . "\\>")
309 (?\$ . "\\$")
310 (?\% . "\\%")
311 (?\{ . "\\{")
312 (?\} . "\\}")
313 (?\& . "\\&")
314 (?\# . "\\#"))
315 "A table of characters which must be represented specially.
316 These are applied to URLs."
317 :type '(alist :key-type character :value-type string)
318 :group 'muse-latex)
320 (defcustom muse-latex-markup-specials-image
321 '((?\\ . "\\textbackslash{}") ; cannot find suitable replacement
322 (?\< . "\\<")
323 (?\> . "\\>")
324 (?\$ . "\\$")
325 (?\% . "\\%")
326 (?\{ . "\\{")
327 (?\} . "\\}")
328 (?\& . "\\&")
329 (?\# . "\\#") ; cannot find suitable replacement
331 "A table of characters which must be represented specially.
332 These are applied to image filenames."
333 :type '(alist :key-type character :value-type string)
334 :group 'muse-latex)
336 (defun muse-latex-decide-specials (context)
337 "Determine the specials to escape, depending on CONTEXT."
338 (cond ((memq context '(underline emphasis document url-desc verbatim))
339 muse-latex-markup-specials-document)
340 ((eq context 'image)
341 muse-latex-markup-specials-image)
342 ((memq context '(email url))
343 muse-latex-markup-specials-url)
344 ((eq context 'literal)
345 muse-latex-markup-specials-literal)
346 ((eq context 'example)
347 muse-latex-markup-specials-example)
348 (t (error "Invalid context '%s' in muse-latex" context))))
350 (defun muse-latex-markup-table ()
351 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
352 (match-end 0)))
353 (row-len (car table-info))
354 (field-list (cdr table-info)))
355 (when table-info
356 (muse-insert-markup "\\begin{tabular}{" (make-string row-len ?l) "}\n")
357 (dolist (fields field-list)
358 (let ((type (car fields)))
359 (setq fields (cdr fields))
360 (when (= type 3)
361 (muse-insert-markup "\\hline\n"))
362 (insert (car fields))
363 (setq fields (cdr fields))
364 (dolist (field fields)
365 (muse-insert-markup " & ")
366 (insert field))
367 (muse-insert-markup " \\\\\n")
368 (when (= type 2)
369 (muse-insert-markup "\\hline\n"))))
370 (muse-insert-markup "\\end{tabular}"))))
372 (defun muse-latex-fixup-dquotes ()
373 "Fixup double quotes."
374 (goto-char (point-min))
375 (let ((open t))
376 (while (search-forward "\"" nil t)
377 (unless (get-text-property (match-beginning 0) 'read-only)
378 (when (or (bobp)
379 (eq (char-before) ?\n))
380 (setq open t))
381 (if open
382 (progn
383 (replace-match "``")
384 (setq open nil))
385 (replace-match "''")
386 (setq open t))))))
388 (defcustom muse-latex-permit-contents-tag nil
389 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
391 Most of the time, it is best to have a table of contents on the
392 first page, with a new page immediately following. To make this
393 work with documents published in both HTML and LaTeX, we need to
394 ignore the <contents> tag.
396 If you don't agree with this, then set this option to non-nil,
397 and it will do what you expect."
398 :type 'boolean
399 :group 'muse-latex)
401 (defun muse-latex-finalize-buffer ()
402 (muse-latex-fixup-dquotes)
403 (when (and muse-latex-permit-contents-tag
404 muse-publish-generate-contents)
405 (goto-char (car muse-publish-generate-contents))
406 (muse-insert-markup "\\tableofcontents")))
408 (defun muse-latex-pdf-browse-file (file)
409 (shell-command (concat "open " file)))
411 (defun muse-latex-pdf-generate (file output-path final-target)
412 (muse-publish-transform-output
413 file output-path final-target "PDF"
414 (function
415 (lambda (file output-path)
416 (let ((command (format "cd \"%s\"; pdflatex \"%s\""
417 (file-name-directory output-path) file))
418 (times 0)
419 result)
420 ;; XEmacs can sometimes return a non-number result. We'll err
421 ;; on the side of caution by continuing to attempt to generate
422 ;; the PDF if this happens and treat the final result as
423 ;; successful.
424 (while (and (< times 2)
425 (or (not (numberp result))
426 (not (eq result 0))
427 ;; table of contents takes 2 passes
428 (file-readable-p
429 (muse-replace-regexp-in-string
430 "\\.tex\\'" ".toc" file t t))))
431 (setq result (shell-command command)
432 times (1+ times)))
433 (if (or (not (numberp result))
434 (eq result 0))
436 nil))))
437 ".aux" ".toc" ".out" ".log"))
439 (unless (assoc "latex" muse-publishing-styles)
440 (muse-define-style "latex"
441 :suffix 'muse-latex-extension
442 :regexps 'muse-latex-markup-regexps
443 :functions 'muse-latex-markup-functions
444 :strings 'muse-latex-markup-strings
445 :specials 'muse-latex-decide-specials
446 :after 'muse-latex-finalize-buffer
447 :header 'muse-latex-header
448 :footer 'muse-latex-footer
449 :browser 'find-file)
451 (muse-derive-style "pdf" "latex"
452 :final 'muse-latex-pdf-generate
453 :browser 'muse-latex-pdf-browse-file
454 :link-suffix 'muse-latex-pdf-extension
455 :osuffix 'muse-latex-pdf-extension)
457 (muse-derive-style "latexcjk" "latex"
458 :header 'muse-latexcjk-header
459 :footer 'muse-latexcjk-footer)
461 (muse-derive-style "pdfcjk" "latexcjk"
462 :final 'muse-latex-pdf-generate
463 :browser 'muse-latex-pdf-browse-file
464 :link-suffix 'muse-latex-pdf-extension
465 :osuffix 'muse-latex-pdf-extension))
467 (provide 'muse-latex)
469 ;;; muse-latex.el ends here