1ca9b5f4d251023ef73dd24733df0fb277b1855d
[muse-el.git] / lisp / muse-docbook.el
blob1ca9b5f4d251023ef73dd24733df0fb277b1855d
1 ;;; muse-docbook.el --- publish DocBook files
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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 3, 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 ;; Dale P. Smith (dpsm AT en DOT com) improved the markup
27 ;; significantly and made many valuable suggestions.
29 ;;; Code:
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;; Muse DocBook XML Publishing
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 (require 'muse-publish)
38 (require 'muse-regexps)
39 (require 'muse-xml-common)
41 (defgroup muse-docbook nil
42 "Options controlling the behavior of Muse DocBook XML publishing.
43 See `muse-docbook' for more information."
44 :group 'muse-publish)
46 (defcustom muse-docbook-extension ".xml"
47 "Default file extension for publishing DocBook XML files."
48 :type 'string
49 :group 'muse-docbook)
51 (defcustom muse-docbook-header
52 "<?xml version=\"1.0\" encoding=\"<lisp>
53 (muse-docbook-encoding)</lisp>\"?>
54 <!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"
55 \"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\"<lisp>(muse-docbook-entities)</lisp>>
56 <article>
57 <articleinfo>
58 <title><lisp>(muse-publishing-directive \"title\")</lisp></title>
59 <author><lisp>(muse-docbook-get-author
60 (muse-publishing-directive \"author\"))</lisp></author>
61 <pubdate><lisp>(muse-publishing-directive \"date\")</lisp></pubdate>
62 </articleinfo>
63 <!-- Page published by Emacs Muse begins here -->\n"
64 "Header used for publishing DocBook XML files.
65 This may be text or a filename."
66 :type 'string
67 :group 'muse-docbook)
69 (defcustom muse-docbook-footer "
70 <!-- Page published by Emacs Muse ends here -->
71 <lisp>(muse-docbook-bibliography)</lisp></article>\n"
72 "Footer used for publishing DocBook XML files.
73 This may be text or a filename."
74 :type 'string
75 :group 'muse-docbook)
77 (defcustom muse-docbook-markup-regexps
78 `(;; Beginning of doc, end of doc, or plain paragraph separator
79 (10000 ,(concat "\\(\\(\n\\(?:[" muse-regexp-blank "]*\n\\)*"
80 "\\([" muse-regexp-blank "]*\n\\)\\)"
81 "\\|\\`\\s-*\\|\\s-*\\'\\)")
82 3 muse-docbook-markup-paragraph))
83 "List of markup rules for publishing a Muse page to DocBook XML.
84 For more on the structure of this list, see `muse-publish-markup-regexps'."
85 :type '(repeat (choice
86 (list :tag "Markup rule"
87 integer
88 (choice regexp symbol)
89 integer
90 (choice string function symbol))
91 function))
92 :group 'muse-docbook)
94 (defcustom muse-docbook-markup-functions
95 '((anchor . muse-xml-markup-anchor)
96 (table . muse-xml-markup-table))
97 "An alist of style types to custom functions for that kind of text.
98 For more on the structure of this list, see
99 `muse-publish-markup-functions'."
100 :type '(alist :key-type symbol :value-type function)
101 :group 'muse-docbook)
103 (defcustom muse-docbook-markup-strings
104 '((image-with-desc . "<mediaobject>
105 <imageobject>
106 <imagedata fileref=\"%1%.%2%\" format=\"%2%\" />
107 </imageobject>
108 <caption><para>%3%</para></caption>
109 </mediaobject>")
110 (image . "<inlinemediaobject><imageobject>
111 <imagedata fileref=\"%1%.%2%\" format=\"%2%\" />
112 </imageobject></inlinemediaobject>")
113 (image-link . "<ulink url=\"%1%\"><inlinemediaobject><imageobject>
114 <imagedata fileref=\"%2%.%3%\" format=\"%3%\" />
115 </imageobject></inlinemediaobject></ulink>")
116 (anchor-ref . "<link linkend=\"%s\">%s</link>")
117 (url . "<ulink url=\"%s\">%s</ulink>")
118 (link . "<ulink url=\"%s\">%s</ulink>")
119 (link-and-anchor . "<ulink url=\"%s#%s\">%s</ulink>")
120 (email-addr . "<email>%s</email>")
121 (anchor . "<anchor id=\"%s\" />\n")
122 (emdash . "%s&mdash;%s")
123 (comment-begin . "<!-- ")
124 (comment-end . " -->")
125 (rule . "")
126 (no-break-space . "&nbsp;")
127 (enddots . "....")
128 (dots . "...")
129 (section . "<section><title>")
130 (section-end . "</title>")
131 (subsection . "<section><title>")
132 (subsection-end . "</title>")
133 (subsubsection . "<section><title>")
134 (subsubsection-end . "</title>")
135 (section-other . "<section><title>")
136 (section-other-end . "</title>")
137 (section-close . "</section>")
138 (footnote . "<footnote><para>")
139 (footnote-end . "</para></footnote>")
140 (begin-underline . "")
141 (end-underline . "")
142 (begin-literal . "<systemitem>")
143 (end-literal . "</systemitem>")
144 (begin-emph . "<emphasis>")
145 (end-emph . "</emphasis>")
146 (begin-more-emph . "<emphasis role=\"strong\">")
147 (end-more-emph . "</emphasis>")
148 (begin-most-emph . "<emphasis role=\"strong\"><emphasis>")
149 (end-most-emph . "</emphasis></emphasis>")
150 (begin-verse . "<literallayout>\n")
151 (verse-space . " ")
152 (end-verse . "</literallayout>")
153 (begin-example . "<programlisting>")
154 (end-example . "</programlisting>")
155 (begin-center . "<para role=\"centered\">\n")
156 (end-center . "\n</para>")
157 (begin-quote . "<blockquote>\n")
158 (end-quote . "\n</blockquote>")
159 (begin-cite . "<citation role=\"%s\">")
160 (begin-cite-author . "<citation role=\"%s\">A:")
161 (begin-cite-year . "<citation role=\"%s\">Y:")
162 (end-cite . "</citation>")
163 (begin-quote-item . "<para>")
164 (end-quote-item . "</para>")
165 (begin-uli . "<itemizedlist mark=\"bullet\">\n")
166 (end-uli . "\n</itemizedlist>")
167 (begin-uli-item . "<listitem><para>")
168 (end-uli-item . "</para></listitem>")
169 (begin-oli . "<orderedlist>\n")
170 (end-oli . "\n</orderedlist>")
171 (begin-oli-item . "<listitem><para>")
172 (end-oli-item . "</para></listitem>")
173 (begin-dl . "<variablelist>\n")
174 (end-dl . "\n</variablelist>")
175 (begin-dl-item . "<varlistentry>\n")
176 (end-dl-item . "\n</varlistentry>")
177 (begin-ddt . "<term>")
178 (end-ddt . "</term>")
179 (begin-dde . "<listitem><para>")
180 (end-dde . "</para></listitem>")
181 (begin-table . "<informaltable>\n")
182 (end-table . "</informaltable>")
183 (begin-table-group . " <tgroup cols='%s'>\n")
184 (end-table-group . " </tgroup>\n")
185 (begin-table-row . " <row>\n")
186 (end-table-row . " </row>\n")
187 (begin-table-entry . " <entry>")
188 (end-table-entry . "</entry>\n"))
189 "Strings used for marking up text.
190 These cover the most basic kinds of markup, the handling of which
191 differs little between the various styles."
192 :type '(alist :key-type symbol :value-type string)
193 :group 'muse-docbook)
195 (defcustom muse-docbook-encoding-default 'utf-8
196 "The default Emacs buffer encoding to use in published files.
197 This will be used if no special characters are found."
198 :type 'symbol
199 :group 'muse-docbook)
201 (defcustom muse-docbook-charset-default "utf-8"
202 "The default DocBook XML charset to use if no translation is
203 found in `muse-docbook-encoding-map'."
204 :type 'string
205 :group 'muse-docbook)
207 (defun muse-docbook-encoding ()
208 (muse-xml-transform-content-type
209 (or (and (boundp 'buffer-file-coding-system)
210 buffer-file-coding-system)
211 muse-docbook-encoding-default)
212 muse-docbook-charset-default))
214 (defun muse-docbook-markup-paragraph ()
215 (catch 'bail-out
216 (let ((end (copy-marker (match-end 0) t)))
217 (goto-char (match-beginning 0))
218 (when (save-excursion
219 (save-match-data
220 (and (not (get-text-property (max (point-min) (1- (point)))
221 'muse-no-paragraph))
222 (re-search-backward
223 "<\\(/?\\)\\(para\\|footnote\\|literallayout\\)[ >]"
224 nil t)
225 (cond ((string= (match-string 2) "literallayout")
226 (and (not (string= (match-string 1) "/"))
227 (throw 'bail-out t)))
228 ((string= (match-string 2) "para")
229 (and
230 (not (string= (match-string 1) "/"))
231 ;; don't mess up nested lists
232 (not (and (muse-looking-back "<listitem>")
233 (throw 'bail-out t)))))
234 ((string= (match-string 2) "footnote")
235 (string= (match-string 1) "/"))
236 (t nil)))))
237 (when (get-text-property (1- (point)) 'muse-end-list)
238 (goto-char (previous-single-property-change (1- (point))
239 'muse-end-list)))
240 (muse-insert-markup "</para>"))
241 (goto-char end))
242 (cond
243 ((eobp)
244 (unless (bolp)
245 (insert "\n")))
246 ((get-text-property (point) 'muse-no-paragraph)
247 (forward-char 1)
248 nil)
249 ((eq (char-after) ?\<)
250 (when (looking-at (concat "<\\(emphasis\\|systemitem\\|inlinemediaobject"
251 "\\|u?link\\|anchor\\|email\\)[ >]"))
252 (muse-insert-markup "<para>")))
254 (muse-insert-markup "<para>")))))
256 (defun muse-docbook-get-author (&optional author)
257 "Split the AUTHOR directive into separate fields.
258 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
259 and anything after `Firstname' is optional."
260 (setq author (save-match-data (split-string author)))
261 (let ((num-el (length author)))
262 (cond ((eq num-el 1)
263 (concat "<firstname>" (car author) "</firstname>"))
264 ((eq num-el 2)
265 (concat "<firstname>" (nth 0 author) "</firstname>"
266 "<surname>" (nth 1 author) "</surname>"))
267 ((eq num-el 3)
268 (concat "<firstname>" (nth 0 author) "</firstname>"
269 "<othername>" (nth 1 author) "</othername>"
270 "<surname>" (nth 2 author) "</surname>"))
272 (let (first last)
273 (setq first (car author))
274 (setq author (nreverse (cdr author)))
275 (setq last (car author))
276 (setq author (nreverse (cdr author)))
277 (concat "<firstname>" first "</firstname>"
278 "<othername>"
279 (mapconcat 'identity author " ")
280 "</othername>"
281 "<surname>" last "</surname>"))))))
283 (defun muse-docbook-fixup-images ()
284 (goto-char (point-min))
285 (while (re-search-forward (concat "<imagedata fileref=\"[^\"]+\""
286 " format=\"\\([^\"]+\\)\" />$")
287 nil t)
288 (replace-match (upcase (match-string 1)) t t nil 1)))
290 (defun muse-docbook-fixup-citations ()
291 ;; remove the role attribute if there is no role
292 (goto-char (point-min))
293 (while (re-search-forward "<\\(citation role=\"nil\"\\)>" nil t)
294 (replace-match "citation" t t nil 1))
295 ;; replace colons in multi-head citations with semicolons
296 (goto-char (point-min))
297 (while (re-search-forward "<citation.*>" nil t)
298 (let ((start (point))
299 (end (re-search-forward "</citation>")))
300 (save-restriction
301 (narrow-to-region start end)
302 (goto-char (point-min))
303 (while (re-search-forward "," nil t)
304 (replace-match ";"))))))
306 (defun muse-docbook-munge-buffer ()
307 (muse-docbook-fixup-images)
308 (muse-docbook-fixup-citations))
310 (defun muse-docbook-entities ()
311 (save-excursion
312 (goto-char (point-min))
313 (if (re-search-forward "<citation" nil t)
314 (concat
315 " [\n<!ENTITY bibliography SYSTEM \""
316 (if (string-match ".short$" (muse-page-name))
317 (substring (muse-page-name) 0 -6)
318 (muse-page-name))
319 ".bib.xml\">\n]")
320 "")))
322 (defun muse-docbook-bibliography ()
323 (save-excursion
324 (goto-char (point-min))
325 (if (re-search-forward "<citation" nil t)
326 "&bibliography;\n"
327 "")))
329 (defun muse-docbook-finalize-buffer ()
330 (when (boundp 'buffer-file-coding-system)
331 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
332 ;; make it agree with the default charset
333 (setq buffer-file-coding-system muse-docbook-encoding-default))))
335 ;;; Register the Muse DocBook XML Publisher
337 (muse-define-style "docbook"
338 :suffix 'muse-docbook-extension
339 :regexps 'muse-docbook-markup-regexps
340 :functions 'muse-docbook-markup-functions
341 :strings 'muse-docbook-markup-strings
342 :specials 'muse-xml-decide-specials
343 :before-end 'muse-docbook-munge-buffer
344 :after 'muse-docbook-finalize-buffer
345 :header 'muse-docbook-header
346 :footer 'muse-docbook-footer
347 :browser 'find-file)
349 (provide 'muse-docbook)
351 ;;; muse-docbook.el ends here