No, we really did need the auto-mode-alist part at top-level
[muse-el.git] / lisp / muse-docbook.el
blob3c55aa8d1e333cf8243ca4c6272b3e990cdf06e6
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.
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\">
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 </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-quote-item . "<para>")
160 (end-quote-item . "</para>")
161 (begin-uli . "<itemizedlist mark=\"bullet\">\n")
162 (end-uli . "\n</itemizedlist>")
163 (begin-uli-item . "<listitem><para>")
164 (end-uli-item . "</para></listitem>")
165 (begin-oli . "<orderedlist>\n")
166 (end-oli . "\n</orderedlist>")
167 (begin-oli-item . "<listitem><para>")
168 (end-oli-item . "</para></listitem>")
169 (begin-dl . "<variablelist>\n")
170 (end-dl . "\n</variablelist>")
171 (begin-dl-item . "<varlistentry>\n")
172 (end-dl-item . "\n</varlistentry>")
173 (begin-ddt . "<term>")
174 (end-ddt . "</term>")
175 (begin-dde . "<listitem><para>")
176 (end-dde . "</para></listitem>")
177 (begin-table . "<informaltable>\n")
178 (end-table . "</informaltable>")
179 (begin-table-group . " <tgroup cols='%s'>\n")
180 (end-table-group . " </tgroup>\n")
181 (begin-table-row . " <row>\n")
182 (end-table-row . " </row>\n")
183 (begin-table-entry . " <entry>")
184 (end-table-entry . "</entry>\n"))
185 "Strings used for marking up text.
186 These cover the most basic kinds of markup, the handling of which
187 differs little between the various styles."
188 :type '(alist :key-type symbol :value-type string)
189 :group 'muse-docbook)
191 (defcustom muse-docbook-encoding-default 'utf-8
192 "The default Emacs buffer encoding to use in published files.
193 This will be used if no special characters are found."
194 :type 'symbol
195 :group 'muse-docbook)
197 (defcustom muse-docbook-charset-default "utf-8"
198 "The default DocBook XML charset to use if no translation is
199 found in `muse-docbook-encoding-map'."
200 :type 'string
201 :group 'muse-docbook)
203 (defun muse-docbook-encoding ()
204 (muse-xml-transform-content-type
205 (or (and (boundp 'buffer-file-coding-system)
206 buffer-file-coding-system)
207 muse-docbook-encoding-default)
208 muse-docbook-charset-default))
210 (defun muse-docbook-markup-paragraph ()
211 (catch 'bail-out
212 (let ((end (copy-marker (match-end 0) t)))
213 (goto-char (match-beginning 0))
214 (when (save-excursion
215 (save-match-data
216 (and (re-search-backward
217 "<\\(/?\\)\\(para\\|footnote\\|literallayout\\)[ >]"
218 nil t)
219 (cond ((string= (match-string 2) "literallayout")
220 (throw 'bail-out t))
221 ((string= (match-string 2) "para")
222 (and
223 (not (string= (match-string 1) "/"))
224 ;; don't mess up nested lists
225 (not (and (muse-looking-back "<listitem>")
226 (throw 'bail-out t)))))
227 ((string= (match-string 2) "footnote")
228 (string= (match-string 1) "/"))
229 (t nil)))))
230 (when (get-text-property (1- (point)) 'end-list)
231 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
232 (muse-insert-markup "</para>"))
233 (goto-char end))
234 (cond
235 ((eobp)
236 (unless (bolp)
237 (insert "\n")))
238 ((eq (char-after) ?\<)
239 (when (looking-at (concat "<\\(emphasis\\|systemitem\\|inlinemediaobject"
240 "\\|u?link\\|anchor\\|email\\)[ >]"))
241 (muse-insert-markup "<para>")))
243 (muse-insert-markup "<para>")))))
245 (defun muse-docbook-get-author (&optional author)
246 "Split the AUTHOR directive into separate fields.
247 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
248 and anything after `Firstname' is optional."
249 (setq author (save-match-data (split-string author)))
250 (let ((num-el (length author)))
251 (cond ((eq num-el 1)
252 (concat "<firstname>" (car author) "</firstname>"))
253 ((eq num-el 2)
254 (concat "<firstname>" (nth 0 author) "</firstname>"
255 "<surname>" (nth 1 author) "</surname>"))
256 ((eq num-el 3)
257 (concat "<firstname>" (nth 0 author) "</firstname>"
258 "<othername>" (nth 1 author) "</othername>"
259 "<surname>" (nth 2 author) "</surname>"))
261 (let (first last)
262 (setq first (car author))
263 (setq author (nreverse (cdr author)))
264 (setq last (car author))
265 (setq author (nreverse (cdr author)))
266 (concat "<firstname>" first "</firstname>"
267 "<othername>"
268 (mapconcat 'identity author " ")
269 "</othername>"
270 "<surname>" last "</surname>"))))))
272 (defun muse-docbook-fixup-images ()
273 (goto-char (point-min))
274 (while (re-search-forward (concat "<imagedata fileref=\"[^\"]+\""
275 " format=\"\\([^\"]+\\)\" />$")
276 nil t)
277 (replace-match (upcase (match-string 1)) t t nil 1)))
279 (defun muse-docbook-munge-buffer ()
280 (muse-docbook-fixup-images))
282 (defun muse-docbook-finalize-buffer ()
283 (when (boundp 'buffer-file-coding-system)
284 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
285 ;; make it agree with the default charset
286 (setq buffer-file-coding-system muse-docbook-encoding-default))))
288 ;;; Register the Muse DocBook XML Publisher
290 (muse-define-style "docbook"
291 :suffix 'muse-docbook-extension
292 :regexps 'muse-docbook-markup-regexps
293 :functions 'muse-docbook-markup-functions
294 :strings 'muse-docbook-markup-strings
295 :specials 'muse-xml-decide-specials
296 :before-end 'muse-docbook-munge-buffer
297 :after 'muse-docbook-finalize-buffer
298 :header 'muse-docbook-header
299 :footer 'muse-docbook-footer
300 :browser 'find-file)
302 (provide 'muse-docbook)
304 ;;; muse-docbook.el ends here