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