muse-publish: Implement muse-publish-enable-dangerous-tags.
[muse-el.git] / lisp / muse-docbook.el
blob46d7b8325f67373e690ff42f80c10fe4d7d35a37
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 (re-search-backward
221 "<\\(/?\\)\\(para\\|footnote\\|literallayout\\)[ >]"
222 nil t)
223 (cond ((string= (match-string 2) "literallayout")
224 (and (not (string= (match-string 1) "/"))
225 (throw 'bail-out t)))
226 ((string= (match-string 2) "para")
227 (and
228 (not (string= (match-string 1) "/"))
229 ;; don't mess up nested lists
230 (not (and (muse-looking-back "<listitem>")
231 (throw 'bail-out t)))))
232 ((string= (match-string 2) "footnote")
233 (string= (match-string 1) "/"))
234 (t nil)))))
235 (when (get-text-property (1- (point)) 'end-list)
236 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
237 (muse-insert-markup "</para>"))
238 (goto-char end))
239 (cond
240 ((eobp)
241 (unless (bolp)
242 (insert "\n")))
243 ((eq (char-after) ?\<)
244 (when (looking-at (concat "<\\(emphasis\\|systemitem\\|inlinemediaobject"
245 "\\|u?link\\|anchor\\|email\\)[ >]"))
246 (muse-insert-markup "<para>")))
248 (muse-insert-markup "<para>")))))
250 (defun muse-docbook-get-author (&optional author)
251 "Split the AUTHOR directive into separate fields.
252 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
253 and anything after `Firstname' is optional."
254 (setq author (save-match-data (split-string author)))
255 (let ((num-el (length author)))
256 (cond ((eq num-el 1)
257 (concat "<firstname>" (car author) "</firstname>"))
258 ((eq num-el 2)
259 (concat "<firstname>" (nth 0 author) "</firstname>"
260 "<surname>" (nth 1 author) "</surname>"))
261 ((eq num-el 3)
262 (concat "<firstname>" (nth 0 author) "</firstname>"
263 "<othername>" (nth 1 author) "</othername>"
264 "<surname>" (nth 2 author) "</surname>"))
266 (let (first last)
267 (setq first (car author))
268 (setq author (nreverse (cdr author)))
269 (setq last (car author))
270 (setq author (nreverse (cdr author)))
271 (concat "<firstname>" first "</firstname>"
272 "<othername>"
273 (mapconcat 'identity author " ")
274 "</othername>"
275 "<surname>" last "</surname>"))))))
277 (defun muse-docbook-fixup-images ()
278 (goto-char (point-min))
279 (while (re-search-forward (concat "<imagedata fileref=\"[^\"]+\""
280 " format=\"\\([^\"]+\\)\" />$")
281 nil t)
282 (replace-match (upcase (match-string 1)) t t nil 1)))
284 (defun muse-docbook-fixup-citations ()
285 ;; remove the role attribute if there is no role
286 (goto-char (point-min))
287 (while (re-search-forward "<\\(citation role=\"nil\"\\)>" nil t)
288 (replace-match "citation" t t nil 1))
289 ;; replace colons in multi-head citations with semicolons
290 (goto-char (point-min))
291 (while (re-search-forward "<citation.*>" nil t)
292 (let ((start (point))
293 (end (re-search-forward "</citation>")))
294 (save-restriction
295 (narrow-to-region start end)
296 (goto-char (point-min))
297 (while (re-search-forward "," nil t)
298 (replace-match ";"))))))
300 (defun muse-docbook-munge-buffer ()
301 (muse-docbook-fixup-images)
302 (muse-docbook-fixup-citations))
304 (defun muse-docbook-entities ()
305 (save-excursion
306 (goto-char (point-min))
307 (if (re-search-forward "<citation" nil t)
308 (concat
309 " [\n<!ENTITY bibliography SYSTEM \""
310 (if (string-match ".short$" (muse-page-name))
311 (substring (muse-page-name) 0 -6)
312 (muse-page-name))
313 ".bib.xml\">\n]")
314 "")))
316 (defun muse-docbook-bibliography ()
317 (save-excursion
318 (goto-char (point-min))
319 (if (re-search-forward "<citation" nil t)
320 "&bibliography;\n"
321 "")))
323 (defun muse-docbook-finalize-buffer ()
324 (when (boundp 'buffer-file-coding-system)
325 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
326 ;; make it agree with the default charset
327 (setq buffer-file-coding-system muse-docbook-encoding-default))))
329 ;;; Register the Muse DocBook XML Publisher
331 (muse-define-style "docbook"
332 :suffix 'muse-docbook-extension
333 :regexps 'muse-docbook-markup-regexps
334 :functions 'muse-docbook-markup-functions
335 :strings 'muse-docbook-markup-strings
336 :specials 'muse-xml-decide-specials
337 :before-end 'muse-docbook-munge-buffer
338 :after 'muse-docbook-finalize-buffer
339 :header 'muse-docbook-header
340 :footer 'muse-docbook-footer
341 :browser 'find-file)
343 (provide 'muse-docbook)
345 ;;; muse-docbook.el ends here