muse-xml: Separate section from title.
[muse-el.git] / lisp / muse-docbook.el
blob7b9446b92ae77a5e9ee40e2680f5d600fa0f878f
1 ;;; muse-docbook.el --- Publish DocBook files.
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 ;; 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)
40 (defgroup muse-docbook nil
41 "Options controlling the behavior of Muse DocBook XML publishing.
42 See `muse-docbook' for more information."
43 :group 'muse-publish)
45 (defcustom muse-docbook-extension ".xml"
46 "Default file extension for publishing DocBook XML files."
47 :type 'string
48 :group 'muse-docbook)
50 (defcustom muse-docbook-header
51 "<?xml version=\"1.0\" encoding=\"<lisp>
52 (muse-docbook-encoding)</lisp>\"?>
53 <!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"
54 \"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">
55 <article>
56 <articleinfo>
57 <title><lisp>(muse-publishing-directive \"title\")</lisp></title>
58 <author><lisp>(muse-docbook-get-author
59 (muse-publishing-directive \"author\"))</lisp></author>
60 <pubdate><lisp>(muse-publishing-directive \"date\")</lisp></pubdate>
61 </articleinfo>
62 <!-- Page published by Emacs Muse begins here -->\n"
63 "Header used for publishing DocBook XML files.
64 This may be text or a filename."
65 :type 'string
66 :group 'muse-docbook)
68 (defcustom muse-docbook-footer "
69 <!-- Page published by Emacs Muse ends here -->
70 </article>\n"
71 "Footer used for publishing DocBook XML files.
72 This may be text or a filename."
73 :type 'string
74 :group 'muse-docbook)
76 (defcustom muse-docbook-markup-regexps
77 `(;; Join together the parts of a table
78 (10000 ,(concat " </t\\(body\\|head\\|foot\\)>\\s-*"
79 "</tgroup>\\s-*</informaltable>\\s-*"
80 "<informaltable[^>]*>\\s-*<tgroup[^>]*>\\s-*"
81 "<t\\1>\n") 0 "")
82 (10100 ,(concat " </tgroup>\\s-*</informaltable>\\s-*"
83 "<informaltable[^>]*>\\s-*<tgroup[^>]*>\n") 0 "")
85 ;; Join together the parts of a list
86 (10200 ,(concat "</\\(itemized\\|ordered\\|variable\\)list>"
87 "\\s-*<\\1list" "[^>]*>\\s-*") 0 "")
89 ;; Beginning of doc, end of doc, or plain paragraph separator
90 (10300 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?"
91 "\\(?:\n\\(["
92 muse-regexp-blank
93 "]*\n\\)+\\|\\`\\s-*\\|\\s-*\\'\\)"
94 "\\(<\\(blockquote\\|center\\)>\n\\)?")
95 0 muse-docbook-markup-paragraph))
96 "List of markup rules for publishing a Muse page to DocBook XML.
97 For more on the structure of this list, see `muse-publish-markup-regexps'."
98 :type '(repeat (choice
99 (list :tag "Markup rule"
100 integer
101 (choice regexp symbol)
102 integer
103 (choice string function symbol))
104 function))
105 :group 'muse-docbook)
107 (defcustom muse-docbook-markup-functions
108 '((anchor . muse-docbook-markup-anchor)
109 (table . muse-docbook-markup-table))
110 "An alist of style types to custom functions for that kind of text.
111 For more on the structure of this list, see
112 `muse-publish-markup-functions'."
113 :type '(alist :key-type symbol :value-type function)
114 :group 'muse-docbook)
116 (defcustom muse-docbook-markup-strings
117 '((url-link . "<ulink url=\"%s\">%s</ulink>")
118 (email-addr . "<email>%s</email>")
119 (emdash . " &mdash; ")
120 (rule . "")
121 (enddots . "....")
122 (dots . "...")
123 (section . "<section><title>")
124 (section-end . "</title>")
125 (subsection . "<section><title>")
126 (subsection-end . "</title>")
127 (subsubsection . "<section><title>")
128 (subsubsection-end . "</title>")
129 (section-other . "<section><title>")
130 (section-other-end . "</title>")
131 (section-close . "</section>")
132 (footnote . "<footnote><para>")
133 (footnote-end . "</para></footnote>")
134 (begin-underline . "")
135 (end-underline . "")
136 (begin-literal . "<systemitem>")
137 (end-literal . "</systemitem>")
138 (begin-emph . "<emphasis>")
139 (end-emph . "</emphasis>")
140 (begin-more-emph . "<emphasis role=\"strong\">")
141 (end-more-emph . "</emphasis>")
142 (begin-most-emph . "<emphasis role=\"strong\"><emphasis>")
143 (end-most-emph . "</emphasis></emphasis>")
144 (begin-verse . "<literallayout>\n")
145 (verse-space . " ")
146 (end-verse . "</literallayout>")
147 (begin-example . "<programlisting>")
148 (end-example . "</programlisting>")
149 (begin-center . "<center>\n")
150 (end-center . "\n</center>")
151 (begin-quote . "<blockquote>\n")
152 (end-quote . "\n</blockquote>")
153 (begin-uli . "<itemizedlist mark=\"bullet\">\n<listitem><para>")
154 (end-uli . "</para></listitem>\n</itemizedlist>")
155 (begin-oli . "<orderedlist>\n<listitem><para>")
156 (end-oli . "</para></listitem>\n</orderedlist>")
157 (begin-ddt . "<variablelist>\n<varlistentry>\n<term>")
158 (start-dde . "</term>\n<listitem><para>")
159 (end-ddt . "</para></listitem>\n</varlistentry>\n</variablelist>"))
160 "Strings used for marking up text.
161 These cover the most basic kinds of markup, the handling of which
162 differs little between the various styles."
163 :type '(alist :key-type symbol :value-type string)
164 :group 'muse-docbook)
166 (defcustom muse-docbook-markup-specials
167 '((?\" . "&quot;")
168 (?\< . "&lt;")
169 (?\> . "&gt;")
170 (?\& . "&amp;"))
171 "A table of characters which must be represented specially."
172 :type '(alist :key-type character :value-type string)
173 :group 'muse-docbook)
175 (defcustom muse-docbook-encoding-default 'utf-8
176 "The default Emacs buffer encoding to use in published files.
177 This will be used if no special characters are found."
178 :type 'symbol
179 :group 'muse-docbook)
181 (defcustom muse-docbook-charset-default "utf-8"
182 "The default DocBook XML charset to use if no translation is
183 found in `muse-docbook-encoding-map'."
184 :type 'string
185 :group 'muse-docbook)
187 (defcustom muse-docbook-encoding-map
188 '((iso-8859-1 . "iso-8859-1")
189 (iso-2022-jp . "iso-2022-jp")
190 (utf-8 . "utf-8")
191 (japanese-iso-8bit . "euc-jp")
192 (chinese-big5 . "big5")
193 (mule-utf-8 . "utf-8")
194 (chinese-iso-8bit . "gb2312")
195 (chinese-gbk . "gbk"))
196 "An alist mapping emacs coding systems to appropriate DocBook charsets.
197 Use the base name of the coding system (i.e. without the -unix)."
198 :type '(alist :key-type coding-system :value-type string)
199 :group 'muse-docbook)
201 (defun muse-docbook-transform-content-type (content-type)
202 "Using `muse-docbook-encoding-map', try and resolve an emacs
203 coding system to an associated DocBook XML coding system. If no
204 match is found, `muse-docbook-charset-default' is used instead."
205 (let ((match (and (fboundp 'coding-system-base)
206 (assoc (coding-system-base content-type)
207 muse-docbook-encoding-map))))
208 (if match
209 (cdr match)
210 muse-docbook-charset-default)))
212 (defun muse-docbook-encoding ()
213 (muse-docbook-transform-content-type
214 (or (and (boundp 'buffer-file-coding-system)
215 buffer-file-coding-system)
216 muse-docbook-encoding-default)))
218 (defun muse-docbook-markup-paragraph ()
219 (let ((end (copy-marker (match-end 0) t)))
220 (goto-char (match-beginning 0))
221 (when (save-excursion
222 (save-match-data
223 (and (re-search-backward "<\\(/?\\)\\(para\\|footnote\\)[ >]"
224 nil t)
225 (or (and (string= (match-string 2) "para")
226 (not (string= (match-string 1) "/")))
227 (and (string= (match-string 2) "footnote")
228 (string= (match-string 1) "/"))))))
229 (insert "</para>"))
230 (goto-char end))
231 (cond
232 ((eobp)
233 (unless (bolp)
234 (insert "\n")))
235 ((eq (char-after) ?\<)
236 (when (looking-at (concat "<\\(emphasis\\|systemitem"
237 "\\|ulink\\|anchor\\|email\\)[ >]"))
238 (insert "<para>")))
240 (insert "<para>"))))
242 (defun muse-docbook-markup-anchor ()
243 (save-match-data
244 (muse-docbook-insert-anchor (match-string 1))) "")
246 (defun muse-docbook-insert-anchor (anchor)
247 "Insert an anchor, either around the word at point, or within a tag."
248 (skip-chars-forward muse-regexp-space)
249 (when (looking-at "<\\([^ />]+\\)>")
250 (goto-char (match-end 0)))
251 (insert "<anchor id=\"" anchor "\" />"))
253 (defun muse-docbook-markup-table ()
254 (let* ((str (prog1
255 (match-string 1)
256 (delete-region (match-beginning 0) (match-end 0))))
257 (fields (split-string str "\\s-*|+\\s-*"))
258 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str)
259 (length (match-string 1 str))))
260 (part (cond ((= type 1) "tbody")
261 ((= type 2) "thead")
262 ((= type 3) "tfoot"))))
263 (insert "<informaltable>\n"
264 " <tgroup cols='" (number-to-string (length fields)) "'>\n"
265 " <" part ">\n"
266 " <row>\n")
267 (dolist (field fields)
268 (insert " <entry>" field "</entry>\n"))
269 (insert " </row>\n"
270 " </" part ">\n"
271 " </tgroup>\n"
272 "</informaltable>\n")))
274 (defun muse-docbook-get-author (&optional author)
275 "Split the AUTHOR directive into separate fields.
276 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
277 and anything after `Firstname' is optional."
278 (setq author (save-match-data (split-string author)))
279 (let ((num-el (length author)))
280 (cond ((eq num-el 1)
281 (concat "<firstname>" (car author) "</firstname>"))
282 ((eq num-el 2)
283 (concat "<firstname>" (nth 0 author) "</firstname>"
284 "<surname>" (nth 1 author) "</surname>"))
285 ((eq num-el 3)
286 (concat "<firstname>" (nth 0 author) "</firstname>"
287 "<othername>" (nth 1 author) "</othername>"
288 "<surname>" (nth 2 author) "</surname>"))
290 (let (first last)
291 (setq first (car author))
292 (setq author (nreverse (cdr author)))
293 (setq last (car author))
294 (setq author (nreverse (cdr author)))
295 (concat "<firstname>" first "</firstname>"
296 "<othername>"
297 (mapconcat 'identity author " ")
298 "</othername>"
299 "<surname>" last "</surname>"))))))
301 (defun muse-docbook-fixup-tables ()
302 "Sort table parts."
303 (goto-char (point-min))
304 (let (last)
305 (while (re-search-forward "^<informaltable>$" nil t)
306 (unless (get-text-property (point) 'read-only)
307 (forward-line 2)
308 (save-restriction
309 (let ((beg (point)))
310 (narrow-to-region beg (and (re-search-forward "^ </tgroup>$"
311 nil t)
312 (match-beginning 0))))
313 (goto-char (point-min))
314 (let ((inhibit-read-only t))
315 (sort-subr nil
316 (function
317 (lambda ()
318 (if (re-search-forward
319 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t)
320 (goto-char (match-beginning 0))
321 (goto-char (point-max)))))
322 (function
323 (lambda ()
324 (if (re-search-forward
325 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t)
326 (goto-char (match-end 0))
327 (goto-char (point-max)))))
328 (function
329 (lambda ()
330 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
331 (cond ((string= (match-string 1) "head") 1)
332 ((string= (match-string 1) "foot") 2)
333 (t 3)))))))))))
335 (defun muse-docbook-finalize-buffer ()
336 (when (boundp 'buffer-file-coding-system)
337 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
338 ;; make it agree with the default charset
339 (setq buffer-file-coding-system muse-docbook-encoding-default))))
341 ;; Register the Muse DocBook XML Publisher
343 (unless (assoc "docbook" muse-publishing-styles)
344 (muse-define-style "docbook"
345 :suffix 'muse-docbook-extension
346 :regexps 'muse-docbook-markup-regexps
347 :functions 'muse-docbook-markup-functions
348 :strings 'muse-docbook-markup-strings
349 :specials 'muse-docbook-markup-specials
350 :before-end 'muse-docbook-fixup-tables
351 :after 'muse-docbook-finalize-buffer
352 :header 'muse-docbook-header
353 :footer 'muse-docbook-footer
354 :browser 'find-file))
356 (provide 'muse-docbook)
358 ;;; muse-docbook.el ends here