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