]> code.delx.au - gnu-emacs-elpa/blob - packages/muse/muse-import-latex.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / muse / muse-import-latex.el
1 ;;; muse-import-latex.el --- convert a LaTex file into a Muse file
2
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5
6 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7
8 ;; Emacs Muse is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published
10 ;; by the Free Software Foundation; either version 3, or (at your
11 ;; option) any later version.
12
13 ;; Emacs Muse is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Emacs Muse; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
22
23 ;;; Commentary:
24
25 ;; Helper commands for converting a LaTeX file into a Muse file.
26
27 ;;; Contributors:
28
29 ;;; Code:
30
31 (require 'muse)
32 (require 'muse-regexps)
33
34 (defun muse-i-l-write-citation (note author citation pages)
35 (save-excursion
36 (goto-char (point-max))
37 (if (= note 1)
38 (insert "\nFootnotes:\n\n"))
39 (let ((beg (point)))
40 (insert "\n[" (number-to-string note) "] " author)
41 (if (and citation pages)
42 (insert ", " citation ", " pages))
43 (insert "\n")
44 (goto-char beg)
45 (while (re-search-forward (concat "p.\\\\[" muse-regexp-blank "\n]+")
46 nil t)
47 (replace-match "p."))
48 (goto-char beg)
49 (while (re-search-forward "--" nil t)
50 (replace-match "-")))))
51
52 (defun muse-i-l-write-footnote (note text)
53 (save-excursion
54 (goto-char (point-max))
55 (if (= note 1)
56 (insert "\nFootnotes:\n\n"))
57 (insert "\n[" (number-to-string note) "] " text ?\n)))
58
59 ;;;###autoload
60 (defun muse-import-latex ()
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) (muse-line-end-position)))
73 ((looking-at "^\\\\title{\\(.+\\)}")
74 (delete-region (match-end 1) (muse-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 (set-marker e nil))))
134 (insert "[" (number-to-string footnote-index) "]")
135 (if (string= (match-string 2) "footnote")
136 (muse-i-l-write-footnote footnote-index (match-string 9))
137 (muse-i-l-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")))
146
147 (provide 'muse-import-latex)
148
149 ;;; muse-import-latex.el ends here