Add comments to nested list code.
[muse-el.git] / lisp / muse-publish.el
blobf46564ba1a2fbe209d48c25236cedba95f24335c
1 ;;; muse-publish.el --- base publishing implementation
3 ;; Copyright (C) 2004, 2005, 2006 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 ;; Jim Ottaway (j DOT ottaway AT lse DOT ac DOT uk) provided an
33 ;; implementation for nested lists.
35 ;;; Code:
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; Muse Publishing
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 (require 'muse)
44 (require 'muse-regexps)
46 (defgroup muse-publish nil
47 "Options controlling the general behavior of Muse publishing."
48 :group 'muse)
50 (defcustom muse-before-publish-hook nil
51 "A hook run in the buffer to be published, before it is done."
52 :type 'hook
53 :group 'muse-publish)
55 (defcustom muse-after-publish-hook nil
56 "A hook run in the buffer to be published, after it is done."
57 :type 'hook
58 :group 'muse-publish)
60 (defcustom muse-publish-url-transforms
61 '(muse-resolve-url)
62 "A list of functions used to prepare URLs for publication.
63 Each is passed the URL. The transformed URL should be returned."
64 :type 'hook
65 :options '(muse-resolve-url)
66 :group 'muse-publish)
68 (defcustom muse-publish-desc-transforms nil
69 "A list of functions used to prepare URL desciptions for publication.
70 Each is passed the description. The modified description should
71 be returned."
72 :type 'hook
73 :group 'muse-publish)
75 (defcustom muse-publish-comments-p nil
76 "If nil, remove comments before publishing.
77 If non-nil, publish comments using the markup of the current style."
78 :type 'boolean
79 :group 'muse-publish)
81 (defcustom muse-publish-report-threshhold 100000
82 "If a file is this size or larger, report publishing progress."
83 :type 'integer
84 :group 'muse-publish)
86 (defcustom muse-publish-markup-regexps
87 `(;; Remove leading and trailing whitespace from the file
88 (1000 "\\(\\`\n+\\|\n+\\'\\)" 0 "")
90 ;; Remove trailing whitespace from all lines
91 (1100 ,(concat "[" muse-regexp-blank "]+$") 0 "")
93 ;; Handle any leading #directives
94 (1200 "\\`#\\([a-zA-Z-]+\\)\\s-+\\(.+\\)\n+" 0 directive)
96 ;; markup tags
97 (1300 muse-tag-regexp 0 tag)
99 ;; commented lines
100 (1350 "^;\\s-+\\(.+\\)" 0 comment)
102 ;; prevent emphasis characters in explicit links from being marked
103 (1400 muse-explicit-link-regexp 0 muse-publish-mark-noemphasis)
105 ;; define anchor points
106 (1500 "^\\(\\W*\\)#\\(\\S-+\\)\\s-*" 0 anchor)
108 ;; emphasized or literal text
109 (1600 ,(concat "\\(^\\|[-[" muse-regexp-blank
110 "<('`\"\n]\\)\\(=[^=" muse-regexp-blank
111 "\n]\\|_[^_" muse-regexp-blank
112 "\n]\\|\\*+[^*" muse-regexp-blank
113 "\n]\\)")
114 2 word)
116 ;; headings, outline-mode style
117 (1700 "^\\(\\*+\\)\\s-+" 0 heading)
119 ;; ellipses
120 (1800 "\\.\\.\\.\\." 0 enddots)
121 (1850 "\\.\\.\\." 0 dots)
123 ;; horizontal rule, or section separator
124 (1900 "^----+" 0 rule)
126 ;; non-breaking space
127 (1950 "~~" 0 no-break-space)
129 ;; beginning of footnotes section
130 (2000 "^Footnotes:?\\s-*" 0 fn-sep)
131 ;; footnote definition/reference (def if at beginning of line)
132 (2100 "\\[\\([1-9][0-9]*\\)\\]" 0 footnote)
134 ;; unnumbered List items begin with a -. numbered list items
135 ;; begin with number and a period. definition lists have a
136 ;; leading term separated from the body with ::. centered
137 ;; paragraphs begin with at least six columns of whitespace; any
138 ;; other whitespace at the beginning indicates a blockquote. The
139 ;; reason all of these rules are handled here, is so that
140 ;; blockquote detection doesn't interfere with indented list
141 ;; members.
143 (2200 ,(format muse-list-item-regexp (concat "[" muse-regexp-blank "]*"))
144 0 list)
146 (2400 ,(concat "^\\([" muse-regexp-blank "]+\\).+") 0 quote)
148 (2500 ,(concat "\\(^\\|[" muse-regexp-blank "]+\\)--\\($\\|["
149 muse-regexp-blank "]+\\)")
150 0 emdash)
152 ;; "verse" text is indicated the same way as a quoted e-mail
153 ;; response: "> text", where text may contain initial whitespace
154 ;; (see below).
155 (2600 ,(concat "^[" muse-regexp-blank "]*> ") 0 verse)
157 ;; simple table markup is supported, nothing fancy. use | to
158 ;; separate cells, || to separate header cells, and ||| for footer
159 ;; cells
160 (2700 ,(concat "\\(\\([" muse-regexp-blank "]*\n\\)?"
161 "\\(" muse-table-line-regexp "\\(?:\n\\|\\'\\)\\)\\)+")
162 0 table)
164 ;; replace links in the buffer (links to other pages)
165 (2900 muse-explicit-link-regexp 0 link)
167 ;; bare URLs
168 (3000 muse-url-regexp 0 url)
170 ;; bare email addresses
171 (3500
172 "\\([^[]\\)[-a-zA-Z0-9._]+@\\([-a-zA-z0-9_]+\\.\\)+[a-zA-Z0-9]+" 0 email)
174 "List of markup rules for publishing a page with Muse.
175 The rules given in this variable are invoked first, followed by
176 whatever rules are specified by the current style.
178 Each member of the list is either a function, or a list of the form:
180 (REGEXP/SYMBOL TEXT-BEGIN-GROUP REPLACEMENT-TEXT/FUNCTION/SYMBOL)
182 REGEXP is a regular expression, or symbol whose value is a regular
183 expression, which is searched for using `re-search-forward'.
184 TEXT-BEGIN-GROUP is the matching group within that regexp which
185 denotes the beginning of the actual text to be marked up.
186 REPLACEMENT-TEXT is a string that will be passed to `replace-match'.
187 If it is not a string, but a function, it will be called to determine
188 what the replacement text should be (it must return a string). If it
189 is a symbol, the value of that symbol should be a string.
191 The replacements are done in order, one rule at a time. Writing
192 the regular expressions can be a tricky business. Note that case
193 is never ignored. `case-fold-search' is always bound to nil
194 while processing the markup rules."
195 :type '(repeat (choice
196 (list :tag "Markup rule"
197 integer
198 (choice regexp symbol)
199 integer
200 (choice string function symbol))
201 function))
202 :group 'muse-publish)
204 (defcustom muse-publish-markup-functions
205 '((directive . muse-publish-markup-directive)
206 (comment . muse-publish-markup-comment)
207 (anchor . muse-publish-markup-anchor)
208 (tag . muse-publish-markup-tag)
209 (word . muse-publish-markup-word)
210 (emdash . muse-publish-markup-emdash)
211 (enddots . muse-publish-markup-enddots)
212 (dots . muse-publish-markup-dots)
213 (rule . muse-publish-markup-rule)
214 (no-break-space . muse-publish-markup-no-break-space)
215 (heading . muse-publish-markup-heading)
216 (footnote . muse-publish-markup-footnote)
217 (fn-sep . muse-publish-markup-fn-sep)
218 (list . muse-publish-markup-list)
219 (quote . muse-publish-markup-quote)
220 (verse . muse-publish-markup-verse)
221 (table . muse-publish-markup-table)
222 (email . muse-publish-markup-email)
223 (link . muse-publish-markup-link)
224 (url . muse-publish-markup-url))
225 "An alist of style types to custom functions for that kind of text.
227 Each member of the list is of the form:
229 (SYMBOL FUNCTION)
231 SYMBOL describes the type of text to associate with this rule.
232 `muse-publish-markup-regexps' maps regexps to these symbols.
234 FUNCTION is the function to use to mark up this kind of rule if
235 no suitable function is found through the :functions tag of the
236 current style."
237 :type '(alist :key-type symbol :value-type function)
238 :group 'muse-publish)
240 (defcustom muse-publish-markup-tags
241 '(("contents" nil t muse-publish-contents-tag)
242 ("verse" t nil muse-publish-verse-tag)
243 ("example" t nil muse-publish-example-tag)
244 ("code" t nil muse-publish-code-tag)
245 ("quote" t nil muse-publish-quote-tag)
246 ("literal" t nil muse-publish-mark-read-only)
247 ("verbatim" t nil muse-publish-verbatim-tag)
248 ("lisp" t nil muse-publish-lisp-tag)
249 ("class" t t muse-publish-class-tag)
250 ("command" t t muse-publish-command-tag)
251 ("comment" t nil muse-publish-comment-tag))
252 "A list of tag specifications, for specially marking up text.
253 XML-style tags are the best way to add custom markup to Muse.
254 This is easily accomplished by customizing this list of markup tags.
256 For each entry, the name of the tag is given, whether it expects
257 a closing tag and/or an optional set of attributes, and a
258 function that performs whatever action is desired within the
259 delimited region.
261 The tags themselves are deleted during publishing, before the
262 function is called. The function is called with three arguments,
263 the beginning and end of the region surrounded by the tags. If
264 properties are allowed, they are passed as a third argument in
265 the form of an alist. The `end' argument to the function is
266 always a marker.
268 Point is always at the beginning of the region within the tags, when
269 the function is called. Wherever point is when the function finishes
270 is where tag markup will resume.
272 These tag rules are processed once at the beginning of markup, and
273 once at the end, to catch any tags which may have been inserted
274 in-between."
275 :type '(repeat (list (string :tag "Markup tag")
276 (boolean :tag "Expect closing tag" :value t)
277 (boolean :tag "Parse attributes" :value nil)
278 function))
279 :group 'muse-publish)
281 (defcustom muse-publish-markup-specials nil
282 "A table of characters which must be represented specially."
283 :type '(alist :key-type character :value-type string)
284 :group 'muse-publish)
286 (defvar muse-publishing-p nil
287 "Set to t while a page is being published.")
288 (defvar muse-batch-publishing-p nil
289 "Set to t while a page is being batch published.")
290 (defvar muse-publishing-styles nil
291 "The publishing styles that Muse recognizes.
292 This is automatically generated when loading publishing styles.")
293 (defvar muse-publishing-current-file nil
294 "The file that is currently being published.")
295 (defvar muse-publishing-current-output-path nil
296 "The path where the current file will be published to.")
297 (defvar muse-publishing-current-style nil
298 "The style of the file that is currently being published.")
299 (defvar muse-publishing-directives nil
300 "An alist of publishing directives from the top of a file.")
301 (defvar muse-publish-generate-contents nil
302 "Non-nil if a table of contents should be generated.
303 If non-nil, it is a cons cell specifying (MARKER . DEPTH), to
304 tell where the <contents> was seen, and to what depth the
305 contents were requested.")
306 (defvar muse-publishing-last-position nil
307 "Last position of the point when publishing.
308 This is used to make sure that publishing doesn't get stalled.")
310 ;; Functions for handling style information
312 (defsubst muse-style (&optional style)
313 "Resolve the given STYLE into a Muse style, if it is a string."
314 (if (null style)
315 muse-publishing-current-style
316 (if (stringp style)
317 (assoc style muse-publishing-styles)
318 (muse-assert (consp style))
319 style)))
321 (defun muse-define-style (name &rest elements)
322 (let ((entry (assoc name muse-publishing-styles)))
323 (if entry
324 (error "There is already a style named %s." name)
325 (setq muse-publishing-styles
326 (cons (append (list name) elements)
327 muse-publishing-styles)))))
329 (defun muse-derive-style (new-name base-name &rest elements)
330 (let ((entry (assoc new-name muse-publishing-styles)))
331 (if entry
332 (error "There is already a style named %s." new-name)
333 (apply 'muse-define-style new-name
334 (append elements (list :base base-name))))))
336 (defsubst muse-get-keyword (keyword list &optional direct)
337 (let ((value (cadr (memq keyword list))))
338 (if (and (not direct) (symbolp value))
339 (symbol-value value)
340 value)))
342 (defun muse-style-elements-list (elem &optional style)
343 "Return a list all references to ELEM in STYLE, including base styles.
344 If STYLE is not specified, use current style."
345 (let (base elements)
346 (while style
347 (setq style (muse-style style))
348 (setq elements (append elements
349 (muse-get-keyword elem style)))
350 (setq style (muse-get-keyword :base style)))
351 elements))
353 (defsubst muse-style-element (elem &optional style direct)
354 "Search for ELEM in STYLE, including base styles.
355 If STYLE is not specified, use current style."
356 (setq style (muse-style style))
357 (let ((value (muse-get-keyword elem style direct)))
358 (if value
359 value
360 (let ((base (muse-get-keyword :base style)))
361 (if base
362 (muse-style-element elem base direct))))))
364 (defun muse-find-markup-element (keyword ident style)
365 (let ((def (assq ident (muse-style-element keyword style))))
366 (if def
367 (cdr def)
368 (let ((base (muse-style-element :base style)))
369 (if base
370 (muse-find-markup-element keyword ident base))))))
372 (defun muse-markup-text (ident &rest args)
373 "Insert ARGS into the text markup associated with IDENT.
374 If the markup text has sections like %N%, this will be replaced
375 with the N-1th argument in ARGS. After that, `format' is applied
376 to the text with ARGS as parameters."
377 (let ((text (muse-find-markup-element :strings ident (muse-style))))
378 (if (and text args)
379 (progn
380 (let (start repl-text)
381 (while (setq start (string-match "%\\([1-9][0-9]*\\)%" text start))
382 ;; escape '%' in the argument text, since we will be
383 ;; using format on it
384 (setq repl-text (muse-replace-regexp-in-string
385 "%" "%%"
386 (nth (1- (string-to-number
387 (match-string 1 text))) args)
388 t t)
389 start (+ start (length repl-text))
390 text (replace-match repl-text t t text))))
391 (apply 'format text args))
392 (or text ""))))
394 (defun muse-insert-markup (&rest args)
395 (let ((beg (point)))
396 (apply 'insert args)
397 (muse-publish-mark-read-only beg (point))))
399 (defun muse-find-markup-tag (keyword tagname style)
400 (let ((def (assoc tagname (muse-style-element keyword style))))
401 (or def
402 (let ((base (muse-style-element :base style)))
403 (if base
404 (muse-find-markup-tag keyword tagname base))))))
406 (defsubst muse-markup-tag-info (tagname &rest args)
407 (let ((tag-info (muse-find-markup-tag :tags tagname (muse-style))))
408 (or tag-info
409 (assoc (match-string 1) muse-publish-markup-tags))))
411 (defsubst muse-markup-function (category)
412 (let ((func (muse-find-markup-element :functions category (muse-style))))
413 (or func
414 (cdr (assq category muse-publish-markup-functions)))))
416 ;; Publishing routines
418 (defun muse-publish-markup (name rules)
419 (let* ((case-fold-search nil)
420 (inhibit-read-only t)
421 (limit (* (length rules) (point-max)))
422 (verbose (and muse-publish-report-threshhold
423 (> (point-max) muse-publish-report-threshhold)))
424 (base 0))
425 (while rules
426 (goto-char (point-min))
427 (let ((regexp (nth 1 (car rules)))
428 (group (nth 2 (car rules)))
429 (repl (nth 3 (car rules)))
430 pos)
431 (setq muse-publishing-last-position nil)
432 (if (symbolp regexp)
433 (setq regexp (symbol-value regexp)))
434 (if (and verbose (not muse-batch-publishing-p))
435 (message "Publishing %s...%d%%" name
436 (* (/ (float (+ (point) base)) limit) 100)))
437 (while (and regexp (setq pos (re-search-forward regexp nil t)))
438 (if (and verbose (not muse-batch-publishing-p))
439 (message "Publishing %s...%d%%" name
440 (* (/ (float (+ (point) base)) limit) 100)))
441 (unless (and (> (- (match-end 0) (match-beginning 0)) 0)
442 (get-text-property (match-beginning group) 'read-only))
443 (let* (func
444 (text (cond
445 ((and (symbolp repl)
446 (setq func (muse-markup-function repl)))
447 (funcall func))
448 ((functionp repl)
449 (funcall repl))
450 ((symbolp repl)
451 (symbol-value repl))
452 (t repl))))
453 (if (stringp text)
454 (replace-match text t))))
455 (if (and muse-publishing-last-position
456 (= pos muse-publishing-last-position))
457 (if (eobp)
458 (setq regexp nil)
459 (forward-char 1)))
460 (setq muse-publishing-last-position pos)))
461 (setq rules (cdr rules)
462 base (+ base (point-max))))
463 (if (and verbose (not muse-batch-publishing-p))
464 (message "Publishing %s...done" name))))
466 (defun muse-insert-file-or-string (file-or-string &optional title)
467 (let ((beg (point)) end)
468 (if (and (not (string-equal file-or-string ""))
469 (file-readable-p file-or-string))
470 (setq end (+ beg (cadr (insert-file-contents file-or-string))))
471 (insert file-or-string)
472 (setq end (point)))
473 (save-restriction
474 (narrow-to-region beg end)
475 (muse-publish-markup (or title "")
476 '((100 "<\\(lisp\\)>" 0
477 muse-publish-markup-tag))))))
479 (defun muse-style-run-hooks (keyword style &rest args)
480 (let (handled)
481 (while (and style (not handled))
482 (setq style (muse-style style))
483 (let ((func (muse-get-keyword keyword style t)))
484 (if func
485 (if (apply func args)
486 (setq handled t))))
487 (unless handled
488 (setq style (muse-style-element :base style))))
489 handled))
491 (defun muse-publish-markup-region (beg end title style)
492 "Apply the given STYLE's markup rules to the given region."
493 (save-restriction
494 (narrow-to-region beg end)
495 (muse-style-run-hooks :before style)
496 (muse-publish-markup
497 title
498 (sort (copy-alist (append muse-publish-markup-regexps
499 (muse-style-elements-list :regexps style)))
500 (function
501 (lambda (l r)
502 (< (car l) (car r))))))
503 (muse-style-run-hooks :before-end style)
504 (muse-publish-escape-specials (point-min) (point-max) nil 'document)))
506 (defun muse-publish-markup-buffer (title style)
507 "Apply the given STYLE's markup rules to the current buffer."
508 (setq style (muse-style style))
509 (let ((style-header (muse-style-element :header style))
510 (style-footer (muse-style-element :footer style))
511 (muse-publishing-current-style style)
512 (muse-publishing-directives
513 (list (cons "title" title)
514 (cons "author" (user-full-name))
515 (cons "date" (format-time-string
516 "%B %e, %Y"
517 (nth 5 (file-attributes
518 muse-publishing-current-file))))))
519 (muse-publishing-p t)
520 (inhibit-read-only 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 (when style-header
525 (muse-insert-file-or-string style-header title))
526 (goto-char (point-max))
527 (when style-footer
528 (muse-insert-file-or-string style-footer title))
529 (muse-style-run-hooks :after style)
530 (run-hooks 'muse-after-publish-hook)))
532 (defun muse-publish-markup-string (string &optional style)
533 "Markup STRING using the given STYLE's markup rules."
534 (setq style (muse-style style))
535 (muse-with-temp-buffer
536 (insert string)
537 (let ((muse-publishing-current-style style)
538 (muse-publishing-p t))
539 (muse-publish-markup "*string*" (muse-style-element :rules style)))
540 (buffer-string)))
542 ;; Commands for publishing files
544 (defsubst muse-publish-get-style ()
545 (if (= 1 (length muse-publishing-styles))
546 (car muse-publishing-styles)
547 (assoc (completing-read "Publish with style: "
548 muse-publishing-styles nil t)
549 muse-publishing-styles)))
551 (defsubst muse-publish-get-output-dir (style)
552 (let ((default-directory (or (muse-style-element :path style)
553 default-directory)))
554 (muse-read-directory-name "Publish to directory: " nil default-directory)))
556 (defsubst muse-publish-get-info ()
557 (let ((style (muse-publish-get-style)))
558 (list style (muse-publish-get-output-dir style)
559 current-prefix-arg)))
561 (defsubst muse-publish-output-name (&optional file style)
562 (setq style (muse-style style))
563 (concat (muse-style-element :prefix style)
564 (muse-page-name file)
565 (muse-style-element :suffix style)))
567 (defsubst muse-publish-output-file (file &optional output-dir style)
568 (setq style (muse-style style))
569 (if output-dir
570 (expand-file-name (muse-publish-output-name file style) output-dir)
571 (concat (file-name-directory file)
572 (muse-publish-output-name file style))))
574 (defsubst muse-publish-link-name (&optional file style)
575 (setq style (muse-style style))
576 (concat (muse-style-element :prefix style)
577 (muse-page-name file)
578 (or (muse-style-element :link-suffix style)
579 (muse-style-element :suffix style))))
581 (defsubst muse-publish-link-file (file &optional output-dir style)
582 (setq style (muse-style style))
583 (if output-dir
584 (expand-file-name (muse-publish-link-name file style) output-dir)
585 (concat (file-name-directory file)
586 (muse-publish-link-name file style))))
588 ;;;###autoload
589 (defun muse-publish-file (file style &optional output-dir force)
590 "Publish the given FILE in a particular STYLE to OUTPUT-DIR.
591 If the argument FORCE is nil, each file is only published if it is
592 newer than the published version. If the argument FORCE is non-nil,
593 the file is published no matter what."
594 (interactive (cons (read-file-name "Publish file: ")
595 (muse-publish-get-info)))
596 (setq style (muse-style style))
597 (let* ((output-path (muse-publish-output-file file output-dir style))
598 (output-suffix (muse-style-element :osuffix style))
599 (muse-publishing-current-file file)
600 (muse-publishing-current-output-path output-path)
601 (target (if output-suffix
602 (concat (file-name-sans-extension output-path)
603 output-suffix)
604 output-path))
605 (threshhold (nth 7 (file-attributes file))))
606 (if (not threshhold)
607 (message "Please save %s before publishing" file)
608 (when (or force (file-newer-than-file-p file target))
609 (if (and muse-publish-report-threshhold
610 (> threshhold
611 muse-publish-report-threshhold))
612 (message "Publishing %s ..." file))
613 (muse-with-temp-buffer
614 (insert-file-contents file)
615 (muse-publish-markup-buffer (muse-page-name file) style)
616 (let ((backup-inhibited t))
617 (write-file output-path))
618 (muse-style-run-hooks :final style file output-path target))
619 t))))
621 ;;;###autoload
622 (defun muse-publish-this-file (style output-dir &optional force)
623 "Publish the page in the current file."
624 (interactive (muse-publish-get-info))
625 (unless (muse-publish-file buffer-file-name style output-dir force)
626 (message (concat "The published version is up-to-date; use"
627 " C-u C-c C-t to force an update."))))
629 (defun muse-batch-publish-files ()
630 "Publish Muse files in batch mode."
631 (let ((muse-batch-publishing-p t)
632 style-name style output-dir)
633 (setq style-name (car command-line-args-left)
634 style (muse-style style-name)
635 command-line-args-left (cdr command-line-args-left)
636 output-dir (car command-line-args-left)
637 output-dir
638 (if (string-match "\\`--output-dir=" output-dir)
639 (prog1
640 (substring output-dir (match-end 0))
641 (setq command-line-args-left (cdr command-line-args-left)))))
642 (unless style
643 (error "There is no style '%s' defined." style-name))
644 (dolist (file command-line-args-left)
645 (muse-publish-file file style output-dir t))))
647 ;; Default publishing rules
649 (defun muse-publish-section-close (depth)
650 "Seach forward for the closing tag of given DEPTH."
651 (let (not-end)
652 (save-excursion
653 (while (and (setq not-end (re-search-forward
654 (concat "^\\*\\{1," (number-to-string depth)
655 "\\}\\s-+")
656 nil t))
657 (get-text-property (match-beginning 0) 'read-only)))
658 (if not-end
659 (forward-line 0)
660 (goto-char (point-max)))
661 (cond ((not (eq (char-before) ?\n))
662 (insert "\n\n"))
663 ((not (eq (char-before (1- (point))) ?\n))
664 (insert "\n")))
665 (muse-insert-markup (muse-markup-text 'section-close depth))
666 (insert "\n"))))
668 (defun muse-publish-markup-directive (&optional name value)
669 (unless name (setq name (match-string 1)))
670 (unless value (setq value (match-string 2)))
671 (let ((elem (assoc name muse-publishing-directives)))
672 (if elem
673 (setcdr elem value)
674 (setq muse-publishing-directives
675 (cons (cons name value)
676 muse-publishing-directives))))
677 ;; Make sure we don't ever try to move the point forward (past the
678 ;; beginning of buffer) while we're still searching for directives.
679 (setq muse-publishing-last-position nil)
680 (delete-region (match-beginning 0) (match-end 0)))
682 (defun muse-publish-markup-anchor ()
683 (unless (get-text-property (match-end 1) 'noemphasis)
684 (let ((text (muse-markup-text 'anchor (match-string 2))))
685 (unless (string= text "")
686 (save-match-data
687 (skip-chars-forward (concat muse-regexp-blank "\n"))
688 (muse-insert-markup text)))
689 (match-string 1))))
691 (defun muse-publish-markup-comment ()
692 (if (null muse-publish-comments-p)
694 (goto-char (match-end 0))
695 (muse-insert-markup (muse-markup-text 'comment-end))
696 (muse-publish-mark-read-only (match-beginning 1) (match-end 1))
697 (delete-region (match-beginning 0) (match-beginning 1))
698 (goto-char (match-beginning 0))
699 (muse-insert-markup (muse-markup-text 'comment-begin))))
701 (defun muse-publish-markup-tag ()
702 (let ((tag-info (muse-markup-tag-info (match-string 1))))
703 (when (and tag-info
704 (not (get-text-property (match-beginning 0) 'read-only)))
705 (let ((closed-tag (match-string 3))
706 (start (match-beginning 0))
707 (beg (point))
708 end attrs)
709 (when (nth 2 tag-info)
710 (let ((attrstr (match-string 2)))
711 (while (and attrstr
712 (string-match (concat "\\([^"
713 muse-regexp-blank
714 "=\n]+\\)\\(=\"\\"
715 "([^\"]+\\)\"\\)?")
716 attrstr))
717 (let ((attr (cons (downcase
718 (muse-match-string-no-properties 1 attrstr))
719 (muse-match-string-no-properties 3 attrstr))))
720 (setq attrstr (replace-match "" t t attrstr))
721 (if attrs
722 (nconc attrs (list attr))
723 (setq attrs (list attr)))))))
724 (if (and (cadr tag-info) (not closed-tag))
725 (if (search-forward (concat "</" (car tag-info) ">") nil t)
726 (delete-region (match-beginning 0) (point))
727 (setq tag-info nil)))
728 (when tag-info
729 (setq end (point-marker))
730 (delete-region start beg)
731 (goto-char start)
732 (let ((args (list start end)))
733 (if (nth 2 tag-info)
734 (nconc args (list attrs)))
735 (apply (nth 3 tag-info) args))))))
736 nil)
738 (defun muse-publish-escape-specials (beg end &optional ignore-read-only context)
739 "Escape specials from BEG to END using style-specific :specials.
740 If IGNORE-READ-ONLY is non-nil, ignore the read-only property.
741 CONTEXT is used to figure out what kind of specials to escape.
743 The following contexts exist in Muse.
744 'underline _underlined text_
745 'literal =monospaced text= or <code>region</code> (monospaced, escaped)
746 'emphasis *emphasized text*
747 'email email@example.com
748 'url http://example.com
749 'url-desc [[...][description of an extended link]]
750 'example <example>region</example> (monospaced, block context, escaped)
751 'verbatim <verbatim>region</verbatim> (escaped)
752 'document normal text"
753 (let ((specials (muse-style-element :specials nil t)))
754 (cond ((functionp specials)
755 (setq specials (funcall specials context)))
756 ((symbolp specials)
757 (setq specials (symbol-value specials))))
758 (save-excursion
759 (save-restriction
760 (narrow-to-region beg end)
761 (goto-char (point-min))
762 (while (< (point) (point-max))
763 (if (and (not ignore-read-only)
764 (get-text-property (point) 'read-only))
765 (goto-char (or (next-single-property-change (point) 'read-only)
766 (point-max)))
767 (let ((repl (or (assoc (char-after) specials)
768 (assoc (char-after)
769 muse-publish-markup-specials))))
770 (if (null repl)
771 (forward-char 1)
772 (delete-char 1)
773 (insert-before-markers (cdr repl))))))))))
775 (defun muse-publish-markup-word ()
776 (let* ((beg (match-beginning 2))
777 (end (1- (match-end 2)))
778 (leader (buffer-substring-no-properties beg end))
779 open-tag close-tag mark-read-only loc context)
780 (cond
781 ((string= leader "_")
782 (setq context 'underline
783 open-tag (muse-markup-text 'begin-underline)
784 close-tag (muse-markup-text 'end-underline)))
785 ((string= leader "=")
786 (setq context 'literal
787 open-tag (muse-markup-text 'begin-literal)
788 close-tag (muse-markup-text 'end-literal))
789 (setq mark-read-only t))
791 (let ((l (length leader)))
792 (setq context 'emphasis)
793 (cond
794 ((= l 1) (setq open-tag (muse-markup-text 'begin-emph)
795 close-tag (muse-markup-text 'end-emph)))
796 ((= l 2) (setq open-tag (muse-markup-text 'begin-more-emph)
797 close-tag (muse-markup-text 'end-more-emph)))
798 ((= l 3) (setq open-tag (muse-markup-text 'begin-most-emph)
799 close-tag (muse-markup-text 'end-most-emph)))
800 (t (setq context nil))))))
801 (if (and context
802 (not (get-text-property beg 'noemphasis))
803 (setq loc (search-forward leader nil t))
804 (or (eobp) (not (eq (char-syntax (char-after loc)) ?w)))
805 (not (eq (char-syntax (char-before (point))) ?\ ))
806 (not (get-text-property (point) 'noemphasis)))
807 (progn
808 (replace-match "")
809 (delete-region beg end)
810 (setq end (point-marker))
811 (muse-insert-markup close-tag)
812 (save-excursion
813 (goto-char beg)
814 (muse-insert-markup open-tag)
815 (setq beg (point)))
816 (when mark-read-only
817 (muse-publish-escape-specials beg end t context)
818 (muse-publish-mark-read-only beg end)))
819 (backward-char))
820 nil))
822 (defun muse-publish-markup-emdash ()
823 (delete-region (match-beginning 0) (match-end 0))
824 (muse-insert-markup (muse-markup-text 'emdash))
825 (if (eq (char-after) ?\<)
826 (insert ?\n)))
828 (defun muse-publish-markup-enddots ()
829 (unless (get-text-property (match-beginning 0) 'noemphasis)
830 (delete-region (match-beginning 0) (match-end 0))
831 (muse-insert-markup (muse-markup-text 'enddots))))
833 (defun muse-publish-markup-dots ()
834 (unless (get-text-property (match-beginning 0) 'noemphasis)
835 (delete-region (match-beginning 0) (match-end 0))
836 (muse-insert-markup (muse-markup-text 'dots))))
838 (defun muse-publish-markup-rule ()
839 (unless (get-text-property (match-beginning 0) 'noemphasis)
840 (delete-region (match-beginning 0) (match-end 0))
841 (muse-insert-markup (muse-markup-text 'rule))))
843 (defun muse-publish-markup-no-break-space ()
844 (unless (get-text-property (match-beginning 0) 'noemphasis)
845 (delete-region (match-beginning 0) (match-end 0))
846 (muse-insert-markup (muse-markup-text 'no-break-space))))
848 (defun muse-publish-markup-heading ()
849 (let* ((len (length (match-string 1)))
850 (start (muse-markup-text
851 (cond ((= len 1) 'section)
852 ((= len 2) 'subsection)
853 ((= len 3) 'subsubsection)
854 (t 'section-other))
855 len))
856 (end (muse-markup-text
857 (cond ((= len 1) 'section-end)
858 ((= len 2) 'subsection-end)
859 ((= len 3) 'subsubsection-end)
860 (t 'section-other-end))
861 len)))
862 (delete-region (match-beginning 0) (match-end 0))
863 (muse-insert-markup start)
864 (end-of-line)
865 (when end
866 (muse-insert-markup end))
867 (muse-publish-section-close len)))
869 (defvar muse-publish-footnotes nil)
871 (defun muse-publish-markup-footnote ()
872 "Scan ahead and snarf up the footnote body"
873 (cond
874 ((get-text-property (match-beginning 0) 'noemphasis)
875 nil)
876 ((= (muse-line-beginning-position) (match-beginning 0))
879 (let ((footnote (save-match-data
880 (string-to-number (match-string 1))))
881 footnotemark)
882 (delete-region (match-beginning 0) (match-end 0))
883 (save-excursion
884 (when (re-search-forward (format "^\\[%d\\]\\s-+" footnote) nil t)
885 (let* ((start (match-beginning 0))
886 (beg (goto-char (match-end 0)))
887 (end (save-excursion
888 (if (search-forward "\n\n" nil t)
889 (copy-marker (match-beginning 0))
890 (goto-char (point-max))
891 (skip-chars-backward "\n")
892 (point-marker)))))
893 (while (re-search-forward
894 (concat "^[" muse-regexp-blank "]+\\([^\n]\\)")
895 end t)
896 (replace-match "\\1" t))
897 (let ((footnotemark-cmd (muse-markup-text 'footnotemark))
898 (footnotemark-end-cmd (muse-markup-text 'footnotemark-end)))
899 (if (string= "" footnotemark-cmd)
900 (setq footnotemark
901 (concat (muse-markup-text 'footnote)
902 (buffer-substring-no-properties beg end)
903 (muse-markup-text 'footnote-end)))
904 (setq footnotemark (format footnotemark-cmd footnote
905 footnotemark-end-cmd))
906 (unless muse-publish-footnotes
907 (set (make-local-variable 'muse-publish-footnotes)
908 (make-vector 256 nil)))
909 (unless (aref muse-publish-footnotes footnote)
910 (setq footnotemark
911 (concat
912 footnotemark
913 (concat (format (muse-markup-text 'footnotetext)
914 footnote)
915 (buffer-substring-no-properties beg end)
916 (muse-markup-text 'footnotetext-end))))
917 (aset muse-publish-footnotes footnote footnotemark))))
918 (goto-char end)
919 (skip-chars-forward "\n")
920 (delete-region start (point)))))
921 (muse-insert-markup (or footnotemark footnote))))))
923 (defun muse-publish-markup-fn-sep ()
924 (delete-region (match-beginning 0) (match-end 0))
925 (muse-insert-markup (muse-markup-text 'fn-sep)))
927 (defun muse-insert-markup-end-list (&rest args)
928 (let ((beg (point)))
929 (apply 'insert args)
930 (add-text-properties beg (point) '(end-list t))
931 (muse-publish-mark-read-only beg (point))))
933 (defun muse-publish-surround-dl (indent post-indent)
934 (let* ((beg-item (muse-markup-text 'begin-dl-item))
935 (end-item (muse-markup-text 'end-dl-item))
936 (beg-ddt (muse-markup-text 'begin-ddt)) ;; term
937 (end-ddt (muse-markup-text 'end-ddt))
938 (beg-dde (muse-markup-text 'begin-dde)) ;; definition
939 (end-dde (muse-markup-text 'end-dde))
940 (move-term `(lambda ()
941 (let (status)
942 (while (and
943 (setq status
944 (muse-forward-dl-item ,indent))
945 (string= (match-string 3) ""))
946 (goto-char (match-end 0)))
947 status)))
948 (move-entry `(lambda ()
949 (muse-forward-dl-item ,indent t)))
950 (continue t)
951 beg)
952 (while continue
953 ;; envelope this as one term+definitions unit -- HTML does not
954 ;; need this, but DocBook and Muse's custom XML format do
955 (muse-insert-markup beg-item)
956 (when (looking-at muse-dl-term-regexp)
957 ;; find the term and wrap it with published markup
958 (setq beg (point))
959 (goto-char (match-end 1))
960 (delete-region (point) (match-end 0))
961 (muse-insert-markup end-ddt)
962 ;; if definition is immediately after term, move to next line
963 (unless (eq (char-after) ?\n)
964 (insert ?\n))
965 (save-excursion
966 (goto-char beg)
967 (delete-region (point) (match-beginning 1))
968 (muse-insert-markup beg-ddt)))
969 (setq beg (point)
970 ;; move past current item
971 continue (funcall move-term))
972 (save-restriction
973 (narrow-to-region beg (point))
974 (goto-char (point-min))
975 ;; publish each definition that we find, defaulting to an
976 ;; empty definition if none are found
977 (muse-publish-surround-text beg-dde end-dde move-entry
978 indent post-indent)
979 (goto-char (point-max))
980 (skip-chars-backward (concat muse-regexp-blank "\n"))
981 (muse-insert-markup end-item)
982 (when continue
983 (goto-char (point-max)))))))
985 (defun muse-publish-surround-text (beg-tag end-tag move-func &optional indent post-indent)
986 (unless indent
987 (setq indent (concat "[" muse-regexp-blank "]+")))
988 (when post-indent
989 (setq indent (concat indent " \\{0," (number-to-string post-indent)
990 "\\}")))
991 (let ((continue t)
992 (list-item (format muse-list-item-regexp
993 (concat "[" muse-regexp-blank "]*")))
994 (list-nested-p nil)
995 beg)
996 (while continue
997 (muse-insert-markup beg-tag)
998 (setq beg (point)
999 ;; move past current item; continue is non-nil if there
1000 ;; are more like items to be processed
1001 continue (funcall move-func))
1002 (save-restriction
1003 (narrow-to-region beg (point))
1004 ;; narrow to current item
1005 (goto-char (point-min))
1006 (while (< (point) (point-max))
1007 (when (and (not list-nested-p)
1008 ;; if we encounter even one nested list item, we
1009 ;; have to stop removing indentation
1010 (not (and (looking-at list-item)
1011 (setq list-nested-p t)))
1012 (looking-at indent))
1013 ;; if list is not nested, remove indentation
1014 (replace-match ""))
1015 (forward-line 1))
1016 (skip-chars-backward (concat muse-regexp-blank "\n"))
1017 (muse-insert-markup-end-list end-tag)
1018 (when continue
1019 (goto-char (point-max)))))))
1021 (defun muse-list-item-type (str)
1022 "Determine the type of list given STR.
1023 Returns either 'ul, 'ol, or 'dl."
1024 (cond ((or (string= str "")
1025 (< (length str) 2))
1026 nil)
1027 ((and (= (aref str 0) ?\ )
1028 (= (aref str 1) ?-))
1029 'ul)
1030 ((save-match-data
1031 (string-match (concat "\\`[" muse-regexp-blank "][0-9]+\\.") str))
1032 'ol)
1033 (t 'dl)))
1035 (defsubst muse-forward-paragraph (&optional pattern)
1036 (forward-line 1)
1037 (if (re-search-forward (if pattern
1038 (concat "^\\(?:" pattern "\\|\n\\|\\'\\)")
1039 "^\\s-*\\(\n\\|\\'\\)") nil t)
1040 (goto-char (match-beginning 0))
1041 (goto-char (point-max))))
1043 (defun muse-forward-dl-item (indent &optional entry-p)
1044 "Move forward to the next definition list item.
1045 Return non-nil if successful, nil otherwise.
1046 The beginning indentation is given by INDENT.
1048 If ENTRY-P is non-nil, seach ahead by definition list entries.
1049 Otherwise, search ahead by definition list terms."
1050 (let* ((list-item (if entry-p
1051 muse-dl-entry-regexp
1052 (format muse-list-item-regexp indent)))
1053 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1054 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
1055 (next-list-end (or (next-single-property-change (point) 'end-list)
1056 (point-max)))
1057 (list-pattern (concat "\\(?:" empty-line "\\)?"
1058 "\\(" list-item "\\)")))
1059 (while (progn
1060 (muse-forward-paragraph list-pattern)
1061 (when (and (not (match-beginning 1))
1062 (< (point) next-list-end))
1063 ;; blank line encountered with no list item on the same
1064 ;; level after it
1065 (let ((beg (point)))
1066 (forward-line 1)
1067 (if (and (looking-at indented-line)
1068 (not (looking-at empty-line)))
1069 ;; found that this blank line is followed by some
1070 ;; indentation, plus other text, so we'll keep
1071 ;; going
1073 (goto-char beg)
1074 nil)))))
1075 (cond ((>= (point) next-list-end)
1076 ;; past the list boundary, so go back
1077 (goto-char next-list-end)
1078 nil)
1079 (entry-p
1080 (if (match-beginning 1)
1081 (progn
1082 ;; valid list entry, so remove the markup
1083 (goto-char (match-beginning 1))
1084 (replace-match "" t t nil 1)
1086 nil))
1087 ((match-string 2)
1088 ;; move just before next occurrence of list item
1089 (goto-char (match-beginning 1))
1090 (if (eq 'dl (muse-list-item-type (match-string 2)))
1091 ;; if same type, indicate that there are more items to
1092 ;; be parsed
1094 nil))
1096 ;; list item found, but was not a definition list
1097 (when (match-beginning 1)
1098 (goto-char (match-beginning 1)))
1099 nil))))
1101 (defun muse-forward-list-item (type indent)
1102 "Move forward to the next item of TYPE.
1103 Return non-nil if successful, nil otherwise.
1104 The beginning indentation is given by INDENT."
1105 (let* ((list-item (format muse-list-item-regexp indent))
1106 (empty-line (concat "^[" muse-regexp-blank "]*\n"))
1107 (indented-line (concat "^" indent "[" muse-regexp-blank "]"))
1108 (next-list-end (or (next-single-property-change (point) 'end-list)
1109 (point-max)))
1110 (list-pattern (concat "\\(?:" empty-line "\\)?"
1111 "\\(" list-item "\\)")))
1112 (while (progn
1113 (muse-forward-paragraph list-pattern)
1114 (when (and (not (match-beginning 1))
1115 (< (point) next-list-end))
1116 ;; blank line encountered with no list item on the same
1117 ;; level after it
1118 (let ((beg (point)))
1119 (forward-line 1)
1120 (if (and (looking-at indented-line)
1121 (not (looking-at empty-line)))
1122 ;; found that this blank line is followed by some
1123 ;; indentation, plus other text, so we'll keep
1124 ;; going
1126 (goto-char beg)
1127 nil)))))
1128 (cond ((>= (point) next-list-end)
1129 ;; past the list boundary, so go back
1130 (goto-char next-list-end)
1131 nil)
1132 ((and (match-string 2)
1133 (eq type (muse-list-item-type (match-string 2))))
1134 ;; same type, so remove the markup and indicate that there
1135 ;; are more items to be parsed
1136 (replace-match "" t t nil 1)
1137 (goto-char (match-beginning 1)))
1139 (when (match-beginning 1)
1140 (goto-char (match-beginning 1)))
1141 ;; move to just before foreign list item markup
1142 nil))))
1144 (defun muse-publish-markup-list ()
1145 "Markup a list entry.
1146 The reason this function is so funky, is to prevent text properties
1147 like read-only from being inadvertently deleted."
1148 (let* ((str (match-string 1))
1149 (type (muse-list-item-type str))
1150 (indent (buffer-substring (muse-line-beginning-position)
1151 (match-beginning 1)))
1152 (post-indent (length str))
1153 (last (match-beginning 0)))
1154 (cond
1155 ((eq type 'ul)
1156 (unless (eq (char-after (match-end 1)) ?-)
1157 (delete-region (match-beginning 0) (match-end 0))
1158 (muse-insert-markup (muse-markup-text 'begin-uli))
1159 (save-excursion
1160 (muse-publish-surround-text
1161 (muse-markup-text 'begin-uli-item)
1162 (muse-markup-text 'end-uli-item)
1163 `(lambda ()
1164 (muse-forward-list-item 'ul ,indent))
1165 indent post-indent)
1166 (muse-insert-markup-end-list (muse-markup-text 'end-uli)))
1167 (forward-line 1)))
1168 ((eq type 'ol)
1169 (delete-region (match-beginning 0) (match-end 0))
1170 (muse-insert-markup (muse-markup-text 'begin-oli))
1171 (save-excursion
1172 (muse-publish-surround-text
1173 (muse-markup-text 'begin-oli-item)
1174 (muse-markup-text 'end-oli-item)
1175 `(lambda ()
1176 (muse-forward-list-item 'ol ,indent))
1177 indent post-indent)
1178 (muse-insert-markup-end-list (muse-markup-text 'end-oli)))
1179 (forward-line 1))
1181 (goto-char (match-beginning 0))
1182 (muse-insert-markup (muse-markup-text 'begin-dl))
1183 (save-excursion
1184 (muse-publish-surround-dl indent post-indent)
1185 (muse-insert-markup-end-list (muse-markup-text 'end-dl)))
1186 (forward-line 1))))
1187 nil)
1189 (defun muse-publish-markup-quote ()
1190 "Markup a quoted paragraph.
1191 The reason this function is so funky, is to prevent text properties
1192 like read-only from being inadvertently deleted."
1193 (let* ((ws (match-string 1))
1194 (centered (>= (string-width ws) 6))
1195 (begin-elem (if centered 'begin-center 'begin-quote-item))
1196 (end-elem (if centered 'end-center 'end-quote-item)))
1197 (replace-match "" t t nil 1)
1198 (unless centered
1199 (muse-insert-markup (muse-markup-text 'begin-quote)))
1200 (muse-publish-surround-text (muse-markup-text begin-elem)
1201 (muse-markup-text end-elem)
1202 (function (lambda ()
1203 (muse-forward-paragraph)
1204 nil)))
1205 (unless centered
1206 (muse-insert-markup (muse-markup-text 'end-quote)))))
1208 (defun muse-publish-markup-leading-space (markup-space multiple)
1209 (let (count)
1210 (when (and markup-space
1211 (>= (setq count (skip-chars-forward " ")) 0))
1212 (delete-region (muse-line-beginning-position) (point))
1213 (while (> count 0)
1214 (muse-insert-markup markup-space)
1215 (setq count (- count multiple))))))
1217 (defun muse-publish-markup-verse ()
1218 (let ((leader (match-string 0)))
1219 (goto-char (match-beginning 0))
1220 (muse-insert-markup (muse-markup-text 'begin-verse))
1221 (while (looking-at leader)
1222 (replace-match "")
1223 (muse-publish-markup-leading-space (muse-markup-text 'verse-space) 2)
1224 (let ((beg (point)))
1225 (end-of-line)
1226 (cond
1227 ((bolp)
1228 (let ((text (muse-markup-text 'empty-verse-line)))
1229 (when text (muse-insert-markup text))))
1230 ((save-excursion
1231 (save-match-data
1232 (forward-line 1)
1233 (or (looking-at (concat leader "["
1234 muse-regexp-blank
1235 "]*$"))
1236 (not (looking-at leader)))))
1237 (let ((begin-text (muse-markup-text 'begin-last-stanza-line))
1238 (end-text (muse-markup-text 'end-last-stanza-line)))
1239 (when end-text (muse-insert-markup end-text))
1240 (goto-char beg)
1241 (when begin-text (muse-insert-markup begin-text))
1242 (end-of-line)))
1244 (let ((begin-text (muse-markup-text 'begin-verse-line))
1245 (end-text (muse-markup-text 'end-verse-line)))
1246 (when end-text (muse-insert-markup end-text))
1247 (goto-char beg)
1248 (when begin-text (muse-insert-markup begin-text))
1249 (end-of-line))))
1250 (forward-line 1))))
1251 (muse-insert-markup (muse-markup-text 'end-verse) ?\n))
1253 (defun muse-publish-table-fields (beg end)
1254 "Parse given region as a table, returning a cons cell.
1255 The car is the length of the longest row.
1257 The cdr is a list of the fields of the table, with the first
1258 element indicating the type of the row:
1259 1: body, 2: header, 3: footer.
1261 The existing region will be removed, except for initial blank lines."
1262 (let ((longest 0)
1263 (left 0)
1264 fields field-list)
1265 (save-restriction
1266 (narrow-to-region beg end)
1267 (goto-char (point-min))
1268 (while (looking-at (concat "^[" muse-regexp-blank "]*$"))
1269 (forward-line 1))
1270 (setq beg (point))
1271 (while (= left 0)
1272 (when (looking-at muse-table-line-regexp)
1273 (setq fields (cons (length (match-string 1))
1274 (split-string (match-string 0)
1275 muse-table-field-regexp))
1276 field-list (cons fields field-list)
1277 longest (max (length fields) longest)))
1278 (setq left (forward-line 1))))
1279 (delete-region beg end)
1280 (if (= longest 0)
1281 (cons 0 nil)
1282 (cons (1- longest) (nreverse field-list)))))
1284 (defun muse-publish-markup-table ()
1285 "Style does not support tables.")
1287 (defun muse-publish-escape-specials-in-string (string &optional context)
1288 "Escape specials in STRING using style-specific :specials.
1289 CONTEXT is used to figure out what kind of specials to escape.
1291 See the documentation of the `muse-publish-escape-specials'
1292 function for the list of available contexts."
1293 (unless string
1294 (setq string ""))
1295 (let ((specials (muse-style-element :specials nil t)))
1296 (cond ((functionp specials)
1297 (setq specials (funcall specials context)))
1298 ((symbolp specials)
1299 (setq specials (symbol-value specials))))
1300 (if (functionp specials)
1301 (funcall specials string)
1302 (apply (function concat)
1303 (mapcar
1304 (lambda (ch)
1305 (let (repl)
1306 (setq repl (or (assoc ch specials)
1307 (assoc ch muse-publish-markup-specials)))
1308 (if (null repl)
1309 (char-to-string ch)
1310 (cdr repl))))
1311 (append string nil))))))
1313 (defun muse-publish-markup-email ()
1314 (let* ((beg (match-end 1))
1315 (addr (buffer-substring-no-properties beg (match-end 0))))
1316 (setq addr (muse-publish-escape-specials-in-string addr 'email))
1317 (goto-char beg)
1318 (delete-region beg (match-end 0))
1319 (if (or (eq (char-before (match-beginning 0)) ?\")
1320 (eq (char-after (match-end 0)) ?\"))
1321 (insert addr)
1322 (insert (format (muse-markup-text 'email-addr) addr addr)))
1323 (muse-publish-mark-read-only beg (point))))
1325 (defun muse-publish-classify-url (target)
1326 "Transform anchors and get published name, if TARGET is a page.
1327 The return value is a cons cell. The car is the type of link,
1328 the cadr is the page name, and the cddr is the anchor."
1329 (save-match-data
1330 (cond ((or (null target) (string= target ""))
1331 nil)
1332 ((string-match muse-url-regexp target)
1333 (cons 'url (cons target nil)))
1334 ((string-match muse-image-regexp target)
1335 (cons 'image (cons target nil)))
1336 ((string-match muse-file-regexp target)
1337 (cons 'file (cons target nil)))
1338 ((string-match "#" target)
1339 (if (eq (aref target 0) ?\#)
1340 (cons 'anchor-ref (cons nil (substring target 1)))
1341 (cons 'link-and-anchor
1342 (cons (muse-publish-link-name
1343 (substring target 0 (match-beginning 0)))
1344 (substring target (match-end 0))))))
1346 (cons 'link (cons (muse-publish-link-name target) nil))))))
1348 (defun muse-publish-url (url &optional desc explicit)
1349 "Resolve a URL into its final <a href> form."
1350 (let ((orig-url url)
1351 type anchor)
1352 (dolist (transform muse-publish-url-transforms)
1353 (setq url (save-match-data (when url (funcall transform url explicit)))))
1354 (dolist (transform muse-publish-desc-transforms)
1355 (setq desc (save-match-data
1356 (when desc (funcall transform desc explicit)))))
1357 (when desc
1358 (setq desc (muse-publish-escape-specials-in-string desc 'url-desc)))
1359 (setq orig-url
1360 (muse-publish-escape-specials-in-string orig-url 'url-desc))
1361 (let ((target (muse-publish-classify-url url)))
1362 (setq type (car target)
1363 url (muse-publish-escape-specials-in-string (cadr target) 'url)
1364 anchor (muse-publish-escape-specials-in-string
1365 (cddr target) 'url)))
1366 (cond ((eq type 'anchor-ref)
1367 (muse-markup-text 'anchor-ref anchor (or desc orig-url)))
1368 ((string= url "")
1369 desc)
1370 ((eq type 'image)
1371 (if desc
1372 (muse-markup-text 'image-with-desc url desc)
1373 (muse-markup-text 'image-link url)))
1374 ((eq type 'link-and-anchor)
1375 (muse-markup-text 'link-and-anchor url anchor
1376 (or desc orig-url)))
1377 ((and desc (string-match muse-image-regexp desc))
1378 (muse-markup-text 'url-with-image url desc))
1379 ((eq type 'link)
1380 (muse-markup-text 'link url (or desc orig-url)))
1382 (muse-markup-text 'url url (or desc orig-url))))))
1384 (defun muse-publish-insert-url (url &optional desc explicit)
1385 "Resolve a URL into its final <a href> form."
1386 (delete-region (match-beginning 0) (match-end 0))
1387 (let ((text (muse-publish-url url desc explicit)))
1388 (when text
1389 (muse-insert-markup text))))
1391 (defun muse-publish-markup-link ()
1392 (let (desc explicit link)
1393 (setq explicit (save-match-data
1394 (if (string-match muse-explicit-link-regexp
1395 (match-string 0))
1396 t nil)))
1397 (setq desc (if explicit (match-string 2) (match-string 0)))
1398 (setq link (if explicit
1399 (muse-handle-explicit-link (match-string 1))
1400 (muse-handle-implicit-link (match-string 0))))
1401 (when (and link
1402 (or explicit
1403 (not (or (eq (char-before (match-beginning 0)) ?\")
1404 (eq (char-after (match-end 0)) ?\")))))
1405 (muse-publish-insert-url link desc explicit))))
1407 (defun muse-publish-markup-url ()
1408 (unless (or (eq (char-before (match-beginning 0)) ?\")
1409 (eq (char-after (match-end 0)) ?\"))
1410 (muse-publish-insert-url (match-string 0))))
1412 ;; Default publishing tags
1414 (defun muse-publish-contents-tag (beg end attrs)
1415 (set (make-local-variable 'muse-publish-generate-contents)
1416 (cons (copy-marker (point) t)
1417 (let ((depth (cdr (assoc "depth" attrs))))
1418 (or (and depth (string-to-number depth)) 2)))))
1420 (defun muse-publish-verse-tag (beg end)
1421 (save-excursion
1422 (save-restriction
1423 (narrow-to-region beg end)
1424 (goto-char (point-min))
1425 (while (eq ?\ (char-syntax (char-after)))
1426 (delete-char 1))
1427 (while (< (point) (point-max))
1428 (insert "> ")
1429 (forward-line))
1430 (if (eq ?\ (char-syntax (char-before)))
1431 (delete-char -1)))))
1433 (defun muse-publish-mark-read-only (beg end)
1434 "Add read-only properties to the given region."
1435 (add-text-properties beg end '(rear-nonsticky (read-only) read-only t))
1436 nil)
1438 (defun muse-publish-mark-noemphasis (&optional beg end)
1439 "Make sure that no emphasis characters are interpreted within
1440 the given region. If a region is not specified, use the 0th
1441 match data to determine it.
1443 This is usually applied to extended links."
1444 (unless beg (setq beg (match-beginning 0)))
1445 (unless end (setq end (match-end 0)))
1446 (add-text-properties beg end '(noemphasis t))
1447 nil)
1449 (defun muse-publish-quote-tag (beg end)
1450 (save-excursion
1451 (save-restriction
1452 (narrow-to-region beg end)
1453 (muse-insert-markup (muse-markup-text 'begin-quote))
1454 (muse-publish-surround-text (muse-markup-text 'begin-quote-item)
1455 (muse-markup-text 'end-quote-item)
1456 (function (lambda ()
1457 (muse-forward-paragraph)
1458 (goto-char (match-end 0))
1459 (< (point) (point-max)))))
1460 (muse-insert-markup (muse-markup-text 'end-quote)))))
1462 (defun muse-publish-code-tag (beg end)
1463 (muse-publish-escape-specials beg end nil 'literal)
1464 (goto-char beg)
1465 (insert (muse-markup-text 'begin-literal))
1466 (goto-char end)
1467 (insert (muse-markup-text 'end-literal))
1468 (muse-publish-mark-read-only beg (point)))
1470 (defun muse-publish-example-tag (beg end)
1471 (muse-publish-escape-specials beg end nil 'example)
1472 (goto-char beg)
1473 (insert (muse-markup-text 'begin-example))
1474 (goto-char end)
1475 (insert (muse-markup-text 'end-example))
1476 (muse-publish-mark-read-only beg (point)))
1478 (defun muse-publish-verbatim-tag (beg end)
1479 (muse-publish-escape-specials beg end nil 'verbatim)
1480 (muse-publish-mark-read-only beg end))
1482 (defalias 'muse-publish-class-tag 'ignore)
1484 (defun muse-publish-lisp-tag (beg end)
1485 (save-excursion
1486 (let ((str (muse-eval-lisp
1487 (prog1
1488 (concat "(progn "
1489 (buffer-substring-no-properties beg end)
1490 ")")
1491 (delete-region beg end)))))
1492 (set-text-properties 0 (length str) nil str)
1493 (insert str))))
1495 (defun muse-publish-command-tag (beg end attrs)
1496 (while (looking-at "\\s-*$")
1497 (forward-line))
1498 (let ((interp (cdr (assoc "interp" attrs))))
1499 (if (null interp)
1500 (shell-command
1501 (prog1
1502 (buffer-substring-no-properties (point) end)
1503 (delete-region beg end)) t)
1504 (shell-command-on-region beg end interp t t))
1505 (muse-publish-mark-read-only beg (point))))
1507 (defun muse-publish-comment-tag (beg end)
1508 (if (null muse-publish-comments-p)
1509 (delete-region beg end)
1510 (goto-char end)
1511 (muse-insert-markup (muse-markup-text 'comment-end))
1512 (muse-publish-mark-read-only beg end)
1513 (goto-char beg)
1514 (muse-insert-markup (muse-markup-text 'comment-begin))))
1516 ;; Miscellaneous helper functions
1518 (defsubst muse-publishing-directive (name)
1519 (cdr (assoc name muse-publishing-directives)))
1521 (defun muse-publish-strip-tags (string)
1522 "Remove all tags from the string."
1523 (while (string-match "<.*?>" string)
1524 (setq string (replace-match "" nil t string)))
1525 string)
1527 (defun muse-publish-markup-type (category default-func)
1528 (let ((rule (muse-find-markup-element :overrides category (muse-style))))
1529 (funcall (or rule default-func))))
1531 (defun muse-published-buffer-contents (buffer)
1532 (with-current-buffer buffer
1533 (goto-char (point-min))
1534 (let ((beg (and (search-forward "Emacs Muse begins here")
1535 (muse-line-end-position)))
1536 (end (and (search-forward "Emacs Muse ends here")
1537 (muse-line-beginning-position))))
1538 (buffer-substring-no-properties beg end))))
1540 (defun muse-published-contents (file)
1541 (when (file-readable-p file)
1542 (muse-with-temp-buffer
1543 (insert-file-contents file)
1544 (muse-published-buffer-contents (current-buffer)))))
1546 (defun muse-publish-transform-output
1547 (file temp-file output-path name gen-func &rest cleanup-exts)
1548 "Transform the given TEMP-FILE into the OUTPUT-PATH, using GEN-FUNC."
1549 (setq file (muse-page-name file))
1550 (message "Generating %s output for %s..." name file)
1551 (if (not (funcall gen-func temp-file output-path))
1552 (message "Generating %s from %s...failed" name file)
1553 (message "Generating %s output for %s...done" name file)
1554 (muse-delete-file-if-exists temp-file)
1555 (dolist (ext cleanup-exts)
1556 (muse-delete-file-if-exists
1557 (expand-file-name (concat file ext)
1558 (file-name-directory output-path))))
1559 (message "Wrote %s" output-path)))
1561 (defun muse-publish-read-only (string)
1562 (let ((end (1- (length string))))
1563 (add-text-properties 0 end
1564 '(rear-nonsticky (read-only) read-only t)
1565 string)
1566 string))
1568 (provide 'muse-publish)
1570 ;;; muse-publish.el ends here