Do some early spring cleaning
[muse-el.git] / muse-docbook.el
blobd21b58f6ee0be01b71a428b4ed4680b45e660085
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Muse HTML Publishing
4 ;;
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 (require 'muse-publish)
9 (defgroup muse-docbook nil
10 "Options controlling the behaviour of Muse DocBook XML publishing.
11 See `muse-docbook' for more information."
12 :group 'muse-publish)
14 (defcustom muse-docbook-extension ".xml"
15 "Default file extension for publishing DocBook XML files."
16 :type 'string
17 :group 'muse-docbook)
19 (defcustom muse-docbook-header
20 "<!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\">
21 <article>
22 <articleinfo>
23 <title><lisp>(muse-publishing-directive \"title\")</lisp></title>
24 <author><lisp>(muse-publishing-directive \"author\")</lisp></author>
25 <pubdate><lisp>(muse-publishing-directive \"date\")</lisp></pubdate>
26 </articleinfo>
27 <!-- Page published by Emacs Muse begins here -->\n"
28 "Header used for publishing DocBook XML files."
29 :type '(choice string file)
30 :group 'muse-docbook)
32 (defcustom muse-docbook-footer "
33 <!-- Page published by Emacs Muse ends here -->
34 </article>\n"
35 "Footer used for publishing DocBook XML files."
36 :type '(choice string file)
37 :group 'muse-docbook)
39 (defcustom muse-docbook-markup-regexps
40 `(;; join together the parts of a list or table
41 (10000 "</\\([oud]l\\)>\\s-*<\\1>\\s-*" 0 "")
42 (10100 "</tbody>\\s-*</table>\\s-*<table[^>]*>\\s-*<tbody>\\s-*" 0 "")
43 (10200 "</table>\\s-*<table[^>]*>\\s-*" 0 "")
45 ;; the beginning of the buffer begins the first paragraph
46 (10300 "\\`\n*\\([^<]\\)" 0 "<para>\\1")
47 ;; plain paragraph separator
48 (10400 ,(concat "\\(\n</\\(blockquote\\|center\\)>\\)?\n"
49 "\\([[:blank:]]*\n\\)+\\(<\\(blockquote\\|center\\)>\n\\)?") 0
50 muse-docbook-markup-paragraph)
51 (10500 "\\([^>[:space:]]\\)\\s-+\\'" 0 "\\1</para>\n"))
52 "List of markup rules for publishing a Muse page to DocBook XML.
53 For more on the structure of this list, see `muse-publish-markup-regexps'."
54 :type '(repeat (choice
55 (list :tag "Markup rule"
56 integer
57 (choice regexp symbol)
58 integer
59 (choice string function symbol))
60 function))
61 :group 'muse-docbook)
63 (defcustom muse-docbook-markup-functions
64 '((table . muse-docbook-markup-table))
65 "An alist of style types to custom functions for that kind of text.
66 For more on the structure of this list, see
67 `muse-publish-markup-functions'."
68 :type '(alist :key-type symbol :value-type function)
69 :group 'muse-docbook)
71 (defcustom muse-docbook-markup-strings
72 '((url-link . "<ulink url=\"%s\">%s</ulink>")
73 (email-addr . "<email>%s</email>")
74 (emdash . " &#151; ")
75 (rule . "")
76 (enddots . "....")
77 (dots . "...")
78 (section . "<section><title>")
79 (section-end . "</title>")
80 (subsection . "<section><title>")
81 (subsection-end . "</title>")
82 (subsubsection . "<section><title>")
83 (subsubsection-end . "</title>")
84 (footnote . "<footnote>")
85 (footnote-end . "</footnote>")
86 (begin-underline . "")
87 (end-underline . "")
88 (begin-literal . "<filename>")
89 (end-literal . "</filename>")
90 (begin-emph . "<emphasis>")
91 (end-emph . "</emphasis>")
92 (begin-more-emph . "<emphasis role=\"strong\">")
93 (end-more-emph . "</emphasis>")
94 (begin-most-emph . "<emphasis role=\"strong\"><emphasis>")
95 (end-most-emph . "</emphasis>></emphasis role=\"strong\">")
96 (begin-verse . "<literallayout>\n")
97 (verse-space . " ")
98 (end-verse . "</literallayout>")
99 (begin-example . "<example>")
100 (end-example . "</example>")
101 (begin-center . "<center>\n")
102 (end-center . "\n</center>")
103 (begin-quote . "<blockquote>\n")
104 (end-quote . "\n</blockquote>")
105 (begin-uli . "<itemizedlist mark=\"bullet\">\n<listitem>")
106 (end-uli . "</listitem>\n</itemizedlist>")
107 (begin-oli . "<orderedlist>\n<listitem>")
108 (end-oli . "</listitem>\n</orderedlist>")
109 (begin-ddt . "<dl>\n<dt>")
110 (start-dde . "</dt>\n <dd>")
111 (end-ddt . "</dd>\n</dl>"))
112 "Strings used for marking up text.
113 These cover the most basic kinds of markup, the handling of which
114 differs little between the various styles."
115 :type '(alist :key-type symbol :value-type string)
116 :group 'muse-docbook)
118 (defcustom muse-docbook-markup-specials
119 '((?\" . "&quot;")
120 (?\< . "&lt;")
121 (?\> . "&gt;")
122 (?\& . "&amp;"))
123 "A table of characters which must be represented specially."
124 :type '(alist :key-type character :value-type string)
125 :group 'muse-docbook)
127 (unless (fboundp 'looking-back)
128 (defun looking-back (regexp &optional limit)
129 (save-excursion
130 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
132 (defun muse-docbook-markup-paragraph ()
133 (let ((end (copy-marker (match-end 0) t)))
134 (goto-char (match-beginning 0))
135 (unless (eq (char-before) ?\>) (insert "</para>"))
136 (goto-char end)
137 (unless (eq (char-after) ?\<)
138 (insert "<para>"))))
140 (defun muse-docbook-markup-table ()
141 (let* ((str (save-match-data
142 (if (featurep 'xemacs)
143 ;; more emacs divergence. :(
144 (replace-in-string (match-string 1) " *|+ *$" "")
145 (match-string 1))))
146 (fields (append (save-match-data
147 (split-string str "[[:blank:]]*|+[[:blank:]]*"))
148 (list (match-string 4))))
149 (len (length (match-string 3)))
150 (row (cond ((= len 1) "tbody")
151 ((= len 2) "thead")
152 ((= len 3) "tfoot")))
153 (col "entry"))
154 (concat "<table>\n" "<" row ">\n" "<row>\n<" col ">"
155 (mapconcat 'identity fields (format "</%s><%s>" col col))
156 "</" col ">\n" "</row>\n" "</" row ">\n"
157 "</table>\n")))
159 (defun muse-docbook-fixup-sections ()
160 (goto-char (point-min))
161 (let (last)
162 (while (re-search-forward "\n*<section>" nil t)
163 (when last
164 (replace-match "\n</section>\n\n<section>"))
165 (setq last (match-beginning 0)))
166 (when last
167 (goto-char (point-max))
168 (insert "</section>"))))
170 ;; Register the Muse DocBook XML Publisher
172 (unless (assoc "docbook" muse-publishing-styles)
173 (muse-define-style "docbook"
174 :suffix 'muse-docbook-extension
175 :regexps 'muse-docbook-markup-regexps
176 :functions 'muse-docbook-markup-functions
177 :strings 'muse-docbook-markup-strings
178 :specials 'muse-docbook-markup-specials
179 :before-end 'muse-docbook-fixup-sections
180 :header 'muse-docbook-header
181 :footer 'muse-docbook-footer
182 :browser 'find-file))
184 (provide 'muse-docbook)
186 ;;; muse-docbook.el ends here