Don't interpret enddots, dots, and rule inside of links.
[muse-el.git] / lisp / muse-publish.el
blobde89e66c1a286a31f431049d800712ac7d376470
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
10 ;; version.
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 ;; for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;; Yann Hodique (yann DOT hodique AT gmail DOT com) fixed an
27 ;; unnecessary URL description transform in `muse-publish-url'.
29 ;; Peter K. Lee (saint AT corenova DOT com) provided the
30 ;; `muse-style-elements-list' function.
32 ;;; Code:
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;; Muse Publishing
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 (require 'muse)
41 (require 'muse-regexps)
43 (defgroup muse-publish nil
44 "Options controlling the general behavior of Muse publishing."
45 :group 'muse)
47 (defcustom muse-before-publish-hook nil
48 "A hook run in the buffer to be published, before it is done."
49 :type 'hook
50 :group 'muse-publish)
52 (defcustom muse-after-publish-hook nil
53 "A hook run in the buffer to be published, after it is done."
54 :type 'hook
55 :group 'muse-publish)
57 (defcustom muse-publish-url-transforms
58 '(muse-publish-escape-specials-in-string
59 muse-publish-prepare-url
60 muse-resolve-url)
61 "A list of functions used to prepare URLs for publication.
62 Each is passed the URL. The transformed URL should be returned."
63 :type 'hook
64 :options '(muse-publish-escape-specials-in-string
65 muse-publish-prepare-url
66 muse-resolve-url)
67 :group 'muse-publish)
69 (defcustom muse-publish-desc-transforms
70 '(muse-publish-escape-specials-in-string)
71 "A list of functions used to prepare URL desciptions for publication.
72 Each is passed the description. The modified description should
73 be returned."
74 :type 'hook
75 :options '(muse-publish-escape-specials-in-string)
76 :group 'muse-publish)
78 (defcustom muse-publish-comments-p nil
79 "If nil, remove comments before publishing.
80 If non-nil, publish comments using the markup of the current style."
81 :type 'boolean
82 :group 'muse-publish)
84 (defcustom muse-publish-report-threshhold 100000
85 "If a file is this size or larger, report publishing progress."
86 :type 'integer
87 :group 'muse-publish)
89 (defcustom muse-publish-markup-regexps
90 `(;; Remove leading and trailing whitespace from the file
91 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
93 ;; Remove trailing whitespace from all lines
94 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
96 ;; Handle any leading #directives
97 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
99 ;; markup tags
100 (1300 muse-tag-regexp 0 tag)
102 ;; commented lines
103 (1350 "^;\\s-+\\(.+\\)" 0 comment)
105 ;; define anchor points
106 (1400 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
108 ;; prevent emphasis characters in explicit links from being marked
109 (1500 muse-explicit-link-regexp 0 muse-publish-mark-noemphasis)
111 ;; emphasized or literal text
112 (1600 ,(concat
113 "\\(^\\|[-["
114 muse-regexp-space
115 "<('`\"]\\)\\(=[^="
116 muse-regexp-space
117 "]\\|_[^_"
118 muse-regexp-space
119 "]\\|\\*+[^*"
120 muse-regexp-space
121 "]\\)")
122 2 word)
124 ;; headings, outline-mode style
125 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
127 ;; ellipses
128 (1800 "\\.\\.\\.\\." 0 enddots)
129 (1850 "\\.\\.\\." 0 dots)
131 ;; horizontal rule, or section separator
132 (1900 "^----+" 0 rule)
134 ;; beginning of footnotes section
135 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
136 ;; footnote definition/reference (def if at beginning of line)
137 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
139 ;; unnumbered List items begin with a -. numbered list items
140 ;; begin with number and a period. definition lists have a
141 ;; leading term separated from the body with ::. centered
142 ;; paragraphs begin with at least six columns of whitespace; any
143 ;; other whitespace at the beginning indicates a blockquote. The
144 ;; reason all of these rules are handled here, is so that
145 ;; blockquote detection doesn't interfere with indented list
146 ;; members.
147 (2200 ,(concat "^["
148 muse-regexp-blank
149 "]+\\(-["
150 muse-regexp-blank
151 "]*\\|[0-9]+\\.["
152 muse-regexp-blank
153 "]*\\|\\(?:.+?\\)["
154 muse-regexp-blank
155 "]+::\n?\\)")
156 1 list)
158 (2300 ,(concat "^\\(\\(?:.+?\\)["
159 muse-regexp-blank
160 "]+::\n?\\)")
161 0 list)
163 (2400 ,(concat "^\\(["
164 muse-regexp-blank
165 "]+\\)")
166 0 quote)
168 (2500 ,(concat "\\(^\\|["
169 muse-regexp-blank
170 "]+\\)--\\($\\|["
171 muse-regexp-blank
172 "]+\\)")
173 0 emdash)
175 ;; "verse" text is indicated the same way as a quoted e-mail
176 ;; response: "> text", where text may contain initial whitespace
177 ;; (see below).
178 (2600 ,(concat "^["
179 muse-regexp-blank
180 "]*> ")
181 0 verse)
183 ;; simple table markup is supported, nothing fancy. use | to
184 ;; separate cells, || to separate header cells, and ||| for footer
185 ;; cells
186 (2700 ,(concat "^["
187 muse-regexp-blank
188 "]*\\(.+?\\(["
189 muse-regexp-blank
190 "]+|+["
191 muse-regexp-blank
192 "]+.+?\\)\\)$")
193 0 table)
195 ;; replace links in the buffer (links to other pages)
196 (2900 muse-explicit-link-regexp 0 link)
198 ;; bare URLs
199 (3000 muse-url-regexp 0 url)
201 ;; bare email addresses
202 (3500
203 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
205 "List of markup rules for publishing a page with Muse.
206 The rules given in this variable are invoked first, followed by
207 whatever rules are specified by the current style.
209 Each member of the list is either a function, or a list of the form:
211 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
213 REGEXP is a regular expression, or symbol whose value is a regular
214 expression, which is searched for using `re-search-forward'.
215 TEXT-BEGIN-GROUP is the matching group within that regexp which
216 denotes the beginning of the actual text to be marked up.
217 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
218 If it is not a string, but a function, it will be called to determine
219 what the replacement text should be (it must return a string). If it
220 is a symbol, the value of that symbol should be a string.
222 The replacements are done in order, one rule at a time. Writing
223 the regular expressions can be a tricky business. Note that case
224 is never ignored. `case-fold-search' is always bound to nil
225 while processing the markup rules."
226 :type '(repeat (choice
227 (list :tag "Markup rule"
228 integer
229 (choice regexp symbol)
230 integer
231 (choice string function symbol))
232 function))
233 :group 'muse-publish)
235 (defcustom muse-publish-markup-functions
236 '((directive . muse-publish-markup-directive)
237 (comment . muse-publish-markup-comment)
238 (anchor . muse-publish-markup-anchor)
239 (tag . muse-publish-markup-tag)
240 (word . muse-publish-markup-word)
241 (emdash . muse-publish-markup-emdash)
242 (enddots . muse-publish-markup-enddots)
243 (dots . muse-publish-markup-dots)
244 (rule . muse-publish-markup-rule)
245 (heading . muse-publish-markup-heading)
246 (footnote . muse-publish-markup-footnote)
247 (fn-sep . muse-publish-markup-fn-sep)
248 (list . muse-publish-markup-list)
249 (quote . muse-publish-markup-quote)
250 (verse . muse-publish-markup-verse)
251 (table . muse-publish-markup-table)
252 (email . muse-publish-markup-email)
253 (link . muse-publish-markup-link)
254 (url . muse-publish-markup-url))
255 "An alist of style types to custom functions for that kind of text.
257 Each member of the list is of the form:
259 (SYMBOL FUNCTION)
261 SYMBOL describes the type of text to associate with this rule.
262 `muse-publish-markup-regexps' maps regexps to these symbols.
264 FUNCTION is the function to use to mark up this kind of rule if
265 no suitable function is found through the :functions tag of the
266 current style."
267 :type '(alist :key-type symbol :value-type function)
268 :group 'muse-publish)
270 (defcustom muse-publish-markup-tags
271 '(("contents" nil t muse-publish-contents-tag)
272 ("verse" t nil muse-publish-verse-tag)
273 ("example" t nil muse-publish-example-tag)
274 ("code" t nil muse-publish-code-tag)
275 ("literal" t nil muse-publish-mark-read-only)
276 ("verbatim" t nil muse-publish-verbatim-tag)
277 ("lisp" t nil muse-publish-lisp-tag)
278 ("class" t t muse-publish-class-tag)
279 ("command" t t muse-publish-command-tag)
280 ("comment" t nil muse-publish-comment-tag))
281 "A list of tag specifications, for specially marking up text.
282 XML-style tags are the best way to add custom markup to Muse.
283 This is easily accomplished by customizing this list of markup tags.
285 For each entry, the name of the tag is given, whether it expects
286 a closing tag and/or an optional set of attributes, and a
287 function that performs whatever action is desired within the
288 delimited region.
290 The tags themselves are deleted during publishing, before the
291 function is called. The function is called with three arguments,
292 the beginning and end of the region surrounded by the tags. If
293 properties are allowed, they are passed as a third argument in
294 the form of an alist. The `end' argument to the function is
295 always a marker.
297 Point is always at the beginning of the region within the tags, when
298 the function is called. Wherever point is when the function finishes
299 is where tag markup will resume.
301 These tag rules are processed once at the beginning of markup, and
302 once at the end, to catch any tags which may have been inserted
303 in-between."
304 :type '(repeat (list (string :tag "Markup tag")
305 (boolean :tag "Expect closing tag" :value t)
306 (boolean :tag "Parse attributes" :value nil)
307 function))
308 :group 'muse-publish)
310 (defcustom muse-publish-markup-specials nil
311 "A table of characters which must be represented specially."
312 :type '(alist :key-type character :value-type string)
313 :group 'muse-publish)
315 (defvar muse-publishing-p nil
316 "Set to t while a page is being published.")
317 (defvar muse-batch-publishing-p nil
318 "Set to t while a page is being batch published.")
319 (defvar muse-publishing-styles nil)
320 (defvar muse-publishing-current-file nil)
321 (defvar muse-publishing-current-style nil)
322 (defvar muse-publishing-directives nil
323 "An alist of publishing directives from the top of a file.")
324 (defvar muse-publish-generate-contents nil
325 "Non-nil if a table of contents should be generated.
326 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
327 tell where the <contents> was seen, and to what depth the
328 contents were requested.")
329 (defvar muse-publishing-last-position nil
330 "Last position of the point when publishing.
331 This is used to make sure that publishing doesn't get stalled.")
333 ;; Functions for handling style information
335 (defsubst muse-style (&optional style)
336 "Resolve the given STYLE into a Muse style, if it is a string."
337 (if (null style)
338 muse-publishing-current-style
339 (if (stringp style)
340 (assoc style muse-publishing-styles)
341 (muse-assert (consp style))
342 style)))
344 (defun muse-define-style (name &rest elements)
345 (let ((entry (assoc name muse-publishing-styles)))
346 (if entry
347 (error "There is already a style named %s." name)
348 (setq muse-publishing-styles
349 (cons (append (list name) elements)
350 muse-publishing-styles)))))
352 (defun muse-derive-style (new-name base-name &rest elements)
353 (let ((entry (assoc new-name muse-publishing-styles)))
354 (if entry
355 (error "There is already a style named %s." new-name)
356 (apply 'muse-define-style new-name
357 (append elements (list :base base-name))))))
359 (defsubst muse-get-keyword (keyword list &optional direct)
360 (let ((value (cadr (memq keyword list))))
361 (if (and (not direct) (symbolp value))
362 (symbol-value value)
363 value)))
365 (defun muse-style-elements-list (elem &optional style)
366 "Return a list all references to ELEM in STYLE, including base styles.
367 If STYLE is not specified, use current style."
368 (let (base elements)
369 (while style
370 (setq style (muse-style style))
371 (setq elements (append elements
372 (muse-get-keyword elem style)))
373 (setq style (muse-get-keyword :base style)))
374 elements))
376 (defsubst muse-style-element (elem &optional style direct)
377 "Search for ELEM in STYLE, including base styles.
378 If STYLE is not specified, use current style."
379 (setq style (muse-style style))
380 (let ((value (muse-get-keyword elem style direct)))
381 (if value
382 value
383 (let ((base (muse-get-keyword :base style)))
384 (if base
385 (muse-style-element elem base direct))))))
387 (defun muse-find-markup-element (keyword ident style)
388 (let ((def (assq ident (muse-style-element keyword style))))
389 (if def
390 (cdr def)
391 (let ((base (muse-style-element :base style)))
392 (if base
393 (muse-find-markup-element keyword ident base))))))
395 (defsubst muse-markup-text (ident &rest args)
396 (let ((text (muse-find-markup-element :strings ident (muse-style))))
397 (if (and text args)
398 (apply 'format text args)
399 (or text ""))))
401 (defun muse-find-markup-tag (keyword tagname style)
402 (let ((def (assoc tagname (muse-style-element keyword style))))
403 (or def
404 (let ((base (muse-style-element :base style)))
405 (if base
406 (muse-find-markup-tag keyword tagname base))))))
408 (defsubst muse-markup-tag-info (tagname &rest args)
409 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
410 (or tag-info
411 (assoc (match-string 1) muse-publish-markup-tags))))
413 (defsubst muse-markup-function (category)
414 (let ((func (muse-find-markup-element :functions category (muse-style))))
415 (or func
416 (cdr (assq category muse-publish-markup-functions)))))
418 ;; Publishing routines
420 (defun muse-publish-markup (name rules)
421 (let* ((case-fold-search nil)
422 (inhibit-read-only t)
423 (limit (* (length rules) (point-max)))
424 (verbose (and muse-publish-report-threshhold
425 (> (point-max) muse-publish-report-threshhold)))
426 (base 0))
427 (while rules
428 (goto-char (point-min))
429 (let ((regexp (nth 1 (car rules)))
430 (group (nth 2 (car rules)))
431 (repl (nth 3 (car rules)))
432 pos)
433 (setq muse-publishing-last-position nil)
434 (if (symbolp regexp)
435 (setq regexp (symbol-value regexp)))
436 (if (and verbose (not muse-batch-publishing-p))
437 (message "Publishing %s...%d%%" name
438 (* (/ (float (+ (point) base)) limit) 100)))
439 (while (and regexp (setq pos (re-search-forward regexp nil t)))
440 (if (and verbose (not muse-batch-publishing-p))
441 (message "Publishing %s...%d%%" name
442 (* (/ (float (+ (point) base)) limit) 100)))
443 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
444 (get-text-property (match-beginning group) 'read-only))
445 (let* (func
446 (text (cond
447 ((and (symbolp repl)
448 (setq func (muse-markup-function repl)))
449 (funcall func))
450 ((functionp repl)
451 (funcall repl))
452 ((symbolp repl)
453 (symbol-value repl))
454 (t repl))))
455 (if text
456 (replace-match text t))))
457 (if (and muse-publishing-last-position
458 (= pos muse-publishing-last-position))
459 (if (eobp)
460 (setq regexp nil)
461 (forward-char 1)))
462 (setq muse-publishing-last-position pos)))
463 (setq rules (cdr rules)
464 base (+ base (point-max))))
465 (if (and verbose (not muse-batch-publishing-p))
466 (message "Publishing %s...done" name))))
468 (defun muse-insert-file-or-string (file-or-string &optional title)
469 (let ((beg (point)) end)
470 (if (and (not (string-equal file-or-string ""))
471 (file-readable-p file-or-string))
472 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
473 (insert file-or-string)
474 (setq end (point)))
475 (save-restriction
476 (narrow-to-region beg end)
477 (muse-publish-markup (or title "")
478 '((100 "<\\(lisp\\)>" 0
479 muse-publish-markup-tag))))))
481 (defun muse-style-run-hooks (keyword style &rest args)
482 (let (handled)
483 (while (and style (not handled))
484 (setq style (muse-style style))
485 (let ((func (muse-get-keyword keyword style t)))
486 (if func
487 (if (apply func args)
488 (setq handled t))))
489 (unless handled
490 (setq style (muse-style-element :base style))))
491 handled))
493 (defun muse-publish-markup-region (beg end title style)
494 "Apply the given STYLE's markup rules to the given region."
495 (save-restriction
496 (narrow-to-region beg end)
497 (muse-style-run-hooks :before style)
498 (muse-publish-markup
499 title
500 (sort (copy-alist (append muse-publish-markup-regexps
501 (muse-style-elements-list :regexps style)))
502 (function
503 (lambda (l r)
504 (< (car l) (car r))))))
505 (muse-style-run-hooks :before-end style)))
507 (defun muse-publish-markup-buffer (title style)
508 "Apply the given STYLE's markup rules to the current buffer."
509 (setq style (muse-style style))
510 (let ((style-header (muse-style-element :header style))
511 (style-footer (muse-style-element :footer style))
512 (muse-publishing-current-style style)
513 (muse-publishing-directives
514 (list (cons "title" title)
515 (cons "author" (user-full-name))
516 (cons "date" (format-time-string
517 "%B %e, %Y"
518 (nth 5 (file-attributes
519 muse-publishing-current-file))))))
520 (muse-publishing-p t))
521 (run-hooks 'muse-before-publish-hook)
522 (muse-publish-markup-region (point-min) (point-max) title style)
523 (goto-char (point-min))
524 (if style-header (muse-insert-file-or-string style-header title))
525 (goto-char (point-max))
526 (if style-footer (muse-insert-file-or-string style-footer title))
527 (muse-style-run-hooks :after style)
528 (run-hooks 'muse-after-publish-hook)))
530 (defun muse-publish-markup-string (string &optional style)
531 "Markup STRING using the given STYLE's markup rules."
532 (setq style (muse-style style))
533 (muse-with-temp-buffer
534 (insert string)
535 (let ((muse-publishing-current-style style)
536 (muse-publishing-p t))
537 (muse-publish-markup "*string*" (muse-style-element :rules style)))
538 (buffer-string)))
540 ;; Commands for publishing files
542 (defsubst muse-publish-get-style ()
543 (if (= 1 (length muse-publishing-styles))
544 (car muse-publishing-styles)
545 (assoc (completing-read "Publish with style: "
546 muse-publishing-styles nil t)
547 muse-publishing-styles)))
549 (defsubst muse-publish-get-output-dir (style)
550 (let ((default-directory (or (muse-style-element :path style)
551 default-directory)))
552 (muse-read-directory-name "Publish to directory: " nil default-directory)))
554 (defsubst muse-publish-get-info ()
555 (let ((style (muse-publish-get-style)))
556 (list style (muse-publish-get-output-dir style)
557 current-prefix-arg)))
559 (defsubst muse-publish-output-name (&optional file style)
560 (setq style (muse-style style))
561 (concat (muse-style-element :prefix style)
562 (muse-page-name file)
563 (muse-style-element :suffix style)))
565 (defsubst muse-publish-output-file (file &optional output-dir style)
566 (setq style (muse-style style))
567 (if output-dir
568 (expand-file-name (muse-publish-output-name file style) output-dir)
569 (concat (file-name-directory file)
570 (muse-publish-output-name file style))))
572 (defsubst muse-publish-link-name (&optional file style)
573 (setq style (muse-style style))
574 (concat (muse-style-element :prefix style)
575 (muse-page-name file)
576 (or (muse-style-element :link-suffix style)
577 (muse-style-element :suffix style))))
579 (defsubst muse-publish-link-file (file &optional output-dir style)
580 (setq style (muse-style style))
581 (if output-dir
582 (expand-file-name (muse-publish-link-name file style) output-dir)
583 (concat (file-name-directory file)
584 (muse-publish-link-name file style))))
586 (defun muse-publish-file (file style &optional output-dir force)
587 "Publish the given file in list FILES.
588 If the argument FORCE is nil, each file is only published if it is
589 newer than the published version. If the argument FORCE is non-nil,
590 the file is published no matter what."
591 (interactive (cons (read-file-name "Publish file: ")
592 (muse-publish-get-info)))
593 (setq style (muse-style style))
594 (let* ((output-path (muse-publish-output-file file output-dir style))
595 (output-suffix (muse-style-element :osuffix style))
596 (muse-publishing-current-file file)
597 (target (if output-suffix
598 (concat (file-name-sans-extension output-path)
599 output-suffix)
600 output-path)))
601 (when (or force (file-newer-than-file-p file target))
602 (if (and muse-publish-report-threshhold
603 (> (nth 7 (file-attributes file))
604 muse-publish-report-threshhold))
605 (message "Publishing %s ..." file))
606 (muse-with-temp-buffer
607 (insert-file-contents file)
608 (muse-publish-markup-buffer (muse-page-name file) style)
609 (let ((backup-inhibited t))
610 (write-file output-path))
611 (muse-style-run-hooks :final style file output-path target))
612 t)))
614 (defun muse-publish-this-file (style output-dir &optional force)
615 "Publish the page in the current file."
616 (interactive (muse-publish-get-info))
617 (unless (muse-publish-file buffer-file-name style output-dir force)
618 (message (concat "The published version is up-to-date; use"
619 " C-u C-c C-t to force an update."))))
621 (defun muse-batch-publish-files ()
622 "Publish Muse files in batch mode."
623 (let ((muse-batch-publishing-p t)
624 style-name style output-dir)
625 (setq style-name (car command-line-args-left)
626 style (muse-style style-name)
627 command-line-args-left (cdr command-line-args-left)
628 output-dir (car command-line-args-left)
629 output-dir
630 (if (string-match "\\`--output-dir=" output-dir)
631 (prog1
632 (substring output-dir (match-end 0))
633 (setq command-line-args-left (cdr command-line-args-left)))))
634 (unless style
635 (error "There is no style '%s' defined." style-name))
636 (dolist (file command-line-args-left)
637 (muse-publish-file file style output-dir t))))
639 ;; Default publishing rules
641 (defun muse-publish-section-close (depth)
642 "Seach forward for the closing tag of given DEPTH."
643 (let (not-end)
644 (save-excursion
645 (while (and (setq not-end (re-search-forward
646 (concat "^\\*\\{1," (number-to-string depth)
647 "\\}\\s-+")
648 nil t))
649 (get-text-property (match-beginning 0) 'read-only)))
650 (if not-end
651 (forward-line 0)
652 (goto-char (point-max)))
653 (cond ((not (eq (char-before) ?\n))
654 (insert "\n\n"))
655 ((not (eq (char-before (1- (point))) ?\n))
656 (insert "\n")))
657 (insert (muse-markup-text 'section-close depth) "\n"))))
659 (defun muse-publish-markup-directive (&optional name value)
660 (unless name (setq name (match-string 1)))
661 (unless value (setq value (match-string 2)))
662 (let ((elem (assoc name muse-publishing-directives)))
663 (if elem
664 (setcdr elem value)
665 (setq muse-publishing-directives
666 (cons (cons name value)
667 muse-publishing-directives))))
668 ;; Make sure we don't ever try to move the point forward (past the
669 ;; beginning of buffer) while we're still searching for directives.
670 (setq muse-publishing-last-position nil)
671 (delete-region (match-beginning 0) (match-end 0)))
673 (defun muse-publish-markup-anchor () "")
675 (defun muse-publish-markup-comment ()
676 (if (null muse-publish-comments-p)
678 (goto-char (match-end 0))
679 (insert (muse-markup-text 'comment-end))
680 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
681 (goto-char (match-beginning 1))
682 (insert (muse-markup-text 'comment-begin))
683 (delete-region (match-beginning 0) (1- (match-beginning 1)))))
685 (defun muse-publish-markup-tag ()
686 (let ((tag-info (muse-markup-tag-info (match-string 1))))
687 (when (and tag-info
688 (not (get-text-property (match-beginning 0) 'read-only)))
689 (let ((closed-tag (match-string 3))
690 (start (match-beginning 0))
691 (beg (point)) end attrs)
692 (when (nth 2 tag-info)
693 (let ((attrstr (match-string 2)))
694 (while (and attrstr
695 (string-match (concat "\\([^"
696 muse-regexp-space
697 "=]+\\)\\(=\"\\"
698 "([^\"]+\\)\"\\)?")
699 attrstr))
700 (let ((attr (cons (downcase
701 (muse-match-string-no-properties 1 attrstr))
702 (muse-match-string-no-properties 3 attrstr))))
703 (setq attrstr (replace-match "" t t attrstr))
704 (if attrs
705 (nconc attrs (list attr))
706 (setq attrs (list attr)))))))
707 (if (and (cadr tag-info) (not closed-tag))
708 (if (search-forward (concat "</" (car tag-info) ">") nil t)
709 (delete-region (match-beginning 0) (point))
710 (setq tag-info nil)))
711 (when tag-info
712 (setq end (point-marker))
713 (delete-region start beg)
714 (goto-char start)
715 (let ((args (list start end)))
716 (if (nth 2 tag-info)
717 (nconc args (list attrs)))
718 (apply (nth 3 tag-info) args))))))
719 nil)
721 (defun muse-publish-escape-specials (beg end &optional ignore-read-only)
722 "Escape specials from BEG to END using style-specific :specials.
723 If IGNORE-READ-ONLY is non-nil, ignore the read-only property."
724 (save-excursion
725 (goto-char beg)
726 (while (< (point) end)
727 (if (and (not ignore-read-only) (get-text-property (point) 'read-only))
728 (forward-char 1)
729 (let ((repl
730 (or (assoc (char-after) (muse-style-element :specials))
731 (assoc (char-after) muse-publish-markup-specials))))
732 (if (null repl)
733 (forward-char 1)
734 (delete-char 1)
735 (insert-before-markers (cdr repl))))))))
737 (defun muse-publish-markup-word ()
738 (let* ((beg (match-beginning 2))
739 (end (1- (match-end 2)))
740 (leader (buffer-substring-no-properties beg end))
741 open-tag close-tag mark-read-only loc)
742 (cond
743 ((string= leader "_")
744 (setq open-tag (muse-markup-text 'begin-underline)
745 close-tag (muse-markup-text 'end-underline)))
746 ((string= leader "=")
747 (setq open-tag (muse-markup-text 'begin-literal)
748 close-tag (muse-markup-text 'end-literal))
749 (setq mark-read-only t))
751 (let ((l (length leader)))
752 (cond
753 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
754 close-tag (muse-markup-text 'end-emph)))
755 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
756 close-tag (muse-markup-text 'end-more-emph)))
757 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
758 close-tag (muse-markup-text 'end-most-emph)))))))
759 (if (and (not (get-text-property beg 'noemphasis))
760 (setq loc (search-forward leader nil t))
761 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
762 (not (eq (char-syntax (char-before (point))) ?\ ))
763 (not (get-text-property (point) 'noemphasis)))
764 (progn
765 (replace-match "")
766 (delete-region beg end)
767 (setq end (point-marker))
768 (insert close-tag)
769 (save-excursion
770 (goto-char beg)
771 (insert open-tag)
772 (setq beg (point)))
773 (when mark-read-only
774 (muse-publish-escape-specials beg end t)
775 (muse-publish-mark-read-only (1- beg) (1+ end))))
776 (backward-char))
777 nil))
779 (defun muse-publish-markup-emdash ()
780 (delete-region (match-beginning 0) (match-end 0))
781 (insert (muse-markup-text 'emdash))
782 (if (eq (char-after) ?\<)
783 (insert ?\n)))
785 (defun muse-publish-markup-enddots ()
786 (unless (get-text-property (match-beginning 0) 'noemphasis)
787 (delete-region (match-beginning 0) (match-end 0))
788 (insert (muse-markup-text 'enddots))))
790 (defun muse-publish-markup-dots ()
791 (unless (get-text-property (match-beginning 0) 'noemphasis)
792 (delete-region (match-beginning 0) (match-end 0))
793 (insert (muse-markup-text 'dots))))
795 (defun muse-publish-markup-rule ()
796 (unless (get-text-property (match-beginning 0) 'noemphasis)
797 (delete-region (match-beginning 0) (match-end 0))
798 (insert (muse-markup-text 'rule))))
800 (defun muse-publish-markup-heading ()
801 (let* ((len (length (match-string 1)))
802 (start (muse-markup-text
803 (cond ((= len 1) 'section)
804 ((= len 2) 'subsection)
805 ((= len 3) 'subsubsection)
806 (t 'section-other))
807 len))
808 (end (muse-markup-text
809 (cond ((= len 1) 'section-end)
810 ((= len 2) 'subsection-end)
811 ((= len 3) 'subsubsection-end)
812 (t 'section-other-end))
813 len)))
814 (delete-region (match-beginning 0) (match-end 0))
815 (insert start)
816 (end-of-line)
817 (if end (insert end))
818 (muse-publish-section-close len)))
820 (defvar muse-publish-footnotes nil)
822 (defun muse-publish-markup-footnote ()
823 "Scan ahead and snarf up the footnote body"
824 (if (= (muse-line-beginning-position) (match-beginning 0))
826 (let ((footnote (save-match-data
827 (string-to-number (match-string 1))))
828 footnotemark)
829 (delete-region (match-beginning 0) (match-end 0))
830 (save-excursion
831 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
832 (let* ((start (match-beginning 0))
833 (beg (goto-char (match-end 0)))
834 (end (save-excursion
835 (if (search-forward "\n\n" nil t)
836 (copy-marker (match-beginning 0))
837 (goto-char (point-max))
838 (skip-chars-backward "\n")
839 (point-marker)))))
840 (while (re-search-forward (concat "^["
841 muse-regexp-blank
842 "]+\\([^\n]\\)")
843 end t)
844 (replace-match "\\1" t))
845 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
846 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
847 (if (string= "" footnotemark-cmd)
848 (setq footnotemark
849 (concat (muse-markup-text 'footnote)
850 (buffer-substring-no-properties beg end)
851 (muse-markup-text 'footnote-end)))
852 (setq footnotemark (format footnotemark-cmd footnote
853 footnotemark-end-cmd))
854 (unless muse-publish-footnotes
855 (set (make-local-variable 'muse-publish-footnotes)
856 (make-vector 256 nil)))
857 (unless (aref muse-publish-footnotes footnote)
858 (setq footnotemark
859 (concat
860 footnotemark
861 (concat (format (muse-markup-text 'footnotetext)
862 footnote)
863 (buffer-substring-no-properties beg end)
864 (muse-markup-text 'footnotetext-end))))
865 (aset muse-publish-footnotes footnote footnotemark))))
866 (goto-char end)
867 (skip-chars-forward "\n")
868 (delete-region start (point)))))
869 (insert (or footnotemark footnote)))))
871 (defun muse-publish-markup-fn-sep ()
872 (delete-region (match-beginning 0) (match-end 0))
873 (insert (muse-markup-text 'fn-sep)))
875 (defun muse-publish-surround-text (beg-tag end-tag move-func)
876 (let ((beg (point)) end)
877 (skip-chars-backward muse-regexp-space)
878 (delete-region (point) beg)
879 (insert "\n\n" beg-tag)
880 (funcall move-func)
881 (setq end (point-marker))
882 (goto-char beg)
883 (while (< (point) end)
884 (if (looking-at "^\\s-+")
885 (replace-match ""))
886 (forward-line 1))
887 (goto-char end)
888 (setq beg (point))
889 (skip-chars-backward muse-regexp-space)
890 (delete-region (point) beg))
891 (insert end-tag "\n"))
893 (defsubst muse-forward-paragraph (&optional pattern)
894 (if (re-search-forward (if pattern
895 (concat "^\\(" pattern "["
896 muse-regexp-blank
897 "]+\\|$\\|\\'\\)")
898 "^\\s-*\\($\\|\\'\\)") nil t)
899 (goto-char (match-beginning 0))
900 (goto-char (point-max))))
902 (defun muse-publish-markup-list ()
903 "Markup a list entry or quoted paragraph.
904 The reason this function is so funky, is to prevent text properties
905 like read-only from being inadvertently deleted."
906 (let ((str (match-string 1)))
907 (cond
908 ((and (eq (aref str 0) ?-))
909 (delete-region (match-beginning 0) (match-end 0))
910 (muse-publish-surround-text
911 (muse-markup-text 'begin-uli)
912 (muse-markup-text 'end-uli)
913 (function
914 (lambda ()
915 (muse-forward-paragraph (concat "["
916 muse-regexp-blank
917 "]+-"))))))
918 ((and (>= (aref str 0) ?0)
919 (<= (aref str 0) ?9))
920 (delete-region (match-beginning 0) (match-end 0))
921 (muse-publish-surround-text
922 (muse-markup-text 'begin-oli)
923 (muse-markup-text 'end-oli)
924 (function
925 (lambda ()
926 (muse-forward-paragraph (concat "["
927 muse-regexp-blank
928 "]+[0-9]+\\."))))))
930 (goto-char (match-beginning 1))
931 (insert (muse-markup-text 'begin-ddt))
932 (save-match-data
933 (save-excursion
934 (forward-line 1)
935 (while (looking-at (concat "^\\(["
936 muse-regexp-blank
937 "]*\\)[^"
938 muse-regexp-space
939 "]"))
940 (delete-region (match-beginning 1) (match-end 1))
941 (forward-line 1))))
942 (save-match-data
943 (when (re-search-forward (concat "["
944 muse-regexp-space
945 "]+::["
946 muse-regexp-space
947 "]+")
948 nil t)
949 (replace-match (muse-markup-text 'start-dde))))
950 (muse-forward-paragraph)
951 (insert (muse-markup-text 'end-ddt) ?\n)))))
953 (defun muse-publish-markup-quote ()
954 "Markup a list entry or quoted paragraph.
955 The reason this function is so funky, is to prevent text properties
956 like read-only from being inadvertently deleted."
957 (let* ((ws (match-string 0))
958 (centered (>= (string-width ws) 6))
959 (begin-elem (if centered 'begin-center 'begin-quote))
960 (end-elem (if centered 'end-center 'end-quote)))
961 (muse-publish-surround-text (muse-markup-text begin-elem)
962 (muse-markup-text end-elem)
963 'muse-forward-paragraph)))
965 (defun muse-publish-markup-leading-space ()
966 (let ((markup-space (muse-markup-text 'verse-space))
967 count)
968 (when (and markup-space
969 (>= (setq count (skip-chars-forward " ")) 0))
970 (delete-region (muse-line-beginning-position) (point))
971 (while (> count 0)
972 (insert markup-space)
973 (setq count (- count 2))))))
975 (defun muse-publish-markup-verse ()
976 (let ((leader (match-string 0)))
977 (goto-char (match-beginning 0))
978 (insert (muse-markup-text 'begin-verse))
979 (while (looking-at leader)
980 (replace-match "")
981 (muse-publish-markup-leading-space)
982 (let ((beg (point)))
983 (end-of-line)
984 (cond
985 ((bolp)
986 (let ((text (muse-markup-text 'empty-verse-line)))
987 (if text (insert text))))
988 ((save-excursion
989 (save-match-data
990 (forward-line 1)
991 (or (looking-at (concat leader "["
992 muse-regexp-blank
993 "]*$"))
994 (not (looking-at leader)))))
995 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
996 (end-text (muse-markup-text 'end-last-stanza-line)))
997 (when end-text (insert end-text))
998 (goto-char beg)
999 (when begin-text (insert begin-text))
1000 (end-of-line)))
1002 (let ((begin-text (muse-markup-text 'begin-verse-line))
1003 (end-text (muse-markup-text 'end-verse-line)))
1004 (when end-text (insert end-text))
1005 (goto-char beg)
1006 (when begin-text (insert begin-text))
1007 (end-of-line))))
1008 (forward-line 1))))
1009 (insert (muse-markup-text 'end-verse) ?\n))
1011 (defun muse-publish-markup-table ()
1012 "Style does not support tables.")
1014 (defun muse-publish-escape-specials-in-string (string &rest ignored)
1015 "Escape specials in STRING using style-specific :specials."
1016 (save-excursion
1017 (apply (function concat)
1018 (mapcar
1019 (lambda (ch)
1020 (let ((repl
1021 (or (assoc ch (muse-style-element :specials))
1022 (assoc ch muse-publish-markup-specials))))
1023 (if (null repl)
1024 (char-to-string ch)
1025 (cdr repl))))
1026 (append string nil)))))
1028 (defun muse-publish-markup-email ()
1029 (let* ((beg (match-end 1))
1030 (addr (buffer-substring-no-properties beg (match-end 0))))
1031 (setq addr (muse-publish-escape-specials-in-string addr))
1032 (goto-char beg)
1033 (delete-region beg (match-end 0))
1034 (if (or (eq (char-before (match-beginning 0)) ?\")
1035 (eq (char-after (match-end 0)) ?\"))
1036 (insert addr)
1037 (insert (format (muse-markup-text 'email-addr) addr addr)))
1038 (muse-publish-mark-read-only beg (point))))
1040 (defun muse-publish-url (url &optional desc explicit)
1041 "Resolve a URL into its final <a href> form."
1042 (let ((orig-url url))
1043 (dolist (transform muse-publish-url-transforms)
1044 (setq url (save-match-data (when url (funcall transform url explicit)))))
1045 (dolist (transform muse-publish-desc-transforms)
1046 (setq desc (save-match-data
1047 (when desc (funcall transform desc explicit)))))
1048 (if url
1049 (cond ((string-match muse-image-regexp url)
1050 (if desc
1051 (muse-markup-text 'image-with-desc url desc)
1052 (muse-markup-text 'image-link url)))
1053 ((and desc (string-match muse-image-regexp desc))
1054 (muse-markup-text 'url-with-image url desc))
1055 ((eq (aref url 0) ?\#)
1056 (muse-markup-text 'internal-link (substring url 1)
1057 (or desc orig-url)))
1059 (muse-markup-text 'url-link url (or desc orig-url))))
1060 desc)))
1062 (defun muse-publish-insert-url (url &optional desc explicit)
1063 "Resolve a URL into its final <a href> form."
1064 (delete-region (match-beginning 0) (match-end 0))
1065 (let ((beg (point))
1066 (text (muse-publish-url url desc explicit)))
1067 (when text
1068 (insert text)
1069 (muse-publish-mark-read-only beg (point)))))
1071 (defun muse-publish-markup-link ()
1072 (let (desc explicit link)
1073 (setq explicit (save-match-data
1074 (if (string-match muse-explicit-link-regexp
1075 (match-string 0))
1076 t nil)))
1077 (setq desc (if explicit (match-string 2) (match-string 0)))
1078 (setq link (if explicit
1079 (muse-handle-explicit-link (match-string 1))
1080 (muse-handle-implicit-link (match-string 0))))
1081 (when (and link
1082 (or explicit
1083 (not (or (eq (char-before (match-beginning 0)) ?\")
1084 (eq (char-after (match-end 0)) ?\")))))
1085 (muse-publish-insert-url link desc explicit))))
1087 (defun muse-publish-markup-url ()
1088 (if (not (or (eq (char-before (match-beginning 0)) ?\")
1089 (eq (char-after (match-end 0)) ?\")))
1090 (muse-publish-insert-url (match-string 0))
1091 (let ((beg (match-beginning 0))
1092 (url (match-string 0)))
1093 (delete-region (match-beginning 0) (match-end 0))
1094 (insert (muse-publish-escape-specials-in-string url))
1095 (muse-publish-mark-read-only beg (point)))))
1097 ;; Default publishing tags
1099 (defun muse-publish-contents-tag (beg end attrs)
1100 (set (make-local-variable 'muse-publish-generate-contents)
1101 (cons (copy-marker (point) t)
1102 (let ((depth (cdr (assoc "depth" attrs))))
1103 (or (and depth (string-to-number depth)) 2)))))
1105 (defun muse-publish-verse-tag (beg end)
1106 (save-excursion
1107 (goto-char beg)
1108 (while (eq ?\ (char-syntax (char-after)))
1109 (delete-char 1))
1110 (while (< (point) end)
1111 (insert "> ")
1112 (forward-line))
1113 (if (eq ?\ (char-syntax (char-before)))
1114 (delete-char -1))))
1116 (defun muse-publish-mark-read-only (beg end)
1117 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1118 nil)
1120 (defun muse-publish-mark-noemphasis (&optional beg end)
1121 (unless beg (setq beg (match-beginning 0)))
1122 (unless end (setq end (match-end 0)))
1123 (add-text-properties beg end '(noemphasis t))
1124 nil)
1126 (defun muse-publish-code-tag (beg end)
1127 (muse-publish-escape-specials beg end)
1128 (goto-char beg)
1129 (insert (muse-markup-text 'begin-literal))
1130 (goto-char end)
1131 (insert (muse-markup-text 'end-literal))
1132 (muse-publish-mark-read-only beg (point)))
1134 (defun muse-publish-example-tag (beg end)
1135 (muse-publish-escape-specials beg end)
1136 (goto-char beg)
1137 (insert (muse-markup-text 'begin-example))
1138 (goto-char end)
1139 (insert (muse-markup-text 'end-example))
1140 (muse-publish-mark-read-only beg (point)))
1142 (defun muse-publish-verbatim-tag (beg end)
1143 (muse-publish-escape-specials beg end)
1144 (muse-publish-mark-read-only beg end))
1146 (defalias 'muse-publish-class-tag 'ignore)
1148 (defun muse-publish-lisp-tag (beg end)
1149 (save-excursion
1150 (let ((str (muse-eval-lisp
1151 (prog1
1152 (buffer-substring-no-properties beg end)
1153 (delete-region beg end)))))
1154 (set-text-properties 0 (length str) nil str)
1155 (insert str))))
1157 (defun muse-publish-command-tag (beg end attrs)
1158 (while (looking-at "\\s-*$")
1159 (forward-line))
1160 (let ((interp (cdr (assoc "interp" attrs))))
1161 (if (null interp)
1162 (shell-command
1163 (prog1
1164 (buffer-substring-no-properties (point) end)
1165 (delete-region beg end)) t)
1166 (shell-command-on-region beg end interp t t))
1167 (muse-publish-mark-read-only beg (point))))
1169 (defun muse-publish-comment-tag (beg end)
1170 (if (null muse-publish-comments-p)
1171 (delete-region beg end)
1172 (goto-char end)
1173 (insert (muse-markup-text 'comment-end))
1174 (goto-char beg)
1175 (insert (muse-markup-text 'comment-begin))))
1177 ;; Miscellaneous helper functions
1179 (defsubst muse-publishing-directive (name)
1180 (cdr (assoc name muse-publishing-directives)))
1182 (defun muse-publish-strip-tags (string)
1183 "Remove all tags from the string."
1184 (while (string-match "<.*?>" string)
1185 (setq string (replace-match "" nil t string)))
1186 string)
1188 (defun muse-publish-markup-type (category default-func)
1189 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1190 (funcall (or rule default-func))))
1192 (defun muse-published-buffer-contents (buffer)
1193 (with-current-buffer buffer
1194 (goto-char (point-min))
1195 (let ((beg (and (search-forward "Emacs Muse begins here")
1196 (muse-line-end-position)))
1197 (end (and (search-forward "Emacs Muse ends here")
1198 (muse-line-beginning-position))))
1199 (buffer-substring-no-properties beg end))))
1201 (defun muse-published-contents (file)
1202 (when (file-readable-p file)
1203 (muse-with-temp-buffer
1204 (insert-file-contents file)
1205 (muse-published-buffer-contents (current-buffer)))))
1207 (defun muse-publish-transform-output
1208 (file temp-file output-path name gen-func &rest cleanup-exts)
1209 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1210 (setq file (muse-page-name file))
1211 (message "Generating %s output for %s..." name file)
1212 (if (not (funcall gen-func temp-file output-path))
1213 (message "Generating %s from %s...failed" name file)
1214 (message "Generating %s output for %s...done" name file)
1215 (muse-delete-file-if-exists temp-file)
1216 (dolist (ext cleanup-exts)
1217 (muse-delete-file-if-exists
1218 (expand-file-name (concat file ext)
1219 (file-name-directory output-path))))
1220 (message "Wrote %s" output-path)))
1222 (defun muse-publish-read-only (string)
1223 (let ((end (1- (length string))))
1224 (add-text-properties 0 end
1225 '(rear-nonsticky (read-only) read-only t)
1226 string)
1227 string))
1229 (defun muse-publish-prepare-url (target &rest ignored)
1230 "Transform anchors and get published name, if TARGET is a page."
1231 (save-match-data
1232 (unless (or (string-match muse-url-regexp target)
1233 (string-match muse-image-regexp target)
1234 (string-match muse-file-regexp target))
1235 (setq target (if (string-match "#" target)
1236 (if (eq (aref target 0) ?\#)
1237 target
1238 (concat (muse-publish-link-name
1239 (substring target 0 (match-beginning 0)))
1240 "#" (substring target (match-end 0))))
1241 (muse-publish-link-name target)))))
1242 target)
1244 (provide 'muse-publish)
1246 ;;; muse-publish.el ends here