Permit non-Muse files in projects to be linked to, tweak file-regexp
[muse-el.git] / lisp / muse-docbook.el
blobde12b59644d55db02e3029f0a23f368f37ddb0a0
1 ;;; muse-docbook.el --- publish DocBook files
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; 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)
39 (require 'muse-xml-common)
41 (defgroup muse-docbook nil
42 "Options controlling the behavior of Muse DocBook XML publishing.
43 See `muse-docbook' for more information."
44 :group 'muse-publish)
46 (defcustom muse-docbook-extension ".xml"
47 "Default file extension for publishing DocBook XML files."
48 :type 'string
49 :group 'muse-docbook)
51 (defcustom muse-docbook-header
52 "<?xml version=\"1.0\" encoding=\"<lisp>
53 (muse-docbook-encoding)</lisp>\"?>
54 <!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"
55 \"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">
56 <article>
57 <articleinfo>
58 <title><lisp>(muse-publishing-directive \"title\")</lisp></title>
59 <author><lisp>(muse-docbook-get-author
60 (muse-publishing-directive \"author\"))</lisp></author>
61 <pubdate><lisp>(muse-publishing-directive \"date\")</lisp></pubdate>
62 </articleinfo>
63 <!-- Page published by Emacs Muse begins here -->\n"
64 "Header used for publishing DocBook XML files.
65 This may be text or a filename."
66 :type 'string
67 :group 'muse-docbook)
69 (defcustom muse-docbook-footer "
70 <!-- Page published by Emacs Muse ends here -->
71 </article>\n"
72 "Footer used for publishing DocBook XML files.
73 This may be text or a filename."
74 :type 'string
75 :group 'muse-docbook)
77 (defcustom muse-docbook-markup-regexps
78 `(;; Beginning of doc, end of doc, or plain paragraph separator
79 (10000 ,(concat "\\(\\(\n\\(?:[" muse-regexp-blank "]*\n\\)*"
80 "\\([" muse-regexp-blank "]*\n\\)\\)"
81 "\\|\\`\\s-*\\|\\s-*\\'\\)")
82 3 muse-docbook-markup-paragraph))
83 "List of markup rules for publishing a Muse page to DocBook XML.
84 For more on the structure of this list, see `muse-publish-markup-regexps'."
85 :type '(repeat (choice
86 (list :tag "Markup rule"
87 integer
88 (choice regexp symbol)
89 integer
90 (choice string function symbol))
91 function))
92 :group 'muse-docbook)
94 (defcustom muse-docbook-markup-functions
95 '((anchor . muse-xml-markup-anchor)
96 (table . muse-xml-markup-table))
97 "An alist of style types to custom functions for that kind of text.
98 For more on the structure of this list, see
99 `muse-publish-markup-functions'."
100 :type '(alist :key-type symbol :value-type function)
101 :group 'muse-docbook)
103 (defcustom muse-docbook-markup-strings
104 '((image-with-desc . "<mediaobject>
105 <imageobject>
106 <imagedata fileref=\"%1%.%2%\" format=\"%2%\" />
107 </imageobject>
108 <caption><para>%3%</para></caption>
109 </mediaobject>")
110 (image . "<inlinemediaobject><imageobject>
111 <imagedata fileref=\"%1%.%2%\" format=\"%2%\" />
112 </imageobject></inlinemediaobject>")
113 (image-link . "<ulink url=\"%1%\"><inlinemediaobject><imageobject>
114 <imagedata fileref=\"%2%.%3%\" format=\"%3%\" />
115 </imageobject></inlinemediaobject></ulink>")
116 (anchor-ref . "<link linkend=\"%s\">%s</link>")
117 (url . "<ulink url=\"%s\">%s</ulink>")
118 (link . "<ulink url=\"%s\">%s</ulink>")
119 (link-and-anchor . "<ulink url=\"%s#%s\">%s</ulink>")
120 (email-addr . "<email>%s</email>")
121 (anchor . "<anchor id=\"%s\" />\n")
122 (emdash . "%s&mdash;%s")
123 (comment-begin . "<!-- ")
124 (comment-end . " -->")
125 (rule . "")
126 (no-break-space . "&nbsp;")
127 (enddots . "....")
128 (dots . "...")
129 (section . "<section><title>")
130 (section-end . "</title>")
131 (subsection . "<section><title>")
132 (subsection-end . "</title>")
133 (subsubsection . "<section><title>")
134 (subsubsection-end . "</title>")
135 (section-other . "<section><title>")
136 (section-other-end . "</title>")
137 (section-close . "</section>")
138 (footnote . "<footnote><para>")
139 (footnote-end . "</para></footnote>")
140 (begin-underline . "")
141 (end-underline . "")
142 (begin-literal . "<systemitem>")
143 (end-literal . "</systemitem>")
144 (begin-emph . "<emphasis>")
145 (end-emph . "</emphasis>")
146 (begin-more-emph . "<emphasis role=\"strong\">")
147 (end-more-emph . "</emphasis>")
148 (begin-most-emph . "<emphasis role=\"strong\"><emphasis>")
149 (end-most-emph . "</emphasis></emphasis>")
150 (begin-verse . "<literallayout>\n")
151 (verse-space . " ")
152 (end-verse . "</literallayout>")
153 (begin-example . "<programlisting>")
154 (end-example . "</programlisting>")
155 (begin-center . "<para role=\"centered\">\n")
156 (end-center . "\n</para>")
157 (begin-quote . "<blockquote>\n")
158 (end-quote . "\n</blockquote>")
159 (begin-quote-item . "<para>")
160 (end-quote-item . "</para>")
161 (begin-uli . "<itemizedlist mark=\"bullet\">\n")
162 (end-uli . "\n</itemizedlist>")
163 (begin-uli-item . "<listitem><para>")
164 (end-uli-item . "</para></listitem>")
165 (begin-oli . "<orderedlist>\n")
166 (end-oli . "\n</orderedlist>")
167 (begin-oli-item . "<listitem><para>")
168 (end-oli-item . "</para></listitem>")
169 (begin-dl . "<variablelist>\n")
170 (end-dl . "\n</variablelist>")
171 (begin-dl-item . "<varlistentry>\n")
172 (end-dl-item . "\n</varlistentry>")
173 (begin-ddt . "<term>")
174 (end-ddt . "</term>")
175 (begin-dde . "<listitem><para>")
176 (end-dde . "</para></listitem>")
177 (begin-table . "<informaltable>\n")
178 (end-table . "</informaltable>")
179 (begin-table-group . " <tgroup cols='%s'>\n")
180 (end-table-group . " </tgroup>\n")
181 (begin-table-row . " <row>\n")
182 (end-table-row . " </row>\n")
183 (begin-table-entry . " <entry>")
184 (end-table-entry . "</entry>\n"))
185 "Strings used for marking up text.
186 These cover the most basic kinds of markup, the handling of which
187 differs little between the various styles."
188 :type '(alist :key-type symbol :value-type string)
189 :group 'muse-docbook)
191 (defcustom muse-docbook-encoding-default 'utf-8
192 "The default Emacs buffer encoding to use in published files.
193 This will be used if no special characters are found."
194 :type 'symbol
195 :group 'muse-docbook)
197 (defcustom muse-docbook-charset-default "utf-8"
198 "The default DocBook XML charset to use if no translation is
199 found in `muse-docbook-encoding-map'."
200 :type 'string
201 :group 'muse-docbook)
203 (defun muse-docbook-encoding ()
204 (muse-xml-transform-content-type
205 (or (and (boundp 'buffer-file-coding-system)
206 buffer-file-coding-system)
207 muse-docbook-encoding-default)
208 muse-docbook-charset-default))
210 (defun muse-docbook-markup-paragraph ()
211 (let ((end (copy-marker (match-end 0) t)))
212 (goto-char (match-beginning 0))
213 (when (save-excursion
214 (save-match-data
215 (and (re-search-backward "<\\(/?\\)\\(para\\|footnote\\)[ >]"
216 nil t)
217 (or (and (string= (match-string 2) "para")
218 (not (string= (match-string 1) "/")))
219 (and (string= (match-string 2) "footnote")
220 (string= (match-string 1) "/"))))))
221 (when (get-text-property (1- (point)) 'end-list)
222 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
223 (muse-insert-markup "</para>"))
224 (goto-char end))
225 (cond
226 ((eobp)
227 (unless (bolp)
228 (insert "\n")))
229 ((eq (char-after) ?\<)
230 (when (looking-at (concat "<\\(emphasis\\|systemitem\\|inlinemediaobject"
231 "\\|u?link\\|anchor\\|email\\)[ >]"))
232 (muse-insert-markup "<para>")))
234 (muse-insert-markup "<para>"))))
236 (defun muse-docbook-get-author (&optional author)
237 "Split the AUTHOR directive into separate fields.
238 AUTHOR should be of the form: \"Firstname Other Names Lastname\",
239 and anything after `Firstname' is optional."
240 (setq author (save-match-data (split-string author)))
241 (let ((num-el (length author)))
242 (cond ((eq num-el 1)
243 (concat "<firstname>" (car author) "</firstname>"))
244 ((eq num-el 2)
245 (concat "<firstname>" (nth 0 author) "</firstname>"
246 "<surname>" (nth 1 author) "</surname>"))
247 ((eq num-el 3)
248 (concat "<firstname>" (nth 0 author) "</firstname>"
249 "<othername>" (nth 1 author) "</othername>"
250 "<surname>" (nth 2 author) "</surname>"))
252 (let (first last)
253 (setq first (car author))
254 (setq author (nreverse (cdr author)))
255 (setq last (car author))
256 (setq author (nreverse (cdr author)))
257 (concat "<firstname>" first "</firstname>"
258 "<othername>"
259 (mapconcat 'identity author " ")
260 "</othername>"
261 "<surname>" last "</surname>"))))))
263 (defun muse-docbook-fixup-images ()
264 (goto-char (point-min))
265 (while (re-search-forward (concat "<imagedata fileref=\"[^\"]+\""
266 " format=\"\\([^\"]+\\)\" />$")
267 nil t)
268 (replace-match (upcase (match-string 1)) t t nil 1)))
270 (defun muse-docbook-finalize-buffer ()
271 (muse-docbook-fixup-images)
272 (when (boundp 'buffer-file-coding-system)
273 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
274 ;; make it agree with the default charset
275 (setq buffer-file-coding-system muse-docbook-encoding-default))))
277 ;; Register the Muse DocBook XML Publisher
279 (unless (assoc "docbook" muse-publishing-styles)
280 (muse-define-style "docbook"
281 :suffix 'muse-docbook-extension
282 :regexps 'muse-docbook-markup-regexps
283 :functions 'muse-docbook-markup-functions
284 :strings 'muse-docbook-markup-strings
285 :specials 'muse-xml-decide-specials
286 :after 'muse-docbook-finalize-buffer
287 :header 'muse-docbook-header
288 :footer 'muse-docbook-footer
289 :browser 'find-file))
291 (provide 'muse-docbook)
293 ;;; muse-docbook.el ends here