Fix publishing bug introduced in last patch.
[muse-el.git] / lisp / muse-wiki.el
blobe3022b15ce773b69c2b241d5bd09a2512a3c8c16
1 ;;; muse-wiki.el --- wiki features for Muse
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Yann Hodique <Yann.Hodique@lifl.fr>
6 ;; Keywords:
8 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
10 ;; Emacs Muse is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
15 ;; Emacs Muse is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with Emacs Muse; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;;; Contributors:
29 ;; Per B. Sederberg (per AT med DOT upenn DOT edu) made it so that all
30 ;; files in a Muse project can become implicit links.
32 ;;; Code:
34 (require 'muse-regexps)
35 (require 'muse-mode)
37 (eval-when-compile
38 (require 'muse-colors))
40 (defgroup muse-wiki nil
41 "Options controlling the behavior of Emacs Muse Wiki features."
42 :group 'muse-mode)
44 (defcustom muse-wiki-use-wikiword t
45 "Whether to use color and publish bare WikiNames."
46 :type 'boolean
47 :group 'muse-wiki)
49 (defcustom muse-wiki-allow-nonexistent-wikiword nil
50 "Whether to color bare WikiNames that don't have an existing file."
51 :type 'boolean
52 :group 'muse-wiki)
54 (defcustom muse-wiki-match-all-project-files nil
55 "Whether to extend WikiName functionality to also match
56 existing filenames, regardless of whether they are named in
57 WikiWord format.
59 If non-nil, Muse will color and publish implicit links to any
60 file in your project."
61 :type 'boolean
62 :group 'muse-wiki)
64 (defcustom muse-wiki-ignore-implicit-links-to-current-page nil
65 "Whether to ignore implicit links to the current page.
67 If non-nil, Muse will not recognize implicit links to the current
68 page, both when formatting and publishing."
69 :type 'boolean
70 :group 'muse-wiki)
72 (eval-when-compile
73 (defvar muse-wiki-wikiword-regexp))
75 (defun muse-wiki-update-local-wikiword-regexp ()
76 "Update a local copy of `muse-wiki-wikiword-regexp' to include
77 all the files in the project."
78 ;; see if the user wants to append project files
79 (when (and muse-wiki-use-wikiword
80 muse-wiki-match-all-project-files)
81 ;; make the regexp local
82 (set (make-local-variable 'muse-wiki-wikiword-regexp)
83 (concat "\\(\\<\\(?:"
84 ;; append the files from the project
85 (mapconcat 'car
86 (muse-project-file-alist (muse-project))
87 "\\|")
88 "\\)\\>\\|\\(?:"
89 (default-value 'muse-wiki-wikiword-regexp)
90 "\\)\\)"))
91 ;; update coloring setup
92 (when (featurep 'muse-colors)
93 (muse-configure-highlighting
94 'muse-colors-markup muse-colors-markup))))
96 (add-hook 'muse-update-values-hook
97 'muse-wiki-update-local-wikiword-regexp)
98 (add-hook 'muse-project-file-alist-hook
99 'muse-wiki-update-local-wikiword-regexp)
101 (defun muse-wiki-update-wikiword-regexp (sym val)
102 "Update everything related to `muse-wiki-wikiword-regexp'."
103 (set sym val)
104 (muse-wiki-update-local-wikiword-regexp)
105 (when (featurep 'muse-colors)
106 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
108 (defcustom muse-wiki-wikiword-regexp
109 (concat "\\<\\(\\(?:[" muse-regexp-upper
110 "]+[" muse-regexp-lower "]+\\)\\(?:["
111 muse-regexp-upper "]+[" muse-regexp-lower "]+\\)+\\)")
112 "Regexp used to match WikiWords."
113 :type 'regexp
114 :group 'muse-wiki
115 :set 'muse-wiki-update-wikiword-regexp)
117 (defcustom muse-wiki-ignore-bare-project-names nil
118 "Determine whether project names without a page specifer are links.
119 If non-nil, project names without a page specifier will not be
120 considered links.
121 When nil, project names without a specifier are highlighted and
122 they link to the default page of the project that they name."
123 :type 'boolean
124 :group 'muse-wiki)
126 (defvar muse-wiki-interwiki-regexp ""
127 "Regexp that matches all interwiki links.
128 This is automatically generated by setting `muse-wiki-interwiki-alist'.
129 It can also be set by calling `muse-wiki-update-interwiki-regexp'.")
131 (defcustom muse-wiki-interwiki-delimiter "#\\|::"
132 "Delimiter regexp used for InterWiki links.
133 If you use groups, use only shy groups."
134 :type 'regexp
135 :group 'muse-wiki)
137 (defcustom muse-wiki-interwiki-replacement ": "
138 "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
139 InterWiki link descriptions.
141 If you want this replacement to happen, you must add
142 `muse-wiki-publish-pretty-interwiki' to
143 `muse-publish-desc-transforms'."
144 :type 'regexp
145 :group 'muse-wiki)
147 (eval-when-compile
148 (defvar muse-wiki-interwiki-alist))
150 (defun muse-wiki-project-files-with-spaces (&optional project)
151 "Return a list of files in PROJECT that have spaces."
152 (setq project (muse-project project))
153 (let ((flist nil))
154 (save-match-data
155 (mapcar (function (lambda (file)
156 (when (string-match " " (car file))
157 (setq flist (cons (car file) flist)))))
158 (muse-project-file-alist project)))
159 flist))
161 (defun muse-wiki-update-interwiki-regexp ()
162 "Update the value of `muse-wiki-interwiki-regexp' based on
163 `muse-wiki-interwiki-alist' and `muse-project-alist'."
164 (setq muse-wiki-interwiki-regexp
165 (concat "\\<\\(" (mapconcat 'car muse-project-alist "\\|")
166 (when muse-wiki-interwiki-alist
167 (concat "\\|" (mapconcat 'car muse-wiki-interwiki-alist
168 "\\|")))
169 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
170 "\\)\\("
171 (when muse-wiki-match-all-project-files
172 ;; append the files from the project
173 (concat
174 (mapconcat
175 (function
176 (lambda (proj)
177 (mapconcat 'identity
178 (muse-wiki-project-files-with-spaces
179 (car proj))
180 "\\|")))
181 muse-project-alist "")
182 "\\|"))
183 "\\sw+\\)\\)?\\>"))
184 (when (featurep 'muse-colors)
185 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
187 (defcustom muse-wiki-interwiki-alist
188 '(("EmacsWiki" . "http://www.emacswiki.org/cgi-bin/wiki/"))
189 "A table of WikiNames that refer to external entities.
190 The format of this table is an alist, or series of cons cells.
191 Each cons cell must be of the form:
193 (WIKINAME . STRING-OR-FUNCTION)
195 The second part of the cons cell may either be a STRING, which in most
196 cases should be a URL, or a FUNCTION. If a function, it will be
197 called with one argument: the tag applied to the Interwiki name, or
198 nil if no tag was used. If the cdr was a STRING and a tag is used,
199 the tag is simply appended.
201 Here are some examples:
203 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
205 Referring to [[JohnWiki::EmacsModules]] then really means:
207 http://alice.dynodns.net/wiki?EmacsModules
209 If a function is used for the replacement text, you can get creative
210 depending on what the tag is. Tags may contain any alphabetic
211 character, any number, % or _. If you need other special characters,
212 use % to specify the hex code, as in %2E. All browsers should support
213 this."
214 :type '(repeat (cons (string :tag "WikiName")
215 (choice (string :tag "URL") function)))
216 :set (function
217 (lambda (sym value)
218 (set sym value)
219 (muse-wiki-update-interwiki-regexp)))
220 :group 'muse-wiki)
222 (add-hook 'muse-update-values-hook
223 'muse-wiki-update-interwiki-regexp)
225 (defun muse-wiki-resolve-project-page (&optional project page)
226 "Return the published path from the current page to PAGE of PROJECT.
227 If PAGE is not specified, use the value of :default in PROJECT.
228 If PROJECT is not specified, default to first project of
229 `muse-projects-alist'.
231 Note that PAGE can have several output directories. If this is
232 the case, we will use the first one that matches our current
233 style and has the same link suffix, ignoring the others. If no
234 style has the same link suffix as the current publishing style,
235 use the first style we find."
236 (setq project (or (and project
237 (muse-project project))
238 (car muse-project-alist))
239 page (or page (muse-get-keyword :default
240 (cadr project))))
241 (let* ((page-path (muse-project-page-file page project))
242 (remote-styles (when page-path (muse-project-applicable-styles
243 page-path (cddr project))))
244 (local-style (muse-project-current-output-style page-path project)))
245 (cond ((and remote-styles local-style muse-publishing-p)
246 (muse-project-resolve-link page local-style remote-styles))
247 ((not muse-publishing-p)
248 (if page-path
249 page-path
250 (when muse-wiki-allow-nonexistent-wikiword
251 ;; make a path to a nonexistent file in project
252 (setq page-path (expand-file-name
253 page (car (cadr project))))
254 (if (and muse-file-extension
255 (not (string= muse-file-extension "")))
256 (concat page-path "." muse-file-extension)
257 page-path)))))))
259 (defun muse-wiki-handle-interwiki (&optional string)
260 "If STRING or point has an interwiki link, resolve it and
261 return the first match.
262 Match 1 is set to the link.
263 Match 2 is set to the description."
264 (when (if string (string-match muse-wiki-interwiki-regexp string)
265 (looking-at muse-wiki-interwiki-regexp))
266 (let* ((project (match-string 1 string))
267 (subst (cdr (assoc project muse-wiki-interwiki-alist)))
268 (word (if string
269 (and (match-beginning 2)
270 (substring string (match-beginning 2)))
271 (match-string 2 string))))
272 (if subst
273 (if (functionp subst)
274 (funcall subst word)
275 (concat subst word))
276 (and (assoc project muse-project-alist)
277 (or word (not muse-wiki-ignore-bare-project-names))
278 (muse-wiki-resolve-project-page project word))))))
280 (defun muse-wiki-handle-wikiword (&optional string)
281 "If STRING or point has a WikiWord, return it.
282 Match 1 is set to the WikiWord."
283 (when (and muse-wiki-use-wikiword
284 (if string
285 (string-match muse-wiki-wikiword-regexp string)
286 (looking-at muse-wiki-wikiword-regexp))
287 (cond
288 (muse-wiki-allow-nonexistent-wikiword
290 ((and muse-wiki-ignore-implicit-links-to-current-page
291 (string= (match-string 1 string) (muse-page-name)))
292 nil)
293 ((and (muse-project-of-file)
294 (muse-project-page-file
295 (match-string 1 string) muse-current-project t))
297 ((file-exists-p (match-string 1 string))
299 (t nil)))
300 (match-string 1 string)))
302 ;; Prettifications
304 (defcustom muse-wiki-publish-small-title-words
305 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
306 "Strings that should be downcased in a page title.
307 This is used by `muse-wiki-publish-pretty-title', which must be
308 called manually."
309 :type '(repeat string)
310 :group 'muse-wiki)
312 (defcustom muse-wiki-hide-nop-tag t
313 "If non-nil, hide <nop> tags when coloring a Muse buffer."
314 :type 'boolean
315 :group 'muse-wiki)
317 (defun muse-wiki-publish-pretty-title (&optional title explicit)
318 "Return a pretty version of the given TITLE.
319 If EXPLICIT is non-nil, TITLE will be returned unmodified."
320 (unless title (setq title (or (muse-publishing-directive "title") "")))
321 (if (or explicit
322 (save-match-data (string-match muse-url-regexp title)))
323 title
324 (save-match-data
325 (let ((case-fold-search nil))
326 (while (string-match (concat "\\([" muse-regexp-lower
327 "]\\)\\([" muse-regexp-upper
328 "0-9]\\)")
329 title)
330 (setq title (replace-match "\\1 \\2" t nil title)))
331 (let* ((words (split-string title))
332 (w (cdr words)))
333 (while w
334 (if (member (downcase (car w))
335 muse-wiki-publish-small-title-words)
336 (setcar w (downcase (car w))))
337 (setq w (cdr w)))
338 (mapconcat 'identity words " "))))))
340 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
341 "Replace instances of `muse-wiki-interwiki-delimiter' with
342 `muse-wiki-interwiki-replacement'."
343 (if (or explicit
344 (save-match-data (string-match muse-url-regexp desc)))
345 desc
346 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
347 muse-wiki-interwiki-replacement
348 desc)))
350 ;; Coloring setup
352 (eval-after-load "muse-colors"
353 '(progn
354 (defun muse-wiki-colors-nop-tag (beg end)
355 (when muse-wiki-hide-nop-tag
356 (add-text-properties beg (+ beg 5)
357 '(invisible muse intangible t))))
358 (defun muse-colors-wikiword-separate ()
359 (add-text-properties (match-beginning 0) (match-end 0)
360 '(invisible muse intangible t)))
362 (add-to-list 'muse-colors-tags
363 '("nop" nil nil muse-wiki-colors-nop-tag)
366 (add-to-list 'muse-colors-markup
367 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
369 (add-to-list 'muse-colors-markup
370 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
372 (add-to-list 'muse-colors-markup
373 '("''''" ?\' muse-colors-wikiword-separate)
374 nil)
376 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
378 ;; Publishing setup
380 (eval-after-load "muse-publish"
381 '(progn
382 (add-to-list 'muse-publish-markup-regexps
383 '(3100 muse-wiki-interwiki-regexp 0 link)
385 (add-to-list 'muse-publish-markup-regexps
386 '(3200 muse-wiki-wikiword-regexp 0 link)
388 (add-to-list 'muse-publish-markup-regexps
389 '(3300 "''''" 0 "")
392 (custom-add-option 'muse-publish-desc-transforms
393 'muse-wiki-publish-pretty-interwiki)
394 (custom-add-option 'muse-publish-desc-transforms
395 'muse-wiki-publish-pretty-title)))
397 ;; Insinuate link handling
399 (custom-add-option 'muse-implicit-link-functions
400 'muse-wiki-handle-interwiki)
401 (custom-add-option 'muse-implicit-link-functions
402 'muse-wiki-handle-wikiword)
404 (custom-add-option 'muse-explicit-link-functions
405 'muse-wiki-handle-interwiki)
407 (add-to-list 'muse-implicit-link-functions
408 'muse-wiki-handle-interwiki t)
409 (add-to-list 'muse-implicit-link-functions
410 'muse-wiki-handle-wikiword t)
412 (add-to-list 'muse-explicit-link-functions
413 'muse-wiki-handle-interwiki t)
415 ;; Obsolete functions
417 (defun muse-wiki-update-custom-values ()
418 (muse-display-warning
419 (concat "Please remove `muse-wiki-update-custom-values' from"
420 " `muse-mode-hook'. Its use is now deprecated.")))
422 (provide 'muse-wiki)
423 ;;; muse-wiki.el ends here