muse-publish: Implement muse-publish-enable-dangerous-tags.
[muse-el.git] / lisp / muse-xml.el
blobef051d2dcd8492c293efa09efa701477f173cdb6
1 ;;; muse-xml.el --- publish XML files
3 ;; Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Michael Olson <mwolson@gnu.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 3, 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 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 "\\([" muse-regexp-blank "]*\n\\)\\)"
93 "\\|\\`\\s-*\\|\\s-*\\'\\)")
94 ;; this is somewhat repetitive because we only require the
95 ;; line just before the paragraph beginning to be not
96 ;; read-only
97 3 muse-xml-markup-paragraph))
98 "List of markup rules for publishing a Muse page to XML.
99 For more on the structure of this list, see `muse-publish-markup-regexps'."
100 :type '(repeat (choice
101 (list :tag "Markup rule"
102 integer
103 (choice regexp symbol)
104 integer
105 (choice string function symbol))
106 function))
107 :group 'muse-xml)
109 (defcustom muse-xml-markup-functions
110 '((anchor . muse-xml-markup-anchor)
111 (table . muse-xml-markup-table))
112 "An alist of style types to custom functions for that kind of text.
113 For more on the structure of this list, see
114 `muse-publish-markup-functions'."
115 :type '(alist :key-type symbol :value-type function)
116 :group 'muse-xml)
118 (defcustom muse-xml-markup-strings
119 '((image-with-desc . "<image href=\"%s.%s\">%s</image>")
120 (image . "<image href=\"%s.%s\"></image>")
121 (image-link . "<link type=\"image\" href=\"%s\">%s.%s</link>")
122 (anchor-ref . "<link type=\"url\" href=\"#%s\">%s</link>")
123 (url . "<link type=\"url\" href=\"%s\">%s</link>")
124 (link . "<link type=\"url\" href=\"%s\">%s</link>")
125 (link-and-anchor . "<link type=\"url\" href=\"%s#%s\">%s</link>")
126 (email-addr . "<link type=\"email\" href=\"%s\">%s</link>")
127 (anchor . "<anchor id=\"%s\" />\n")
128 (emdash . "%s--%s")
129 (comment-begin . "<!-- ")
130 (comment-end . " -->")
131 (rule . "<hr />")
132 (fn-sep . "<hr />\n")
133 (no-break-space . "&nbsp;")
134 (line-break . "<br>")
135 (enddots . "....")
136 (dots . "...")
137 (section . "<section level=\"1\"><title>")
138 (section-end . "</title>")
139 (subsection . "<section level=\"2\"><title>")
140 (subsection-end . "</title>")
141 (subsubsection . "<section level=\"3\"><title>")
142 (subsubsection-end . "</title>")
143 (section-other . "<section level=\"%s\"><title>")
144 (section-other-end . "</title>")
145 (section-close . "</section>")
146 (footnote . "<footnote>")
147 (footnote-end . "</footnote>")
148 (begin-underline . "<format type=\"underline\">")
149 (end-underline . "</format>")
150 (begin-literal . "<code>")
151 (end-literal . "</code>")
152 (begin-emph . "<format type=\"emphasis\" level=\"1\">")
153 (end-emph . "</format>")
154 (begin-more-emph . "<format type=\"emphasis\" level=\"2\">")
155 (end-more-emph . "</format>")
156 (begin-most-emph . "<format type=\"emphasis\" level=\"3\">")
157 (end-most-emph . "</format>")
158 (begin-verse . "<verse>\n")
159 (begin-verse-line . "<line>")
160 (end-verse-line . "</line>")
161 (empty-verse-line . "<line />")
162 (begin-last-stanza-line . "<line>")
163 (end-last-stanza-line . "</line>")
164 (end-verse . "</verse>")
165 (begin-example . "<example>")
166 (end-example . "</example>")
167 (begin-center . "<p><format type=\"center\">\n")
168 (end-center . "\n</format></p>")
169 (begin-quote . "<blockquote>\n")
170 (end-quote . "\n</blockquote>")
171 (begin-cite . "<cite>")
172 (begin-cite-author . "<cite type=\"author\">")
173 (begin-cite-year . "<cite type=\"year\">")
174 (end-cite . "</cite>")
175 (begin-quote-item . "<p>")
176 (end-quote-item . "</p>")
177 (begin-uli . "<list type=\"unordered\">\n")
178 (end-uli . "\n</list>")
179 (begin-uli-item . "<item>")
180 (end-uli-item . "</item>")
181 (begin-oli . "<list type=\"ordered\">\n")
182 (end-oli . "\n</list>")
183 (begin-oli-item . "<item>")
184 (end-oli-item . "</item>")
185 (begin-dl . "<list type=\"definition\">\n")
186 (end-dl . "\n</list>")
187 (begin-dl-item . "<item>\n")
188 (end-dl-item . "\n</item>")
189 (begin-ddt . "<term>")
190 (end-ddt . "</term>")
191 (begin-dde . "<definition>")
192 (end-dde . "</definition>")
193 (begin-table . "<table%s>\n")
194 (end-table . "</table>")
195 (begin-table-row . " <tr>\n")
196 (end-table-row . " </tr>\n")
197 (begin-table-entry . " <%s>")
198 (end-table-entry . "</%s>\n"))
199 "Strings used for marking up text.
200 These cover the most basic kinds of markup, the handling of which
201 differs little between the various styles."
202 :type '(alist :key-type symbol :value-type string)
203 :group 'muse-xml)
205 (defcustom muse-xml-encoding-default 'utf-8
206 "The default Emacs buffer encoding to use in published files.
207 This will be used if no special characters are found."
208 :type 'symbol
209 :group 'muse-xml)
211 (defcustom muse-xml-charset-default "utf-8"
212 "The default XML charset to use if no translation is
213 found in `muse-xml-encoding-map'."
214 :type 'string
215 :group 'muse-xml)
217 (defun muse-xml-encoding ()
218 (muse-xml-transform-content-type
219 (or (and (boundp 'buffer-file-coding-system)
220 buffer-file-coding-system)
221 muse-xml-encoding-default)
222 muse-xml-charset-default))
224 (defun muse-xml-markup-paragraph ()
225 (let ((end (copy-marker (match-end 0) t)))
226 (goto-char (match-beginning 0))
227 (when (save-excursion
228 (save-match-data
229 (and (re-search-backward "<\\(/?\\)p[ >]" nil t)
230 (not (string-equal (match-string 1) "/")))))
231 (when (get-text-property (1- (point)) 'end-list)
232 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
233 (muse-insert-markup "</p>"))
234 (goto-char end))
235 (cond
236 ((eobp)
237 (unless (bolp)
238 (insert "\n")))
239 ((eq (char-after) ?\<)
240 (when (looking-at (concat "<\\(format\\|code\\|link\\|image"
241 "\\|anchor\\|footnote\\)[ >]"))
242 (muse-insert-markup "<p>")))
244 (muse-insert-markup "<p>"))))
246 (defun muse-xml-finalize-buffer ()
247 (when (boundp 'buffer-file-coding-system)
248 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
249 ;; make it agree with the default charset
250 (setq buffer-file-coding-system muse-xml-encoding-default))))
252 ;;; Register the Muse XML Publisher
254 (muse-define-style "xml"
255 :suffix 'muse-xml-extension
256 :regexps 'muse-xml-markup-regexps
257 :functions 'muse-xml-markup-functions
258 :strings 'muse-xml-markup-strings
259 :specials 'muse-xml-decide-specials
260 :after 'muse-xml-finalize-buffer
261 :header 'muse-xml-header
262 :footer 'muse-xml-footer
263 :browser 'find-file)
265 (provide 'muse-xml)
267 ;;; muse-xml.el ends here