1 ;;; muse-context.el --- publish entries in ConTeXt or PDF format
3 ;; Copyright (C) 2007 Free Software Foundation, Inc.
5 ;; Author: Jean Magnan de Bornier (jean@bornier.net)
6 ;; Created: 16-Apr-2007
8 ;; Emacs Muse is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published
10 ;; by the Free Software Foundation; either version 2, or (at your
11 ;; option) any later version.
13 ;; This file when loaded allows you to publish .muse files as ConTeXt
14 ;; files or as pdf files, using respectively the "context" and
15 ;; "context-pdf" styles. It is far from being perfect, so any feedback
16 ;; will be welcome and any mistake hopefully fixed.
20 ;; Jean Magnan de Bornier, who based this file on muse-latex.el and
21 ;; made the context, context-pdf, context-slides, and
22 ;; context-slides-pdf Muse publishing styles.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse ConTeXt Publishing
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse-publish
)
36 (defgroup muse-context nil
37 "Rules for marking up a Muse file as a ConTeXt article."
40 (defcustom muse-context-extension
".tex"
41 "Default file extension for publishing ConTeXt files."
45 (defcustom muse-context-pdf-extension
".pdf"
46 "Default file extension for publishing ConTeXt files to PDF."
50 (defcustom muse-context-pdf-program
"texexec --pdf"
51 "The program that is called to generate PDF content from ConTeXt content."
55 (defcustom muse-context-pdf-cruft
'(".pgf" ".tmp" ".tui" ".tuo" ".toc" ".log")
56 "Extensions of files to remove after generating PDF output successfully."
60 (defcustom muse-context-header
61 "\\setupinteraction [state=start]
64 <lisp>(muse-context-setup-bibliography)</lisp>
65 \\setuppublications[]\n
66 \\setuppublicationlist[]\n\\setupcite[]\n
68 \\startalignment[center]
70 {\\tfd <lisp>(muse-publishing-directive \"title\")</lisp>}
72 {\\tfa <lisp>(muse-publishing-directive \"author\")</lisp>}
74 {\\tfa <lisp>(muse-publishing-directive \"date\")</lisp>}
78 <lisp>(and muse-publish-generate-contents
79 (not muse-context-permit-contents-tag)
80 \"\\\\placecontent\n\\\\page[yes]\")</lisp>\n\n"
81 "Header used for publishing ConTeXt files. This may be text or a filename."
85 (defcustom muse-context-footer
"<lisp>(muse-context-bibliography)</lisp>
87 "Footer used for publishing ConTeXt files. This may be text or a filename."
91 (defcustom muse-context-markup-regexps
93 (10000 "\\([0-9]+\\)-\\([0-9]+\\)" 0 "\\1--\\2")
95 ;; be careful of closing quote pairs
96 (10100 "\"'" 0 "\"\\\\-'"))
97 "List of markup regexps for identifying regions in a Muse page.
98 For more on the structure of this list, see `muse-publish-markup-regexps'."
99 :type
'(repeat (choice
100 (list :tag
"Markup rule"
102 (choice regexp symbol
)
104 (choice string function symbol
))
106 :group
'muse-context
)
108 (defcustom muse-context-markup-functions
109 '((table . muse-context-markup-table
))
110 "An alist of style types to custom functions for that kind of text.
111 For more on the structure of this list, see
112 `muse-publish-markup-functions'."
113 :type
'(alist :key-type symbol
:value-type function
)
114 :group
'muse-context
)
116 (defcustom muse-context-markup-strings
117 '((image-with-desc .
"\\placefigure[][]{%3%}{\\externalfigure[%1%.%2%]}")
118 (image .
"\\placefigure[][]{}{\\externalfigure[%s.%s]}")
119 (image-link .
"\\useURL[aa][%s][][%1%] \\from[aa]")
120 (anchor-ref .
"\\goto{%2%}{}[%1%]")
121 (url .
"\\useURL[aa][%s][][%s] \\from[aa]")
122 (url-and-desc .
"\\useURL[bb][%s][][%s]\\from[bb]\\footnote{%1%}")
123 (link .
"\\goto{%2%}[program(%1%)]\\footnote{%1%}")
124 (link-and-anchor .
"\\useexternaldocument[%4%][%4%][] \\at{%3%, page}{}[%4%::%2%]\\footnote{%1%}")
125 (email-addr .
"\\useURL[mail][mailto:%s][][%s]\\from[mail]")
126 (anchor .
"\\reference[%s] ")
128 (comment-begin .
"\\doifmode{comment}{")
130 (rule .
"\\blank[medium]\\hrule\\blank[medium]")
131 (no-break-space .
"~")
132 (enddots .
"\\ldots ")
136 (chapter .
"\\chapter{")
138 (section .
"\\section{")
140 (subsection .
"\\subsection{")
141 (subsection-end .
"}")
142 (subsubsection .
"\\subsubsection{")
143 (subsubsection-end .
"}")
144 (section-other .
"\\subsubsubject{")
145 (section-other-end .
"}")
146 (footnote .
"\\footnote{")
148 (footnotetext .
"\\footnotetext[%d]{")
149 (begin-underline .
"\\underbar{")
150 (end-underline .
"}")
151 (begin-literal .
"\\type{")
153 (begin-emph .
"{\\em ")
155 (begin-more-emph .
"{\\bf ")
156 (end-more-emph .
"}")
157 (begin-most-emph .
"{\\bf {\\em ")
158 (end-most-emph .
"}}")
159 (begin-example .
"\\starttyping")
160 (end-example .
"\\stoptyping")
161 (begin-center .
"\\startalignment[center]\n")
162 (end-center .
"\n\\stopalignment")
163 (begin-quote .
"\\startquotation\n")
164 (end-quote .
"\n\\stopquotation")
165 (begin-cite .
"\\cite[authoryear][")
166 (begin-cite-author .
"\\cite[author][")
167 (begin-cite-year .
"\\cite[year][")
169 (begin-uli .
"\\startitemize\n")
170 (end-uli .
"\n\\stopitemize")
171 (begin-uli-item .
"\\item ")
172 (begin-oli .
"\\startitemize[n]\n")
173 (end-oli .
"\n\\stopitemize")
174 (begin-oli-item .
"\\item ")
175 (begin-dl .
"\\startitemize\n")
176 (end-dl .
"\n\\stopitemize")
177 (begin-ddt .
"\\head ")
179 (begin-verse .
"\\blank[big]")
180 (end-verse-line .
"\\par")
181 (verse-space .
"\\fixedspaces ~~")
182 (end-verse .
"\\blank[big]"))
183 "Strings used for marking up text.
184 These cover the most basic kinds of markup, the handling of which
185 differs little between the various styles."
186 :type
'(alist :key-type symbol
:value-type string
)
187 :group
'muse-context
)
189 (defcustom muse-context-slides-header
193 \\setupinteraction [state=start]
195 \\TitlePage { <lisp>(muse-publishing-directive \"title\")</lisp>
197 \\tfa <lisp>(muse-publishing-directive \"author\")</lisp>
199 \\tfa <lisp>(muse-publishing-directive \"date\")</lisp>}"
200 "Header for publishing a presentation (slides) using ConTeXt.
201 You can use any of the predefined modules, which are available in
202 the tex/context/base of your distribution, provided it has
203 TitlePage and Topic defined. Alternatively, you can use your own
204 style (mystyle) by replacing \"\\usemodule[]\" with \"\\input mystyle\".
206 This may be text or a filename."
208 :group
'muse-context
)
210 (defcustom muse-context-slides-markup-strings
211 '((section .
"\\Topic {")
212 (subsection .
"\\page \n{\\bf ")
213 (subsubsection .
"{\\em "))
214 "Strings used for marking up text in ConTeXt slides."
215 :type
'(alist :key-type symbol
:value-type string
)
216 :group
'muse-context
)
218 (defcustom muse-context-markup-specials-document
219 '((?
\\ .
"\\textbackslash")
220 (?\_ .
"\\textunderscore")
221 (?\
< .
"\\switchtobodyfont[small]")
222 (?\
> .
"\\switchtobodyfont[big]")
232 "A table of characters which must be represented specially.
233 These are applied to the entire document, sans already-escaped
235 :type
'(alist :key-type character
:value-type string
)
236 :group
'muse-context
)
238 (defcustom muse-context-markup-specials-example
240 "A table of characters which must be represented specially.
241 These are applied to <example> regions.
243 With the default interpretation of <example> regions, no specials
245 :type
'(alist :key-type character
:value-type string
)
246 :group
'muse-context
)
248 (defcustom muse-context-markup-specials-literal
250 "A table of characters which must be represented specially.
251 This applies to =monospaced text= and <code> regions."
252 :type
'(alist :key-type character
:value-type string
)
253 :group
'muse-context
)
255 (defcustom muse-context-markup-specials-url
256 '((?
\\ .
"\\textbackslash")
266 "A table of characters which must be represented specially.
267 These are applied to URLs."
268 :type
'(alist :key-type character
:value-type string
)
269 :group
'muse-context
)
271 (defcustom muse-context-markup-specials-image
272 '((?
\\ .
"\\textbackslash") ; cannot find suitable replacement
280 (?\
# .
"\\#") ; cannot find suitable replacement
282 "A table of characters which must be represented specially.
283 These are applied to image filenames."
284 :type
'(alist :key-type character
:value-type string
)
285 :group
'muse-context
)
287 (defun muse-context-decide-specials (context)
288 "Determine the specials to escape, depending on the CONTEXT argument."
289 (cond ((memq context
'(underline emphasis document url-desc verbatim
))
290 muse-context-markup-specials-document
)
292 muse-context-markup-specials-image
)
293 ((memq context
'(email url
))
294 muse-context-markup-specials-url
)
295 ((eq context
'literal
)
296 muse-context-markup-specials-literal
)
297 ((eq context
'example
)
298 muse-context-markup-specials-example
)
299 (t (error "Invalid context argument '%s' in muse-context" context
))))
301 (defun muse-context-markup-table ()
302 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
304 (row-len (car table-info
))
305 (field-list (cdr table-info
)))
307 (muse-insert-markup "\\starttable[|"
308 (mapconcat 'symbol-name
(make-vector row-len
'l
)
309 "|") "|]\n \\HL\n \\VL ")
310 (dolist (fields field-list
)
311 (let ((type (car fields
)))
312 (setq fields
(cdr fields
))
314 (muse-insert-markup ""))
315 (insert (car fields
))
316 (setq fields
(cdr fields
))
317 (dolist (field fields
)
318 (muse-insert-markup " \\VL ")
320 (muse-insert-markup "\\VL\\NR\n \\HL\n \\VL ")
322 (muse-insert-markup " "))))
323 (muse-insert-markup "\\stoptable\n")
324 (while (search-backward "VL \\stoptable" nil t
)
325 (replace-match "stoptable" nil t
)))))
327 (defun muse-context-fixup-dquotes ()
328 "Fixup double quotes."
329 (goto-char (point-min))
331 (while (search-forward "\"" nil t
)
332 (unless (get-text-property (match-beginning 0) 'read-only
)
334 (eq (char-before) ?
\n))
343 (defcustom muse-context-permit-contents-tag nil
344 "If nil, ignore <contents> tags. Otherwise, insert table of contents.
346 Most of the time, it is best to have a table of contents on the
347 first page, with a new page immediately following. To make this
348 work with documents published in both HTML and ConTeXt, we need to
349 ignore the <contents> tag.
351 If you don't agree with this, then set this option to non-nil,
352 and it will do what you expect."
354 :group
'muse-context
)
356 (defun muse-context-fixup-citations ()
357 "Replace semicolons in multi-head citations with colons."
358 (goto-char (point-min))
359 (while (re-search-forward "\\\\cite.?\\[" nil t
)
360 (let ((start (point))
361 (end (re-search-forward "]")))
363 (narrow-to-region start end
)
364 (goto-char (point-min))
365 (while (re-search-forward ";" nil t
)
366 (replace-match ","))))))
368 (defun muse-context-munge-buffer ()
369 (muse-context-fixup-dquotes)
370 (muse-context-fixup-citations)
371 (when (and muse-context-permit-contents-tag
372 muse-publish-generate-contents
)
373 (goto-char (car muse-publish-generate-contents
))
374 (muse-insert-markup "\\placecontent")))
376 (defun muse-context-bibliography ()
378 (goto-char (point-min))
379 (if (re-search-forward "\\\\cite.?\\[" nil t
)
380 "\\completepublications[criterium=all]"
383 (defun muse-context-setup-bibliography ()
385 (goto-char (point-min))
386 (if (re-search-forward "\\\\cite.?\\[" nil t
)
388 "\\usemodule[bibltx]\n\\setupbibtex [database="
389 (muse-publishing-directive "bibsource") "]")
392 (defun muse-context-pdf-browse-file (file)
393 (shell-command (concat "open " file
)))
395 (defun muse-context-pdf-generate (file output-path final-target
)
397 #'muse-publish-transform-output
398 file output-path final-target
"PDF"
400 (lambda (file output-path
)
401 (let* ((fnd (file-name-directory output-path
))
402 (command (format "cd \"%s\"; %s \"%s\""
403 fnd muse-context-pdf-program
404 (file-relative-name file fnd
)))
407 ;; XEmacs can sometimes return a non-number result. We'll err
408 ;; on the side of caution by continuing to attempt to generate
409 ;; the PDF if this happens and treat the final result as
411 (while (and (< times
2)
412 (or (not (numberp result
))
414 ;; table of contents takes 2 passes
416 ;; (muse-replace-regexp-in-string
417 ;; "\\.tex\\'" ".toc" file t t))
419 (setq result
(shell-command command
)
421 (if (or (not (numberp result
))
425 muse-context-pdf-cruft
))
427 (muse-define-style "context"
428 :suffix
'muse-context-extension
429 :regexps
'muse-context-markup-regexps
430 :functions
'muse-context-markup-functions
431 :strings
'muse-context-markup-strings
432 :specials
'muse-context-decide-specials
433 :after
'muse-context-munge-buffer
434 :header
'muse-context-header
435 :footer
'muse-context-footer
438 (muse-derive-style "context-pdf" "context"
439 :final
'muse-context-pdf-generate
440 :browser
'muse-context-pdf-browse-file
441 :link-suffix
'muse-context-pdf-extension
442 :osuffix
'muse-context-pdf-extension
)
444 (muse-derive-style "context-slides" "context"
445 :header
'muse-context-slides-header
446 :strings
'muse-context-slides-markup-strings
)
448 (muse-derive-style "context-slides-pdf" "context-pdf"
449 :header
'muse-context-slides-header
450 :strings
'muse-context-slides-markup-strings
)
452 (provide 'muse-context
)
454 ;;; muse-context.el ends here