1 ;;; muse-docbook.el --- publish DocBook files
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.
26 ;; Dale P. Smith (dpsm AT en DOT com) improved the markup
27 ;; significantly and made many valuable suggestions.
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."
46 (defcustom muse-docbook-extension
".xml"
47 "Default file extension for publishing DocBook XML files."
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\">
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>
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."
69 (defcustom muse-docbook-footer
"
70 <!-- Page published by Emacs Muse ends here -->
72 "Footer used for publishing DocBook XML files.
73 This may be text or a filename."
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 "\\|\\`\\s-*\\|\\s-*\\'\\)")
81 0 muse-docbook-markup-paragraph
))
82 "List of markup rules for publishing a Muse page to DocBook XML.
83 For more on the structure of this list, see `muse-publish-markup-regexps'."
84 :type
'(repeat (choice
85 (list :tag
"Markup rule"
87 (choice regexp symbol
)
89 (choice string function symbol
))
93 (defcustom muse-docbook-markup-functions
94 '((anchor . muse-xml-markup-anchor
)
95 (table . muse-xml-markup-table
))
96 "An alist of style types to custom functions for that kind of text.
97 For more on the structure of this list, see
98 `muse-publish-markup-functions'."
99 :type
'(alist :key-type symbol
:value-type function
)
100 :group
'muse-docbook
)
102 (defcustom muse-docbook-markup-strings
103 '((image-with-desc .
"<mediaobject>
105 <imagedata fileref=\"%1%.%2%\" format=\"%2%\" />
107 <caption><para>%3%</para></caption>
109 (image .
"<inlinemediaobject><imageobject>
110 <imagedata fileref=\"%1%.%2%\" format=\"%2%\" />
111 </imageobject></inlinemediaobject>")
112 (image-link .
"<ulink url=\"%1%\"><inlinemediaobject><imageobject>
113 <imagedata fileref=\"%2%.%3%\" format=\"%3%\" />
114 </imageobject></inlinemediaobject></ulink>")
115 (anchor-ref .
"<link linkend=\"%s\">%s</link>")
116 (url .
"<ulink url=\"%s\">%s</ulink>")
117 (link .
"<ulink url=\"%s\">%s</ulink>")
118 (link-and-anchor .
"<ulink url=\"%s#%s\">%s</ulink>")
119 (email-addr .
"<email>%s</email>")
120 (anchor .
"<anchor id=\"%s\" />\n")
121 (emdash .
"%s—%s")
122 (comment-begin .
"<!-- ")
123 (comment-end .
" -->")
125 (no-break-space .
" ")
128 (section .
"<section><title>")
129 (section-end .
"</title>")
130 (subsection .
"<section><title>")
131 (subsection-end .
"</title>")
132 (subsubsection .
"<section><title>")
133 (subsubsection-end .
"</title>")
134 (section-other .
"<section><title>")
135 (section-other-end .
"</title>")
136 (section-close .
"</section>")
137 (footnote .
"<footnote><para>")
138 (footnote-end .
"</para></footnote>")
139 (begin-underline .
"")
141 (begin-literal .
"<systemitem>")
142 (end-literal .
"</systemitem>")
143 (begin-emph .
"<emphasis>")
144 (end-emph .
"</emphasis>")
145 (begin-more-emph .
"<emphasis role=\"strong\">")
146 (end-more-emph .
"</emphasis>")
147 (begin-most-emph .
"<emphasis role=\"strong\"><emphasis>")
148 (end-most-emph .
"</emphasis></emphasis>")
149 (begin-verse .
"<literallayout>\n")
151 (end-verse .
"</literallayout>")
152 (begin-example .
"<programlisting>")
153 (end-example .
"</programlisting>")
154 (begin-center .
"<para role=\"centered\">\n")
155 (end-center .
"\n</para>")
156 (begin-quote .
"<blockquote>\n")
157 (end-quote .
"\n</blockquote>")
158 (begin-quote-item .
"<para>")
159 (end-quote-item .
"</para>")
160 (begin-uli .
"<itemizedlist mark=\"bullet\">\n")
161 (end-uli .
"\n</itemizedlist>")
162 (begin-uli-item .
"<listitem><para>")
163 (end-uli-item .
"</para></listitem>")
164 (begin-oli .
"<orderedlist>\n")
165 (end-oli .
"\n</orderedlist>")
166 (begin-oli-item .
"<listitem><para>")
167 (end-oli-item .
"</para></listitem>")
168 (begin-dl .
"<variablelist>\n")
169 (end-dl .
"\n</variablelist>")
170 (begin-dl-item .
"<varlistentry>\n")
171 (end-dl-item .
"\n</varlistentry>")
172 (begin-ddt .
"<term>")
173 (end-ddt .
"</term>")
174 (begin-dde .
"<listitem><para>")
175 (end-dde .
"</para></listitem>")
176 (begin-table .
"<informaltable>\n")
177 (end-table .
"</informaltable>")
178 (begin-table-group .
" <tgroup cols='%s'>\n")
179 (end-table-group .
" </tgroup>\n")
180 (begin-table-row .
" <row>\n")
181 (end-table-row .
" </row>\n")
182 (begin-table-entry .
" <entry>")
183 (end-table-entry .
"</entry>\n"))
184 "Strings used for marking up text.
185 These cover the most basic kinds of markup, the handling of which
186 differs little between the various styles."
187 :type
'(alist :key-type symbol
:value-type string
)
188 :group
'muse-docbook
)
190 (defcustom muse-docbook-encoding-default
'utf-8
191 "The default Emacs buffer encoding to use in published files.
192 This will be used if no special characters are found."
194 :group
'muse-docbook
)
196 (defcustom muse-docbook-charset-default
"utf-8"
197 "The default DocBook XML charset to use if no translation is
198 found in `muse-docbook-encoding-map'."
200 :group
'muse-docbook
)
202 (defun muse-docbook-encoding ()
203 (muse-xml-transform-content-type
204 (or (and (boundp 'buffer-file-coding-system
)
205 buffer-file-coding-system
)
206 muse-docbook-encoding-default
)
207 muse-docbook-charset-default
))
209 (defun muse-docbook-markup-paragraph ()
210 (let ((end (copy-marker (match-end 0) t
)))
211 (goto-char (match-beginning 0))
212 (when (save-excursion
214 (and (re-search-backward "<\\(/?\\)\\(para\\|footnote\\)[ >]"
216 (or (and (string= (match-string 2) "para")
217 (not (string= (match-string 1) "/")))
218 (and (string= (match-string 2) "footnote")
219 (string= (match-string 1) "/"))))))
220 (when (get-text-property (1- (point)) 'end-list
)
221 (goto-char (previous-single-property-change (1- (point)) 'end-list
)))
222 (muse-insert-markup "</para>"))
228 ((eq (char-after) ?\
<)
229 (when (looking-at (concat "<\\(emphasis\\|systemitem\\|inlinemediaobject"
230 "\\|u?link\\|anchor\\|email\\)[ >]"))
231 (muse-insert-markup "<para>")))
233 (muse-insert-markup "<para>"))))
235 (defun muse-docbook-get-author (&optional author
)
236 "Split the AUTHOR directive into separate fields.
237 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
238 and anything after `Firstname' is optional."
239 (setq author
(save-match-data (split-string author
)))
240 (let ((num-el (length author
)))
242 (concat "<firstname>" (car author
) "</firstname>"))
244 (concat "<firstname>" (nth 0 author
) "</firstname>"
245 "<surname>" (nth 1 author
) "</surname>"))
247 (concat "<firstname>" (nth 0 author
) "</firstname>"
248 "<othername>" (nth 1 author
) "</othername>"
249 "<surname>" (nth 2 author
) "</surname>"))
252 (setq first
(car author
))
253 (setq author
(nreverse (cdr author
)))
254 (setq last
(car author
))
255 (setq author
(nreverse (cdr author
)))
256 (concat "<firstname>" first
"</firstname>"
258 (mapconcat 'identity author
" ")
260 "<surname>" last
"</surname>"))))))
262 (defun muse-docbook-fixup-images ()
263 (goto-char (point-min))
264 (while (re-search-forward (concat "<imagedata fileref=\"[^\"]+\""
265 " format=\"\\([^\"]+\\)\" />$")
267 (replace-match (upcase (match-string 1)) t t nil
1)))
269 (defun muse-docbook-finalize-buffer ()
270 (muse-docbook-fixup-images)
271 (when (boundp 'buffer-file-coding-system
)
272 (when (memq buffer-file-coding-system
'(no-conversion undecided-unix
))
273 ;; make it agree with the default charset
274 (setq buffer-file-coding-system muse-docbook-encoding-default
))))
276 ;; Register the Muse DocBook XML Publisher
278 (unless (assoc "docbook" muse-publishing-styles
)
279 (muse-define-style "docbook"
280 :suffix
'muse-docbook-extension
281 :regexps
'muse-docbook-markup-regexps
282 :functions
'muse-docbook-markup-functions
283 :strings
'muse-docbook-markup-strings
284 :specials
'muse-xml-decide-specials
285 :after
'muse-docbook-finalize-buffer
286 :header
'muse-docbook-header
287 :footer
'muse-docbook-footer
288 :browser
'find-file
))
290 (provide 'muse-docbook
)
292 ;;; muse-docbook.el ends here