Link inside table issue, fixes read-only text error.
[muse-el.git] / lisp / muse-docbook.el
blobf8bf742de94807b811da6e72ae2a192be4e15b36
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 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-*"
82 "<t\\1>\n") 0 "")
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\\)>\\)?"
92 "\\(?:\n\\(["
93 muse-regexp-blank
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"
101 integer
102 (choice regexp symbol)
103 integer
104 (choice string function symbol))
105 function))
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 . " &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 . "<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
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 "\\|ulink\\|anchor\\|email\\)[ >]"))
239 (insert "<para>")))
241 (insert "<para>"))))
243 (defun muse-docbook-markup-anchor ()
244 (save-match-data
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 ()
255 (let* ((str (prog1
256 (match-string 1)
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")
262 ((= type 2) "thead")
263 ((= type 3) "tfoot"))))
264 (insert "<informaltable>\n"
265 " <tgroup cols='" (number-to-string (length fields)) "'>\n"
266 " <" part ">\n"
267 " <row>\n")
268 (dolist (field fields)
269 (insert " <entry>" field "</entry>\n"))
270 (insert " </row>\n"
271 " </" part ">\n"
272 " </tgroup>\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)))
281 (cond ((eq num-el 1)
282 (concat "<firstname>" (car author) "</firstname>"))
283 ((eq num-el 2)
284 (concat "<firstname>" (nth 0 author) "</firstname>"
285 "<surname>" (nth 1 author) "</surname>"))
286 ((eq num-el 3)
287 (concat "<firstname>" (nth 0 author) "</firstname>"
288 "<othername>" (nth 1 author) "</othername>"
289 "<surname>" (nth 2 author) "</surname>"))
291 (let (first last)
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>"
297 "<othername>"
298 (mapconcat 'identity author " ")
299 "</othername>"
300 "<surname>" last "</surname>"))))))
302 (defun muse-docbook-fixup-tables ()
303 "Sort table parts."
304 (goto-char (point-min))
305 (let (last)
306 (while (re-search-forward "^<informaltable>$" nil t)
307 (unless (get-text-property (point) 'read-only)
308 (forward-line 2)
309 (save-restriction
310 (let ((beg (point)))
311 (narrow-to-region beg (and (re-search-forward "^ </tgroup>$"
312 nil t)
313 (match-beginning 0))))
314 (goto-char (point-min))
315 (let ((inhibit-read-only t))
316 (sort-subr nil
317 (function
318 (lambda ()
319 (if (re-search-forward
320 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t)
321 (goto-char (match-beginning 0))
322 (goto-char (point-max)))))
323 (function
324 (lambda ()
325 (if (re-search-forward
326 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t)
327 (goto-char (match-end 0))
328 (goto-char (point-max)))))
329 (function
330 (lambda ()
331 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
332 (cond ((string= (match-string 1) "head") 1)
333 ((string= (match-string 1) "foot") 2)
334 (t 3)))))))))))
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