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