Move lisp files to `lisp' directory; hack on Makefiles, XEmacs compat.
[muse-el.git] / lisp / muse-convert.el
blobe7227ce2b312e3ea2fe85c2f8435cb329022cf3a
1 ;;; muse-convert.el --- Convert a LaTex file into a Muse file.
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 ;; Helper commands for converting a LaTeX file into a Muse file.
26 ;;; Contributors:
28 ;;; Code:
30 (require 'muse)
31 (require 'muse-regexps)
33 (defun muse-write-citation (note author citation pages)
34 (save-excursion
35 (goto-char (point-max))
36 (if (= note 1)
37 (insert "\nFootnotes:\n\n"))
38 (let ((beg (point)))
39 (insert "\n[" (number-to-string note) "] " author)
40 (if (and citation pages)
41 (insert ", " citation ", " pages))
42 (insert "\n")
43 (goto-char beg)
44 (while (re-search-forward (concat "p.\\\\["
45 muse-regexp-space
46 "]+")
47 nil t)
48 (replace-match "p."))
49 (goto-char beg)
50 (while (re-search-forward "--" nil t)
51 (replace-match "-")))))
53 (defun muse-write-footnote (note text)
54 (save-excursion
55 (goto-char (point-max))
56 (if (= note 1)
57 (insert "\nFootnotes:\n\n"))
58 (let ((beg (point)))
59 (insert "\n[" (number-to-string note) "] " text ?\n))))
61 ;;;###autoload
62 (defun muse-latex-transform ()
63 (interactive)
64 (goto-char (point-min))
65 (while (not (eobp))
66 (cond
67 ((or (looking-at "^\\\\documentclass")
68 (looking-at "^\\\\input")
69 (looking-at "^\\\\begin{document}")
70 (looking-at "^\\\\end{document}")
71 (looking-at "^\\\\author")
72 (looking-at "^\\\\\\(med\\|big\\|small\\)skip")
73 (looking-at "^\\\\maketitle"))
74 (delete-region (point) (muse-line-end-position)))
75 ((looking-at "^\\\\title{\\(.+\\)}")
76 (delete-region (match-end 1) (muse-line-end-position))
77 (delete-region (point) (match-beginning 1))
78 (insert "#title ")))
79 (forward-line))
80 (goto-char (point-min))
81 (while (re-search-forward "\\\\\\(l\\)?dots{}" nil t)
82 (replace-match (concat (and (string= (match-string 1) "l") ".")
83 "...")))
84 (goto-char (point-min))
85 (while (re-search-forward "\\(``\\|''\\)" nil t)
86 (replace-match "\""))
87 (goto-char (point-min))
88 (while (re-search-forward "---" nil t)
89 (replace-match " -- "))
90 (goto-char (point-min))
91 (while (re-search-forward "\\\\tableofcontents" nil t)
92 (replace-match "<contents>"))
93 (goto-char (point-min))
94 (while (re-search-forward "\\\\\\\\" nil t)
95 (replace-match ""))
96 (goto-char (point-min))
97 (while (re-search-forward "\\\\\\(sub\\)?section{\\([^}]+\\)}" nil t)
98 (replace-match (concat (if (string= (match-string 1) "sub")
99 "**" "*")
100 " " (match-string 2))))
101 (goto-char (point-min))
102 (while (re-search-forward "\\\\\\(begin\\|end\\){verse}" nil t)
103 (replace-match (concat "<" (if (string= (match-string 1) "end") "/")
104 "verse>")))
105 (goto-char (point-min))
106 (while (re-search-forward "\\\\\\(begin\\|end\\){quote}\n" nil t)
107 (replace-match ""))
108 (goto-char (point-min))
109 (while (re-search-forward
110 "\\\\\\(emph\\|textbf\\){\\([^}]+?\\)\\(\\\\/\\)?}" nil t)
111 (replace-match
112 (if (string= (match-string 1) "emph") "*\\2*" "**\\2**")))
113 (let ((footnote-index 1))
114 (goto-char (point-min))
115 (while (re-search-forward
116 (concat "\\\\\\(q\\)?\\(footnote\\|excerpt\\)\\(np\\)?"
117 "\\({\\([^}]+\\)}\\)?"
118 "\\({\\([^}]+\\)}{\\([^}]+\\)}\\)?{\\([^}]+\\)}") nil t)
119 (let ((beg (match-beginning 0))
120 (end (match-end 0)))
121 (unless (string= (match-string 2) "footnote")
122 (if (null (match-string 1))
123 (insert " " (match-string 9))
124 (let ((b (point)) e)
125 (insert "\"" (match-string 9) "\"")
126 (setq e (point-marker))
127 (save-match-data
128 (save-excursion
129 (goto-char b)
130 (while (< (point) e)
131 (if (looking-at "\\s-+")
132 (delete-region (match-beginning 0)
133 (match-end 0)))
134 (forward-line)))))))
135 (insert "[" (number-to-string footnote-index) "]")
136 (if (string= (match-string 2) "footnote")
137 (muse-write-footnote footnote-index (match-string 9))
138 (muse-write-citation footnote-index (match-string 5)
139 (match-string 7) (match-string 8)))
140 (setq footnote-index (1+ footnote-index))
141 (delete-region beg end))))
142 (goto-char (point-min))
143 (while (looking-at "\n") (delete-char 1))
144 (goto-char (point-min))
145 (while (re-search-forward "\n\n+" nil t)
146 (replace-match "\n\n")))
148 (provide 'muse-convert)