Update NEWS.
[muse-el.git] / experimental / muse-cite.el
blobe8f573ab96a30ed981686257a3bdebe020a564ca
1 ;;; muse-cire.el --- smart citations for Muse
3 ;; Copyright (C) 2005 Free Software Foundation, Inc.
5 ;; This file is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
10 ;; This file is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to the
17 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;; Boston, MA 02110-1301, USA.
20 ;;; Commentary:
22 ;; This file is currently in experimental state. I found it in an old
23 ;; pre-release version of Muse and thought it might come in handy.
25 ;;; Contributors:
27 ;;; Code:
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; Muse Smart Citations
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;;; Commentary:
37 ;; If a footnote is of the general form "AUTHOR, TITLE, PAGES", this
38 ;; module offers a function to more intelligently markup such
39 ;; citations. For LaTeX, it italicizes the TITLE and inserts correct
40 ;; spacing and endashes in PAGES. For HTML, it is able to convert the
41 ;; TITLE or PAGES into a link, given knowledge of where to find known
42 ;; texts by certain authors.
44 ;; To use this module -- since it only rewrites markup, and is not
45 ;; particular to any style -- modify `muse-before-publish-hook':
47 ;; (require 'muse-publish)
48 ;; (require 'muse-cite)
49 ;; (add-hook 'muse-before-publish-hook 'muse-cite-munge-footnotes)
51 (require 'muse-publish)
53 (defgroup muse-cite nil
54 "Offers functionality for marking up smart citations."
55 :group 'muse-publish)
57 (defcustom muse-cite-titles nil
58 "An alist of authors and the titles they've written.
59 This is how titles are recognized, and marked up as links to the
60 title and to the specific pages referenced.
62 This variable is an alist of the form:
64 ((AUTHOR . TITLE-LIST)
65 ...)
67 Where AUTHOR is a string, and TITLE-LIST is a list of the form:
69 ((TITLE URL [PAGE-URL])
70 ...)
72 Where TITLE is a string, URL is a URL string, and PAGE-URL can be
73 nil or a URL string with %d somewhere in it -- which is substituted
74 with the first page number mentioned in the reference."
75 :type '(alist :key-type (string :tag "Author")
76 :value-type
77 (repeat (list (string :tag "Title")
78 (string :tag "URL")
79 (choice (string :tag "Page URL")
80 (const :tag "No Page URL" nil)))))
81 :group 'muse-cite)
83 (defun muse-cite-rewrite (citation)
84 "Rewrite an 'Author, Title, Pages' CITATION as an intelligent reference."
85 (when (string-match
86 (concat "\\([^,]+\\), *\\([^,]+\\), *"
87 "\\(pp?\\. *\\([0-9]+\\)\\(-+[0-9]+\\)?\\)") citation)
88 (let* ((author (match-string 1 citation))
89 (title (match-string 2 citation))
90 (pages (match-string 3 citation))
91 (page (match-string 4 citation))
92 (author-entry (assoc author muse-cite-titles))
93 (book-entry (and author-entry
94 (assoc title (cdr author-entry))))
95 (book-url (car (cdr book-entry)))
96 (book-page (car (cddr book-entry))))
97 (cond
98 ((null book-url)
99 (format "%s, *%s*, %s" author title pages))
100 ((or (null book-page)
101 (not (string-match "%d" book-page)))
102 (format "%s, [[%s][%s]], %s" author book-url title pages))
104 (setq book-page (replace-match page nil t book-page))
105 (format "%s, [[%s][%s]], [[%s][%s]]"
106 author book-url title book-page pages))))))
108 (defun muse-cite-munge-footnotes ()
109 "Munge the footnote citations in the current buffer.
110 The author/title definitions given in `muse-cite-titles' are used
111 to change the citations automagically into hyperlinks.."
112 (goto-char (point-max))
113 (when (re-search-backward "^Footnotes" nil t)
114 (while (re-search-forward "^\\[[0-9]+\\][ \t]+\\(.+\\)" nil t)
115 (let ((end (copy-marker (match-end 0) t))
116 (rewrite (save-match-data
117 (muse-cite-rewrite (match-string 1)))))
118 (when rewrite
119 (goto-char (match-beginning 1))
120 (delete-region (match-beginning 1) (match-end 1))
121 (insert rewrite))
122 (goto-char end))))
123 nil)
125 (provide 'muse-cite)
127 ;;; muse-cite.el ends here