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