muse-texinfo: Escape commas in URLs.
[muse-el.git] / lisp / muse-texinfo.el
blobb49bcda2ea26752de51e44d7178f988a2aaf8dbc
1 ;;; muse-texinfo.el --- publish entries to Texinfo format or PDF
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 ;;; Code:
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Texinfo Publishing
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse-publish)
35 (require 'muse-latex)
36 (require 'texnfo-upd)
38 (defgroup muse-texinfo nil
39 "Rules for marking up a Muse file as a Texinfo article."
40 :group 'muse-publish)
42 (defcustom muse-texinfo-process-natively t
43 "If non-nil, use the Emacs `texinfmt' module to make Info files."
44 :type 'boolean
45 :require 'texinfmt
46 :group 'muse-texinfo)
48 (defcustom muse-texinfo-extension ".texi"
49 "Default file extension for publishing Texinfo files."
50 :type 'string
51 :group 'muse-texinfo)
53 (defcustom muse-texinfo-info-extension ".info"
54 "Default file extension for publishing Info files."
55 :type 'string
56 :group 'muse-texinfo)
58 (defcustom muse-texinfo-pdf-extension ".pdf"
59 "Default file extension for publishing PDF files."
60 :type 'string
61 :group 'muse-texinfo)
63 (defcustom muse-texinfo-header
64 "\\input texinfo @c -*-texinfo-*-
66 @setfilename <lisp>(concat (muse-page-name) \".info\")</lisp>
67 @settitle <lisp>(muse-publishing-directive \"title\")</lisp>
69 @documentencoding iso-8859-1
71 @iftex
72 @finalout
73 @end iftex
75 @titlepage
76 @title <lisp>(muse-publishing-directive \"title\")</lisp>
77 @author <lisp>(muse-publishing-directive \"author\")</lisp>
78 @end titlepage
80 <lisp>(and muse-publish-generate-contents \"@contents\")</lisp>
82 @node Top, Overview, , (dir)
83 @top Overview
84 @c Page published by Emacs Muse begins here\n\n"
85 "Text to prepend to a Muse page being published as Texinfo.
86 This may be text or a filename.
87 It may contain <lisp> markup tags."
88 :type 'string
89 :group 'muse-texinfo)
91 (defcustom muse-texinfo-footer
92 "\n@c Page published by Emacs Muse ends here
93 @bye\n"
94 "Text to append to a Muse page being published as Texinfo.
95 This may be text or a filename.
96 It may contain <lisp> markup tags."
97 :type 'string
98 :group 'muse-texinfo)
100 (defcustom muse-texinfo-markup-regexps
101 `(;; join together the parts of a list or table
102 (10000 ,(concat "@end \\(\\(multi\\)?table\\|itemize\\|enumerate\\)"
103 "\n\\{1,2\\}"
104 "@\\1.*\n+")
105 0 ""))
106 "List of markup rules for publishing a Muse page to Texinfo.
107 For more on the structure of this list, see `muse-publish-markup-regexps'."
108 :type '(repeat (choice
109 (list :tag "Markup rule"
110 integer
111 (choice regexp symbol)
112 integer
113 (choice string function symbol))
114 function))
115 :group 'muse-texinfo)
117 (defcustom muse-texinfo-markup-functions
118 '((table . muse-texinfo-markup-table))
119 "An alist of style types to custom functions for that kind of text.
120 For more on the structure of this list, see
121 `muse-publish-markup-functions'."
122 :type '(alist :key-type symbol :value-type function)
123 :group 'muse-texinfo)
125 (defcustom muse-texinfo-markup-strings
126 '((image-with-desc . "@image{%s}")
127 (image-link . "@image{%s}")
128 (url-with-image . "@uref{%s, %s}")
129 (anchor-ref . "@ref{%s, %s}")
130 (url . "@uref{%s, %s}")
131 (link . "@ref{Top, , Overview, %s, %s}")
132 (link-and-anchor . "@ref{%2%, , %2%, %1%, %3%}")
133 (email-addr . "@email{%s}")
134 (anchor . "@anchor{%s} ")
135 (emdash . "---")
136 (comment-begin . "@ignore\n")
137 (comment-end . "\n@end ignore\n")
138 (rule . "@sp 1")
139 (enddots . "@enddots{}")
140 (dots . "@dots{}")
141 (section . "@chapter ")
142 (subsection . "@section ")
143 (subsubsection . "@subsection ")
144 (section-other . "@subsubheading ")
145 (footnote . "@footnote{")
146 (footnote-end . "}")
147 (begin-underline . "_")
148 (end-underline . "_")
149 (begin-literal . "@samp{")
150 (end-literal . "}")
151 (begin-emph . "@emph{")
152 (end-emph . "}")
153 (begin-more-emph . "@strong{")
154 (end-more-emph . "}")
155 (begin-most-emph . "@strong{@emph{")
156 (end-most-emph . "}}")
157 (begin-verse . "@display\n")
158 (end-verse-line . "")
159 (verse-space . "@ @ ")
160 (end-verse . "\n@end display")
161 (begin-example . "@example")
162 (end-example . "@end example")
163 (begin-center . "@quotation\n")
164 (end-center . "\n@end quotation")
165 (begin-quote . "@quotation\n")
166 (end-quote . "\n@end quotation")
167 (begin-uli . "@itemize @bullet\n@item\n")
168 (end-uli . "\n@end itemize")
169 (begin-oli . "@enumerate\n@item\n")
170 (end-oli . "\n@end enumerate")
171 (begin-ddt . "@table @strong\n@item ")
172 (start-dde . "\n")
173 (end-ddt . "\n@end table"))
174 "Strings used for marking up text.
175 These cover the most basic kinds of markup, the handling of which
176 differs little between the various styles."
177 :type '(alist :key-type symbol :value-type string)
178 :group 'muse-texinfo)
180 (defcustom muse-texinfo-markup-specials
181 '((?@ . "@@")
182 (?{ . "@{")
183 (?} . "@}"))
184 "A table of characters which must be represented specially."
185 :type '(alist :key-type character :value-type string)
186 :group 'muse-texinfo)
188 (defcustom muse-texinfo-markup-specials-url
189 '((?@ . "@@")
190 (?{ . "@{")
191 (?} . "@}")
192 (?, . "@comma{}"))
193 "A table of characters which must be represented specially.
194 These are applied to URLs."
195 :type '(alist :key-type character :value-type string)
196 :group 'muse-texinfo)
198 (defun muse-texinfo-decide-specials (context)
199 "Determine the specials to escape, depending on CONTEXT."
200 (cond ((eq context 'url)
201 muse-texinfo-markup-specials-url)
202 (t muse-texinfo-markup-specials)))
204 (defun muse-texinfo-markup-table ()
205 (let* ((table-info (muse-publish-table-fields (match-beginning 0)
206 (match-end 0)))
207 (row-len (car table-info))
208 (field-list (cdr table-info)))
209 (muse-insert-markup "@multitable @columnfractions")
210 (dotimes (field row-len)
211 (muse-insert-markup " " (number-to-string (/ 1.0 row-len))))
212 (dolist (fields field-list)
213 (let ((type (car fields)))
214 (setq fields (cdr fields))
215 (muse-insert-markup "\n@item ")
216 (insert (car fields))
217 (setq fields (cdr fields))
218 (dolist (field fields)
219 (muse-insert-markup " @tab ")
220 (insert field))))
221 (muse-insert-markup "\n@end multitable\n")))
223 (defun muse-texinfo-finalize-buffer ()
224 (goto-char (point-min))
225 (muse-latex-fixup-dquotes)
226 (texinfo-insert-node-lines (point-min) (point-max) t)
227 (texinfo-all-menus-update t))
229 (defun muse-texinfo-pdf-browse-file (file)
230 (shell-command (concat "open " file)))
232 (defun muse-texinfo-info-generate (file output-path final-target)
233 ;; The version of `texinfmt.el' that comes with Emacs 21 doesn't
234 ;; support @documentencoding, so hack it in.
235 (when (and (not (featurep 'xemacs))
236 (eq emacs-major-version 21))
237 (put 'documentencoding 'texinfo-format
238 'texinfo-discard-line-with-args))
239 (muse-publish-transform-output
240 file output-path final-target "Info"
241 (function
242 (lambda (file output-path)
243 (if muse-texinfo-process-natively
244 (save-window-excursion
245 (save-excursion
246 (find-file file)
247 (let ((inhibit-read-only t))
248 (texinfo-format-buffer))
249 (save-buffer)
250 (kill-buffer (current-buffer))
251 (let ((buf (get-file-buffer file)))
252 (with-current-buffer buf
253 (set-buffer-modified-p nil)
254 (kill-buffer (current-buffer))))
256 (= 0 (shell-command
257 (concat "makeinfo --enable-encoding --output="
258 output-path " " file))))))))
260 (defun muse-texinfo-pdf-generate (file output-path final-target)
261 (muse-publish-transform-output
262 file output-path final-target "PDF"
263 (function
264 (lambda (file output-path)
265 (= 0 (shell-command (concat "texi2pdf -q --clean --output="
266 output-path " " file)))))))
268 (unless (assoc "texi" muse-publishing-styles)
269 (muse-define-style "texi"
270 :suffix 'muse-texinfo-extension
271 :regexps 'muse-texinfo-markup-regexps
272 :functions 'muse-texinfo-markup-functions
273 :strings 'muse-texinfo-markup-strings
274 :specials 'muse-texinfo-decide-specials
275 :after 'muse-texinfo-finalize-buffer
276 :header 'muse-texinfo-header
277 :footer 'muse-texinfo-footer
278 :browser 'find-file)
280 (muse-derive-style "info" "texi"
281 :final 'muse-texinfo-info-generate
282 :osuffix 'muse-texinfo-info-extension
283 :browser 'info)
285 (muse-derive-style "info-pdf" "texi"
286 :final 'muse-texinfo-pdf-generate
287 :osuffix 'muse-texinfo-pdf-extension
288 :browser 'muse-texinfo-pdf-browse-file))
290 (provide 'muse-texinfo)
292 ;;; muse-texinfo.el ends here