Release Emacs Muse 3.03!
[muse-el.git] / lisp / muse-xml.el
blob941842c7d41b3f44631f877140cef5a2f36c7917
1 ;;; muse-xml.el --- publish XML files
3 ;; Copyright (C) 2005, 2006, 2007 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 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 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 (enddots . "....")
135 (dots . "...")
136 (section . "<section level=\"1\"><title>")
137 (section-end . "</title>")
138 (subsection . "<section level=\"2\"><title>")
139 (subsection-end . "</title>")
140 (subsubsection . "<section level=\"3\"><title>")
141 (subsubsection-end . "</title>")
142 (section-other . "<section level=\"%s\"><title>")
143 (section-other-end . "</title>")
144 (section-close . "</section>")
145 (footnote . "<footnote>")
146 (footnote-end . "</footnote>")
147 (begin-underline . "<format type=\"underline\">")
148 (end-underline . "</format>")
149 (begin-literal . "<code>")
150 (end-literal . "</code>")
151 (begin-emph . "<format type=\"emphasis\" level=\"1\">")
152 (end-emph . "</format>")
153 (begin-more-emph . "<format type=\"emphasis\" level=\"2\">")
154 (end-more-emph . "</format>")
155 (begin-most-emph . "<format type=\"emphasis\" level=\"3\">")
156 (end-most-emph . "</format>")
157 (begin-verse . "<verse>\n")
158 (begin-verse-line . "<line>")
159 (end-verse-line . "</line>")
160 (empty-verse-line . "<line />")
161 (begin-last-stanza-line . "<line>")
162 (end-last-stanza-line . "</line>")
163 (end-verse . "</verse>")
164 (begin-example . "<example>")
165 (end-example . "</example>")
166 (begin-center . "<p><format type=\"center\">\n")
167 (end-center . "\n</format></p>")
168 (begin-quote . "<blockquote>\n")
169 (end-quote . "\n</blockquote>")
170 (begin-quote-item . "<p>")
171 (end-quote-item . "</p>")
172 (begin-uli . "<list type=\"unordered\">\n")
173 (end-uli . "\n</list>")
174 (begin-uli-item . "<item>")
175 (end-uli-item . "</item>")
176 (begin-oli . "<list type=\"ordered\">\n")
177 (end-oli . "\n</list>")
178 (begin-oli-item . "<item>")
179 (end-oli-item . "</item>")
180 (begin-dl . "<list type=\"definition\">\n")
181 (end-dl . "\n</list>")
182 (begin-dl-item . "<item>\n")
183 (end-dl-item . "\n</item>")
184 (begin-ddt . "<term>")
185 (end-ddt . "</term>")
186 (begin-dde . "<definition>")
187 (end-dde . "</definition>")
188 (begin-table . "<table%s>\n")
189 (end-table . "</table>")
190 (begin-table-row . " <tr>\n")
191 (end-table-row . " </tr>\n")
192 (begin-table-entry . " <%s>")
193 (end-table-entry . "</%s>\n"))
194 "Strings used for marking up text.
195 These cover the most basic kinds of markup, the handling of which
196 differs little between the various styles."
197 :type '(alist :key-type symbol :value-type string)
198 :group 'muse-xml)
200 (defcustom muse-xml-encoding-default 'utf-8
201 "The default Emacs buffer encoding to use in published files.
202 This will be used if no special characters are found."
203 :type 'symbol
204 :group 'muse-xml)
206 (defcustom muse-xml-charset-default "utf-8"
207 "The default XML charset to use if no translation is
208 found in `muse-xml-encoding-map'."
209 :type 'string
210 :group 'muse-xml)
212 (defun muse-xml-encoding ()
213 (muse-xml-transform-content-type
214 (or (and (boundp 'buffer-file-coding-system)
215 buffer-file-coding-system)
216 muse-xml-encoding-default)
217 muse-xml-charset-default))
219 (defun muse-xml-markup-paragraph ()
220 (let ((end (copy-marker (match-end 0) t)))
221 (goto-char (match-beginning 0))
222 (when (save-excursion
223 (save-match-data
224 (and (re-search-backward "<\\(/?\\)p[ >]" nil t)
225 (not (string-equal (match-string 1) "/")))))
226 (when (get-text-property (1- (point)) 'end-list)
227 (goto-char (previous-single-property-change (1- (point)) 'end-list)))
228 (muse-insert-markup "</p>"))
229 (goto-char end))
230 (cond
231 ((eobp)
232 (unless (bolp)
233 (insert "\n")))
234 ((eq (char-after) ?\<)
235 (when (looking-at (concat "<\\(format\\|code\\|link\\|image"
236 "\\|anchor\\|footnote\\)[ >]"))
237 (muse-insert-markup "<p>")))
239 (muse-insert-markup "<p>"))))
241 (defun muse-xml-finalize-buffer ()
242 (when (boundp 'buffer-file-coding-system)
243 (when (memq buffer-file-coding-system '(no-conversion undecided-unix))
244 ;; make it agree with the default charset
245 (setq buffer-file-coding-system muse-xml-encoding-default))))
247 ;;; Register the Muse XML Publisher
249 (muse-define-style "xml"
250 :suffix 'muse-xml-extension
251 :regexps 'muse-xml-markup-regexps
252 :functions 'muse-xml-markup-functions
253 :strings 'muse-xml-markup-strings
254 :specials 'muse-xml-decide-specials
255 :after 'muse-xml-finalize-buffer
256 :header 'muse-xml-header
257 :footer 'muse-xml-footer
258 :browser 'find-file)
260 (provide 'muse-xml)
262 ;;; muse-xml.el ends here