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