Fix problem with <include> and <contents> tags.
[muse-el.git] / lisp / muse-xml.el
blobf11bd343219ccdd15f430eda02ea0a0bf2d58e0c
1 ;;; muse-xml.el --- publish XML files
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Michael Olson (mwolson AT gnu DOT org)
6 ;; Date: Sat 23-Jul-2005
8 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
10 ;; Emacs Muse is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
15 ;; Emacs Muse is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with Emacs Muse; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; James Clarke's nxml-mode can be used for editing and validating
28 ;; Muse-generated XML files. If you are in nxml-mode use the command
29 ;; C-c C-s C-f to point to the schema in `contrib/muse.rnc', which
30 ;; comes with Muse. Say yes if you are asked if you want to copy the
31 ;; file to your location. C-c C-s C-a can then be used to reload the
32 ;; schema if you make changes to the file.
34 ;;; Contributors:
36 ;; Peter K. Lee (saint AT corenova DOT com) made the initial
37 ;; implementation of planner-publish.el, which was heavily borrowed
38 ;; from.
40 ;; Brad Collins (brad AT chenla DOT org) provided a Compact RelaxNG
41 ;; schema.
43 ;;; Code:
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 ;; Muse XML Publishing
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (require 'muse-publish)
52 (require 'muse-regexps)
53 (require 'muse-xml-common)
55 (defgroup muse-xml nil
56 "Options controlling the behavior of Muse XML publishing.
57 See `muse-xml' for more information."
58 :group 'muse-publish)
60 (defcustom muse-xml-extension ".xml"
61 "Default file extension for publishing XML files."
62 :type 'string
63 :group 'muse-xml)
65 (defcustom muse-xml-header
66 "<?xml version=\"1.0\" encoding=\"<lisp>
67 (muse-xml-encoding)</lisp>\"?>
68 <MUSE>
69 <pageinfo>
70 <title><lisp>(muse-publishing-directive \"title\")</lisp></title>
71 <author><lisp>(muse-publishing-directive \"author\")</lisp></author>
72 <maintainer><lisp>(muse-style-element :maintainer)</lisp></maintainer>
73 <pubdate><lisp>(muse-publishing-directive \"date\")</lisp></pubdate>
74 </pageinfo>
75 <!-- Page published by Emacs Muse begins here -->\n"
76 "Header used for publishing XML files.
77 This may be text or a filename."
78 :type 'string
79 :group 'muse-xml)
81 (defcustom muse-xml-footer "
82 <!-- Page published by Emacs Muse ends here -->
83 </MUSE>\n"
84 "Footer used for publishing Xml XML files.
85 This may be text or a filename."
86 :type 'string
87 :group 'muse-xml)
89 (defcustom muse-xml-markup-regexps
90 `(;; Beginning of doc, end of doc, or plain paragraph separator
91 (10000 ,(concat "\\(\\(\n\\([" muse-regexp-blank "]*\n\\)+\\)"
92 "\\|\\`\\s-*\\|\\s-*\\'\\)")
93 0 muse-xml-markup-paragraph))
94 "List of markup rules for publishing a Muse page to XML.
95 For more on the structure of this list, see `muse-publish-markup-regexps'."
96 :type '(repeat (choice
97 (list :tag "Markup rule"
98 integer
99 (choice regexp symbol)
100 integer
101 (choice string function symbol))
102 function))
103 :group 'muse-xml)
105 (defcustom muse-xml-markup-functions
106 '((anchor . muse-xml-markup-anchor)
107 (table . muse-xml-markup-table))
108 "An alist of style types to custom functions for that kind of text.
109 For more on the structure of this list, see
110 `muse-publish-markup-functions'."
111 :type '(alist :key-type symbol :value-type function)
112 :group 'muse-xml)
114 (defcustom muse-xml-markup-strings
115 '((image-with-desc . "<image href=\"%s.%s\">%s</image>")
116 (image . "<image href=\"%s.%s\"></image>")
117 (image-link . "<link type=\"image\" href=\"%s\">%s.%s</link>")
118 (anchor-ref . "<link type=\"url\" href=\"#%s\">%s</link>")
119 (url . "<link type=\"url\" href=\"%s\">%s</link>")
120 (link . "<link type=\"url\" href=\"%s\">%s</link>")
121 (link-and-anchor . "<link type=\"url\" href=\"%s#%s\">%s</link>")
122 (email-addr . "<link type=\"email\" href=\"%s\">%s</link>")
123 (anchor . "<anchor id=\"%s\" />\n")
124 (emdash . "%s--%s")
125 (comment-begin . "<!-- ")
126 (comment-end . " -->")
127 (rule . "<hr />")
128 (fn-sep . "<hr />\n")
129 (no-break-space . "&nbsp;")
130 (enddots . "....")
131 (dots . "...")
132 (section . "<section level=\"1\"><title>")
133 (section-end . "</title>")
134 (subsection . "<section level=\"2\"><title>")
135 (subsection-end . "</title>")
136 (subsubsection . "<section level=\"3\"><title>")
137 (subsubsection-end . "</title>")
138 (section-other . "<section level=\"%s\"><title>")
139 (section-other-end . "</title>")
140 (section-close . "</section>")
141 (footnote . "<footnote>")
142 (footnote-end . "</footnote>")
143 (begin-underline . "<format type=\"underline\">")
144 (end-underline . "</format>")
145 (begin-literal . "<code>")
146 (end-literal . "</code>")
147 (begin-emph . "<format type=\"emphasis\" level=\"1\">")
148 (end-emph . "</format>")
149 (begin-more-emph . "<format type=\"emphasis\" level=\"2\">")
150 (end-more-emph . "</format>")
151 (begin-most-emph . "<format type=\"emphasis\" level=\"3\">")
152 (end-most-emph . "</format>")
153 (begin-verse . "<verse>\n")
154 (begin-verse-line . "<line>")
155 (end-verse-line . "</line>")
156 (empty-verse-line . "<line />")
157 (begin-last-stanza-line . "<line>")
158 (end-last-stanza-line . "</line>")
159 (end-verse . "</verse>")
160 (begin-example . "<example>")
161 (end-example . "</example>")
162 (begin-center . "<p><format type=\"center\">\n")
163 (end-center . "\n</format></p>")
164 (begin-quote . "<blockquote>\n")
165 (end-quote . "\n</blockquote>")
166 (begin-quote-item . "<p>")
167 (end-quote-item . "</p>")
168 (begin-uli . "<list type=\"unordered\">\n")
169 (end-uli . "\n</list>")
170 (begin-uli-item . "<item>")
171 (end-uli-item . "</item>")
172 (begin-oli . "<list type=\"ordered\">\n")
173 (end-oli . "\n</list>")
174 (begin-oli-item . "<item>")
175 (end-oli-item . "</item>")
176 (begin-dl . "<list type=\"definition\">\n")
177 (end-dl . "\n</list>")
178 (begin-dl-item . "<item>\n")
179 (end-dl-item . "\n</item>")
180 (begin-ddt . "<term>")
181 (end-ddt . "</term>")
182 (begin-dde . "<definition>")
183 (end-dde . "</definition>")
184 (begin-table . "<table%s>\n")
185 (end-table . "</table>")
186 (begin-table-row . " <tr>\n")
187 (end-table-row . " </tr>\n")
188 (begin-table-entry . " <%s>")
189 (end-table-entry . "</%s>\n"))
190 "Strings used for marking up text.
191 These cover the most basic kinds of markup, the handling of which
192 differs little between the various styles."
193 :type '(alist :key-type symbol :value-type string)
194 :group 'muse-xml)
196 (defcustom muse-xml-encoding-default 'utf-8
197 "The default Emacs buffer encoding to use in published files.
198 This will be used if no special characters are found."
199 :type 'symbol
200 :group 'muse-xml)
202 (defcustom muse-xml-charset-default "utf-8"
203 "The default XML charset to use if no translation is
204 found in `muse-xml-encoding-map'."
205 :type 'string
206 :group 'muse-xml)
208 (defun muse-xml-encoding ()
209 (muse-xml-transform-content-type
210 (or (and (boundp 'buffer-file-coding-system)
211 buffer-file-coding-system)
212 muse-xml-encoding-default)
213 muse-xml-charset-default))
215 (defun muse-xml-markup-paragraph ()
216 (let ((end (copy-marker (match-end 0) t)))
217 (goto-char (match-beginning 0))
218 (when (save-excursion
219 (save-match-data
220 (and (re-search-backward "<\\(/?\\)p[ >]"
221 nil t)
222 (not (string-equal (match-string 1) "/")))))
223 (when (get-text-property (1- (point)) 'end-list)
224 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
225 (muse-insert-markup "</p>"))
226 (goto-char end))
227 (cond
228 ((eobp)
229 (unless (bolp)
230 (insert "\n")))
231 ((eq (char-after) ?\<)
232 (when (looking-at (concat "<\\(format\\|code\\|link\\|image"
233 "\\|anchor\\|footnote\\)[ >]"))
234 (muse-insert-markup "<p>")))
236 (muse-insert-markup "<p>"))))
238 (defun muse-xml-finalize-buffer ()
239 (when (boundp 'buffer-file-coding-system)
240 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
241 ;; make it agree with the default charset
242 (setq buffer-file-coding-system muse-xml-encoding-default))))
244 ;;; Register the Muse XML Publisher
246 (muse-define-style "xml"
247 :suffix 'muse-xml-extension
248 :regexps 'muse-xml-markup-regexps
249 :functions 'muse-xml-markup-functions
250 :strings 'muse-xml-markup-strings
251 :specials 'muse-xml-decide-specials
252 :after 'muse-xml-finalize-buffer
253 :header 'muse-xml-header
254 :footer 'muse-xml-footer
255 :browser 'find-file)
257 (provide 'muse-xml)
259 ;;; muse-xml.el ends here