Update NEWS, fix muse-url-protocols customization interface.
[muse-el.git] / lisp / muse-convert.el
blobb15841a9291478bdb4ff7ec4adba3a216aa46606
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 (insert "\n[" (number-to-string note) "] " text ?\n)))
60 ;;;###autoload
61 (defun muse-latex-transform ()
62 (interactive)
63 (goto-char (point-min))
64 (while (not (eobp))
65 (cond
66 ((or (looking-at "^\\\\documentclass")
67 (looking-at "^\\\\input")
68 (looking-at "^\\\\begin{document}")
69 (looking-at "^\\\\end{document}")
70 (looking-at "^\\\\author")
71 (looking-at "^\\\\\\(med\\|big\\|small\\)skip")
72 (looking-at "^\\\\maketitle"))
73 (delete-region (point) (muse-line-end-position)))
74 ((looking-at "^\\\\title{\\(.+\\)}")
75 (delete-region (match-end 1) (muse-line-end-position))
76 (delete-region (point) (match-beginning 1))
77 (insert "#title ")))
78 (forward-line))
79 (goto-char (point-min))
80 (while (re-search-forward "\\\\\\(l\\)?dots{}" nil t)
81 (replace-match (concat (and (string= (match-string 1) "l") ".")
82 "...")))
83 (goto-char (point-min))
84 (while (re-search-forward "\\(``\\|''\\)" nil t)
85 (replace-match "\""))
86 (goto-char (point-min))
87 (while (re-search-forward "---" nil t)
88 (replace-match " -- "))
89 (goto-char (point-min))
90 (while (re-search-forward "\\\\tableofcontents" nil t)
91 (replace-match "<contents>"))
92 (goto-char (point-min))
93 (while (re-search-forward "\\\\\\\\" nil t)
94 (replace-match ""))
95 (goto-char (point-min))
96 (while (re-search-forward "\\\\\\(sub\\)?section{\\([^}]+\\)}" nil t)
97 (replace-match (concat (if (string= (match-string 1) "sub")
98 "**" "*")
99 " " (match-string 2))))
100 (goto-char (point-min))
101 (while (re-search-forward "\\\\\\(begin\\|end\\){verse}" nil t)
102 (replace-match (concat "<" (if (string= (match-string 1) "end") "/")
103 "verse>")))
104 (goto-char (point-min))
105 (while (re-search-forward "\\\\\\(begin\\|end\\){quote}\n" nil t)
106 (replace-match ""))
107 (goto-char (point-min))
108 (while (re-search-forward
109 "\\\\\\(emph\\|textbf\\){\\([^}]+?\\)\\(\\\\/\\)?}" nil t)
110 (replace-match
111 (if (string= (match-string 1) "emph") "*\\2*" "**\\2**")))
112 (let ((footnote-index 1))
113 (goto-char (point-min))
114 (while (re-search-forward
115 (concat "\\\\\\(q\\)?\\(footnote\\|excerpt\\)\\(np\\)?"
116 "\\({\\([^}]+\\)}\\)?"
117 "\\({\\([^}]+\\)}{\\([^}]+\\)}\\)?{\\([^}]+\\)}") nil t)
118 (let ((beg (match-beginning 0))
119 (end (match-end 0)))
120 (unless (string= (match-string 2) "footnote")
121 (if (null (match-string 1))
122 (insert " " (match-string 9))
123 (let ((b (point)) e)
124 (insert "\"" (match-string 9) "\"")
125 (setq e (point-marker))
126 (save-match-data
127 (save-excursion
128 (goto-char b)
129 (while (< (point) e)
130 (if (looking-at "\\s-+")
131 (delete-region (match-beginning 0)
132 (match-end 0)))
133 (forward-line)))))))
134 (insert "[" (number-to-string footnote-index) "]")
135 (if (string= (match-string 2) "footnote")
136 (muse-write-footnote footnote-index (match-string 9))
137 (muse-write-citation footnote-index (match-string 5)
138 (match-string 7) (match-string 8)))
139 (setq footnote-index (1+ footnote-index))
140 (delete-region beg end))))
141 (goto-char (point-min))
142 (while (looking-at "\n") (delete-char 1))
143 (goto-char (point-min))
144 (while (re-search-forward "\n\n+" nil t)
145 (replace-match "\n\n")))
147 (provide 'muse-convert)