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 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-*"
85 (10100 ,(concat " </tgroup>\\s-*</informaltable>"
86 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
87 "[" muse-regexp-blank
"]*"
88 "<informaltable[^>]*>\\s-*<tgroup[^>]*>\n")
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-*")
98 ;; Beginning of doc, end of doc, or plain paragraph separator
99 (10300 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?"
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"
110 (choice regexp symbol
)
112 (choice string function symbol
))
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 .
" — ")
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 .
"")
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")
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
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."
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'."
195 :group
'muse-docbook
)
197 (defcustom muse-docbook-encoding-map
198 '((iso-8859-1 .
"iso-8859-1")
199 (iso-2022-jp .
"iso-2022-jp")
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
))))
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
233 (and (re-search-backward "<\\(/?\\)\\(para\\|footnote\\)[ >]"
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) "/"))))))
245 ((eq (char-after) ?\
<)
246 (when (looking-at (concat "<\\(emphasis\\|systemitem"
247 "\\|u?link\\|anchor\\|email\\)[ >]"))
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
"\" />\n"))
259 (defun muse-docbook-markup-anchor ()
261 (muse-docbook-insert-anchor (match-string 2)))
264 (defun muse-docbook-markup-table ()
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")
273 ((= type
3) "tfoot"))))
274 (insert "<informaltable>\n"
275 " <tgroup cols='" (number-to-string (length fields
)) "'>\n"
278 (dolist (field fields
)
279 (insert " <entry>" field
"</entry>\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
)))
292 (concat "<firstname>" (car author
) "</firstname>"))
294 (concat "<firstname>" (nth 0 author
) "</firstname>"
295 "<surname>" (nth 1 author
) "</surname>"))
297 (concat "<firstname>" (nth 0 author
) "</firstname>"
298 "<othername>" (nth 1 author
) "</othername>"
299 "<surname>" (nth 2 author
) "</surname>"))
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>"
308 (mapconcat 'identity author
" ")
310 "<surname>" last
"</surname>"))))))
312 (defun muse-docbook-fixup-tables ()
314 (goto-char (point-min))
316 (while (re-search-forward "^ *<tgroup[^>]*>$" nil t
)
317 (unless (get-text-property (point) 'read-only
)
321 (narrow-to-region beg
(and (re-search-forward "^ *</tgroup>"
323 (match-beginning 0))))
324 (goto-char (point-min))
325 (let ((inhibit-read-only t
))
329 (if (re-search-forward
330 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t
)
331 (goto-char (match-beginning 0))
332 (goto-char (point-max)))))
335 (if (re-search-forward
336 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t
)
337 (goto-char (match-end 0))
338 (goto-char (point-max)))))
341 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
342 (cond ((string= (match-string 1) "head") 1)
343 ((string= (match-string 1) "foot") 2)
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