Merged from mwolson@gnu.org--2006 (patch 129-130)
[muse-el.git] / lisp / muse-convert.el
blobf4a2a40fd8ffc41c374452f9619777913957d50a
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 part of Emacs Muse. It is not part of GNU Emacs.
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 2, or (at your
10 ;; option) any later version.
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.
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.
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.\\\\[" muse-regexp-blank "\n]+")
45 nil t)
46 (replace-match "p."))
47 (goto-char beg)
48 (while (re-search-forward "--" nil t)
49 (replace-match "-")))))
51 (defun muse-write-footnote (note text)
52 (save-excursion
53 (goto-char (point-max))
54 (if (= note 1)
55 (insert "\nFootnotes:\n\n"))
56 (insert "\n[" (number-to-string note) "] " text ?\n)))
58 ;;;###autoload
59 (defun muse-latex-transform ()
60 (interactive)
61 (goto-char (point-min))
62 (while (not (eobp))
63 (cond
64 ((or (looking-at "^\\\\documentclass")
65 (looking-at "^\\\\input")
66 (looking-at "^\\\\begin{document}")
67 (looking-at "^\\\\end{document}")
68 (looking-at "^\\\\author")
69 (looking-at "^\\\\\\(med\\|big\\|small\\)skip")
70 (looking-at "^\\\\maketitle"))
71 (delete-region (point) (muse-line-end-position)))
72 ((looking-at "^\\\\title{\\(.+\\)}")
73 (delete-region (match-end 1) (muse-line-end-position))
74 (delete-region (point) (match-beginning 1))
75 (insert "#title ")))
76 (forward-line))
77 (goto-char (point-min))
78 (while (re-search-forward "\\\\\\(l\\)?dots{}" nil t)
79 (replace-match (concat (and (string= (match-string 1) "l") ".")
80 "...")))
81 (goto-char (point-min))
82 (while (re-search-forward "\\(``\\|''\\)" nil t)
83 (replace-match "\""))
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 "\\\\tableofcontents" nil t)
89 (replace-match "<contents>"))
90 (goto-char (point-min))
91 (while (re-search-forward "\\\\\\\\" nil t)
92 (replace-match ""))
93 (goto-char (point-min))
94 (while (re-search-forward "\\\\\\(sub\\)?section{\\([^}]+\\)}" nil t)
95 (replace-match (concat (if (string= (match-string 1) "sub")
96 "**" "*")
97 " " (match-string 2))))
98 (goto-char (point-min))
99 (while (re-search-forward "\\\\\\(begin\\|end\\){verse}" nil t)
100 (replace-match (concat "<" (if (string= (match-string 1) "end") "/")
101 "verse>")))
102 (goto-char (point-min))
103 (while (re-search-forward "\\\\\\(begin\\|end\\){quote}\n" nil t)
104 (replace-match ""))
105 (goto-char (point-min))
106 (while (re-search-forward
107 "\\\\\\(emph\\|textbf\\){\\([^}]+?\\)\\(\\\\/\\)?}" nil t)
108 (replace-match
109 (if (string= (match-string 1) "emph") "*\\2*" "**\\2**")))
110 (let ((footnote-index 1))
111 (goto-char (point-min))
112 (while (re-search-forward
113 (concat "\\\\\\(q\\)?\\(footnote\\|excerpt\\)\\(np\\)?"
114 "\\({\\([^}]+\\)}\\)?"
115 "\\({\\([^}]+\\)}{\\([^}]+\\)}\\)?{\\([^}]+\\)}") nil t)
116 (let ((beg (match-beginning 0))
117 (end (match-end 0)))
118 (unless (string= (match-string 2) "footnote")
119 (if (null (match-string 1))
120 (insert " " (match-string 9))
121 (let ((b (point)) e)
122 (insert "\"" (match-string 9) "\"")
123 (setq e (point-marker))
124 (save-match-data
125 (save-excursion
126 (goto-char b)
127 (while (< (point) e)
128 (if (looking-at "\\s-+")
129 (delete-region (match-beginning 0)
130 (match-end 0)))
131 (forward-line)))))))
132 (insert "[" (number-to-string footnote-index) "]")
133 (if (string= (match-string 2) "footnote")
134 (muse-write-footnote footnote-index (match-string 9))
135 (muse-write-citation footnote-index (match-string 5)
136 (match-string 7) (match-string 8)))
137 (setq footnote-index (1+ footnote-index))
138 (delete-region beg end))))
139 (goto-char (point-min))
140 (while (looking-at "\n") (delete-char 1))
141 (goto-char (point-min))
142 (while (re-search-forward "\n\n+" nil t)
143 (replace-match "\n\n")))
145 (provide 'muse-convert)