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
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
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.
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
)
40 (defgroup muse-docbook nil
41 "Options controlling the behavior of Muse DocBook XML publishing.
42 See `muse-docbook' for more information."
45 (defcustom muse-docbook-extension
".xml"
46 "Default file extension for publishing DocBook XML files."
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\">
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>
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."
68 (defcustom muse-docbook-footer
"
69 <!-- Page published by Emacs Muse ends here -->
71 "Footer used for publishing DocBook XML files.
72 This may be text or a filename."
76 (defcustom muse-docbook-markup-regexps
77 `(;; join together the parts of a list or table
78 (10000 "</\\([oud]l\\)>\\s-*<\\1>\\s-*" 0 "")
79 (10100 ,(concat " </t\\(body\\|head\\|foot\\)>\\s-*"
80 "</tgroup>\\s-*</informaltable>\\s-*"
81 "<informaltable[^>]*>\\s-*<tgroup[^>]*>\\s-*"
83 (10200 ,(concat " </tgroup>\\s-*</informaltable>\\s-*"
84 "<informaltable[^>]*>\\s-*<tgroup[^>]*>\n") 0 "")
86 ;; Merge consecutive list tags
87 (10300 ,(concat "</\\(itemized\\|ordered\\|variable\\)list>"
88 "\\s-*<\\1list" "[^>]*>\\s-*") 0 "")
90 ;; beginning of doc, end of doc, or plain paragraph separator
91 (10400 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?"
94 "]*\n\\)+\\|\\`\\s-*\\|\\s-*\\'\\)"
95 "\\(<\\(blockquote\\|center\\)>\n\\)?")
96 0 muse-docbook-markup-paragraph
))
97 "List of markup rules for publishing a Muse page to DocBook XML.
98 For more on the structure of this list, see `muse-publish-markup-regexps'."
99 :type
'(repeat (choice
100 (list :tag
"Markup rule"
102 (choice regexp symbol
)
104 (choice string function symbol
))
106 :group
'muse-docbook
)
108 (defcustom muse-docbook-markup-functions
109 '((anchor . muse-docbook-markup-anchor
)
110 (table . muse-docbook-markup-table
))
111 "An alist of style types to custom functions for that kind of text.
112 For more on the structure of this list, see
113 `muse-publish-markup-functions'."
114 :type
'(alist :key-type symbol
:value-type function
)
115 :group
'muse-docbook
)
117 (defcustom muse-docbook-markup-strings
118 '((url-link .
"<ulink url=\"%s\">%s</ulink>")
119 (email-addr .
"<email>%s</email>")
120 (emdash .
" — ")
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 .
"")
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")
147 (end-verse .
"</literallayout>")
148 (begin-example .
"<programlisting>")
149 (end-example .
"</programlisting>")
150 (begin-center .
"<center>\n")
151 (end-center .
"\n</center>")
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
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."
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'."
186 :group
'muse-docbook
)
188 (defcustom muse-docbook-encoding-map
189 '((iso-8859-1 .
"iso-8859-1")
190 (iso-2022-jp .
"iso-2022-jp")
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
))))
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
224 (and (re-search-backward "<\\(/?\\)\\(para\\|footnote\\)[ >]"
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) "/"))))))
236 ((eq (char-after) ?\
<)
237 (when (looking-at (concat "<\\(emphasis\\|systemitem"
238 "\\|ulink\\|anchor\\|email\\)[ >]"))
243 (defun muse-docbook-markup-anchor ()
245 (muse-docbook-insert-anchor (match-string 1))) "")
247 (defun muse-docbook-insert-anchor (anchor)
248 "Insert an anchor, either around the word at point, or within a tag."
249 (skip-chars-forward muse-regexp-space
)
250 (when (looking-at "<\\([^ />]+\\)>")
251 (goto-char (match-end 0)))
252 (insert "<anchor id=\"" anchor
"\" />"))
254 (defun muse-docbook-markup-table ()
257 (delete-region (match-beginning 0) (match-end 0))))
258 (fields (split-string str
"\\s-*|+\\s-*"))
259 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
260 (length (match-string 1 str
))))
261 (part (cond ((= type
1) "tbody")
263 ((= type
3) "tfoot"))))
264 (insert "<informaltable>\n"
265 " <tgroup cols='" (number-to-string (length fields
)) "'>\n"
268 (dolist (field fields
)
269 (insert " <entry>" field
"</entry>\n"))
273 "</informaltable>\n")))
275 (defun muse-docbook-get-author (&optional author
)
276 "Split the AUTHOR directive into separate fields.
277 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
278 and anything after `Firstname' is optional."
279 (setq author
(save-match-data (split-string author
)))
280 (let ((num-el (length author
)))
282 (concat "<firstname>" (car author
) "</firstname>"))
284 (concat "<firstname>" (nth 0 author
) "</firstname>"
285 "<surname>" (nth 1 author
) "</surname>"))
287 (concat "<firstname>" (nth 0 author
) "</firstname>"
288 "<othername>" (nth 1 author
) "</othername>"
289 "<surname>" (nth 2 author
) "</surname>"))
292 (setq first
(car author
))
293 (setq author
(nreverse (cdr author
)))
294 (setq last
(car author
))
295 (setq author
(nreverse (cdr author
)))
296 (concat "<firstname>" first
"</firstname>"
298 (mapconcat 'identity author
" ")
300 "<surname>" last
"</surname>"))))))
302 (defun muse-docbook-fixup-tables ()
304 (goto-char (point-min))
306 (while (re-search-forward "^<informaltable>$" nil t
)
307 (unless (get-text-property (point) 'read-only
)
311 (narrow-to-region beg
(and (re-search-forward "^ </tgroup>$"
313 (match-beginning 0))))
314 (goto-char (point-min))
315 (let ((inhibit-read-only t
))
319 (if (re-search-forward
320 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t
)
321 (goto-char (match-beginning 0))
322 (goto-char (point-max)))))
325 (if (re-search-forward
326 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t
)
327 (goto-char (match-end 0))
328 (goto-char (point-max)))))
331 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
332 (cond ((string= (match-string 1) "head") 1)
333 ((string= (match-string 1) "foot") 2)
336 (defun muse-docbook-finalize-buffer ()
337 (when (boundp 'buffer-file-coding-system
)
338 (when (memq buffer-file-coding-system
'(no-conversion undecided-unix
))
339 ;; make it agree with the default charset
340 (setq buffer-file-coding-system muse-docbook-encoding-default
))))
342 ;; Register the Muse DocBook XML Publisher
344 (unless (assoc "docbook" muse-publishing-styles
)
345 (muse-define-style "docbook"
346 :suffix
'muse-docbook-extension
347 :regexps
'muse-docbook-markup-regexps
348 :functions
'muse-docbook-markup-functions
349 :strings
'muse-docbook-markup-strings
350 :specials
'muse-docbook-markup-specials
351 :before-end
'muse-docbook-fixup-tables
352 :after
'muse-docbook-finalize-buffer
353 :header
'muse-docbook-header
354 :footer
'muse-docbook-footer
355 :browser
'find-file
))
357 (provide 'muse-docbook
)
359 ;;; muse-docbook.el ends here