1 ;;; muse-xml.el --- publish XML files
3 ;; Copyright (C) 2005 Free Software Foundation, Inc.
5 ;; This file is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This file is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to the
17 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;; Boston, MA 02110-1301, USA.
22 ;; James Clarke's nxml-mode can be used for editing and validating
23 ;; Muse-generated XML files. If you are in nxml-mode use the command
24 ;; C-c C-s C-f to point to the schema in `contrib/muse.rnc', which
25 ;; comes with Muse. Say yes if you are asked if you want to copy the
26 ;; file to your location. C-c C-s C-a can then be used to reload the
27 ;; schema if you make changes to the file.
31 ;; Peter K. Lee (saint AT corenova DOT com) made the initial
32 ;; implementation of planner-publish.el, which was heavily borrowed
35 ;; Brad Collins (brad AT chenla DOT org) provided a Compact RelaxNG
38 ;; Michael Olson (mwolson AT gnu DOT org) assembled everything into a
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;; Muse XML Publishing
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 (require 'muse-publish
)
50 (require 'muse-regexps
)
52 (defgroup muse-xml nil
53 "Options controlling the behavior of Muse XML publishing.
54 See `muse-xml' for more information."
57 (defcustom muse-xml-extension
".xml"
58 "Default file extension for publishing XML files."
62 (defcustom muse-xml-header
63 "<?xml version=\"1.0\" encoding=\"<lisp>
64 (muse-xml-encoding)</lisp>\"?>
67 <title><lisp>(muse-publishing-directive \"title\")</lisp></title>
68 <author><lisp>(muse-publishing-directive \"author\")</lisp></author>
69 <maintainer><lisp>(muse-style-element :maintainer)</lisp></maintainer>
70 <pubdate><lisp>(muse-publishing-directive \"date\")</lisp></pubdate>
72 <!-- Page published by Emacs Muse begins here -->\n"
73 "Header used for publishing XML files.
74 This may be text or a filename."
78 (defcustom muse-xml-footer
"
79 <!-- Page published by Emacs Muse ends here -->
81 "Footer used for publishing Xml XML files.
82 This may be text or a filename."
86 (defcustom muse-xml-markup-regexps
87 `(;; Join together the parts of a table
88 (10000 ,(concat " </t\\(body\\|head\\|foot\\)>\\s-*</table>"
89 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
90 "[" muse-regexp-blank
"]*"
91 "<table[^>]*>\\s-*<t\\1>\n")
93 (10100 ,(concat "</table>"
94 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
95 "[" muse-regexp-blank
"]*"
99 ;; Join together the parts of a list
100 (10200 ,(concat "</list>"
101 "\\([" muse-regexp-blank
"]*\n\\)\\{0,2\\}"
102 "[" muse-regexp-blank
"]*"
106 ;; Beginning of doc, end of doc, or plain paragraph separator
107 (10300 ,(concat "\\(\n</\\(blockquote\\|format\\)>\\)?"
110 "]*\n\\)+\\)\\|\\`\\s-*\\|\\s-*\\'\\)"
111 "\\(<\\(blockquote\\|format type=\"center\"\\)>\n\\)?")
112 0 muse-xml-markup-paragraph
))
113 "List of markup rules for publishing a Muse page to XML.
114 For more on the structure of this list, see `muse-publish-markup-regexps'."
115 :type
'(repeat (choice
116 (list :tag
"Markup rule"
118 (choice regexp symbol
)
120 (choice string function symbol
))
124 (defcustom muse-xml-markup-functions
125 '((anchor . muse-xml-markup-anchor
)
126 (table . muse-xml-markup-table
))
127 "An alist of style types to custom functions for that kind of text.
128 For more on the structure of this list, see
129 `muse-publish-markup-functions'."
130 :type
'(alist :key-type symbol
:value-type function
)
133 (defcustom muse-xml-markup-strings
134 '((image-with-desc .
"<image href=\"%s\">%s</image>")
135 (image-link .
"<image href=\"%s\"></image>")
136 (url-with-image .
"<link type=\"image\" href=\"%s\">%s</link>")
137 (url-link .
"<link type=\"url\" href=\"%s\">%s</link>")
138 (email-addr .
"<link type=\"email\" href=\"%s\">%s</link>")
140 (comment-begin .
"<!-- ")
141 (comment-end .
" -->")
143 (fn-sep .
"<hr />\n")
146 (section .
"<section level=\"1\"><title>")
147 (section-end .
"</title>")
148 (subsection .
"<section level=\"2\"><title>")
149 (subsection-end .
"</title>")
150 (subsubsection .
"<section level=\"3\"><title>")
151 (subsubsection-end .
"</title>")
152 (section-other .
"<section level=\"%s\"><title>")
153 (section-other-end .
"</title>")
154 (section-close .
"</section>")
155 (footnote .
"<footnote>")
156 (footnote-end .
"</footnote>")
157 (begin-underline .
"<format type=\"underline\">")
158 (end-underline .
"</format>")
159 (begin-literal .
"<code>")
160 (end-literal .
"</code>")
161 (begin-emph .
"<format type=\"emphasis\" level=\"1\">")
162 (end-emph .
"</format>")
163 (begin-more-emph .
"<format type=\"emphasis\" level=\"2\">")
164 (end-more-emph .
"</format>")
165 (begin-most-emph .
"<format type=\"emphasis\" level=\"3\">")
166 (end-most-emph .
"</format>")
167 (begin-verse .
"<verse>\n")
168 (begin-verse-line .
"<line>")
169 (end-verse-line .
"</line>")
170 (empty-verse-line .
"<line />")
171 (begin-last-stanza-line .
"<line>")
172 (end-last-stanza-line .
"</line>")
173 (end-verse .
"</verse>")
174 (begin-example .
"<example>")
175 (end-example .
"</example>")
176 (begin-center .
"<format type=\"center\">\n")
177 (end-center .
"\n</format>")
178 (begin-quote .
"<blockquote>\n")
179 (end-quote .
"\n</blockquote>")
180 (begin-uli .
"<list type=\"unordered\">\n<item>")
181 (end-uli .
"</item>\n</list>")
182 (begin-oli .
"<list type=\"ordered\">\n<item>")
183 (end-oli .
"</item>\n</list>")
184 (begin-ddt .
"<list type=\"definition\">\n<item><term>")
185 (start-dde .
"</term>\n<definition>")
186 (end-ddt .
"</definition>\n</item>\n</list>"))
187 "Strings used for marking up text.
188 These cover the most basic kinds of markup, the handling of which
189 differs little between the various styles."
190 :type
'(alist :key-type symbol
:value-type string
)
193 (defcustom muse-xml-markup-specials
198 "A table of characters which must be represented specially."
199 :type
'(alist :key-type character
:value-type string
)
202 (defcustom muse-xml-encoding-default
'utf-8
203 "The default Emacs buffer encoding to use in published files.
204 This will be used if no special characters are found."
208 (defcustom muse-xml-charset-default
"utf-8"
209 "The default Xml XML charset to use if no translation is
210 found in `muse-xml-encoding-map'."
214 (defcustom muse-xml-encoding-map
215 '((iso-8859-1 .
"iso-8859-1")
216 (iso-2022-jp .
"iso-2022-jp")
218 (japanese-iso-8bit .
"euc-jp")
219 (chinese-big5 .
"big5")
220 (mule-utf-8 .
"utf-8")
221 (chinese-iso-8bit .
"gb2312")
222 (chinese-gbk .
"gbk"))
223 "An alist mapping emacs coding systems to appropriate Xml charsets.
224 Use the base name of the coding system (i.e. without the -unix)."
225 :type
'(alist :key-type coding-system
:value-type string
)
228 (defun muse-xml-transform-content-type (content-type)
229 "Using `muse-xml-encoding-map', try and resolve an emacs
230 coding system to an associated XML coding system. If no
231 match is found, `muse-xml-charset-default' is used instead."
232 (let ((match (and (fboundp 'coding-system-base
)
233 (assoc (coding-system-base content-type
)
234 muse-xml-encoding-map
))))
237 muse-xml-charset-default
)))
239 (defun muse-xml-encoding ()
240 (muse-xml-transform-content-type
241 (or (and (boundp 'buffer-file-coding-system
)
242 buffer-file-coding-system
)
243 muse-xml-encoding-default
)))
245 (defun muse-xml-markup-paragraph ()
246 (let ((end (copy-marker (match-end 0) t
)))
247 (goto-char (match-beginning 0))
248 (when (save-excursion
250 (and (re-search-backward "<\\(/?\\)p[ >]"
252 (not (string-equal (match-string 1) "/")))))
259 ((eq (char-after) ?\
<)
260 (when (looking-at (concat "<\\(format\\|code\\|link\\|image"
261 "\\|anchor\\|footnote\\)[ >]"))
266 (defun muse-xml-insert-anchor (anchor)
267 "Insert an anchor, either around the word at point, or within a tag."
268 (skip-chars-forward muse-regexp-space
)
269 (when (looking-at "<\\([^ />\n]+\\)>")
270 (goto-char (match-end 0)))
271 (insert "<anchor id=\"" anchor
"\" />\n"))
273 (defun muse-xml-markup-anchor ()
275 (muse-xml-insert-anchor (match-string 2)))
278 (defun muse-xml-markup-table ()
281 (delete-region (match-beginning 0) (match-end 0))))
282 (fields (split-string str
"\\s-*|+\\s-*"))
283 (type (and (string-match "\\s-*\\(|+\\)\\s-*" str
)
284 (length (match-string 1 str
))))
285 (part (cond ((= type
1) "tbody")
287 ((= type
3) "tfoot")))
288 (col (cond ((= type
1) "td")
294 (dolist (field fields
)
295 (insert " <" col
">" field
"</" col
">\n"))
300 (defun muse-xml-fixup-tables ()
302 (goto-char (point-min))
304 (while (re-search-forward "^<table[^>]*>$" nil t
)
305 (unless (get-text-property (point) 'read-only
)
309 (narrow-to-region beg
(and (re-search-forward "^</table>"
311 (match-beginning 0))))
312 (goto-char (point-min))
313 (let ((inhibit-read-only t
))
317 (if (re-search-forward
318 "^\\s-*<t\\(head\\|body\\|foot\\)>$" nil t
)
319 (goto-char (match-beginning 0))
320 (goto-char (point-max)))))
323 (if (re-search-forward
324 "^\\s-*</t\\(head\\|body\\|foot\\)>$" nil t
)
325 (goto-char (match-end 0))
326 (goto-char (point-max)))))
329 (looking-at "\\s-*<t\\(head\\|body\\|foot\\)>")
330 (cond ((string= (match-string 1) "head") 1)
331 ((string= (match-string 1) "foot") 2)
334 (defun muse-xml-finalize-buffer ()
335 (when (boundp 'buffer-file-coding-system
)
336 (when (memq buffer-file-coding-system
'(no-conversion undecided-unix
))
337 ;; make it agree with the default charset
338 (setq buffer-file-coding-system muse-xml-encoding-default
))))
340 ;; Register the Muse XML Publisher
342 (unless (assoc "xml" muse-publishing-styles
)
343 (muse-define-style "xml"
344 :suffix
'muse-xml-extension
345 :regexps
'muse-xml-markup-regexps
346 :functions
'muse-xml-markup-functions
347 :strings
'muse-xml-markup-strings
348 :specials
'muse-xml-markup-specials
349 :before-end
'muse-xml-fixup-tables
350 :after
'muse-xml-finalize-buffer
351 :header
'muse-xml-header
352 :footer
'muse-xml-footer
353 :browser
'find-file
))
357 ;;; muse-xml.el ends here