1 ;; Helper commands for converting a LaTeX file into a Muse file
2 (require 'muse-regexps
)
4 (defun muse-write-citation (note author citation pages
)
6 (goto-char (point-max))
8 (insert "\nFootnotes:\n\n"))
10 (insert "\n[" (number-to-string note
) "] " author
)
11 (if (and citation pages
)
12 (insert ", " citation
", " pages
))
15 (while (re-search-forward (concat "p.\\\\["
21 (while (re-search-forward "--" nil t
)
22 (replace-match "-")))))
24 (defun muse-write-footnote (note text
)
26 (goto-char (point-max))
28 (insert "\nFootnotes:\n\n"))
30 (insert "\n[" (number-to-string note
) "] " text ?
\n))))
33 (defun muse-latex-transform ()
35 (goto-char (point-min))
38 ((or (looking-at "^\\\\documentclass")
39 (looking-at "^\\\\input")
40 (looking-at "^\\\\begin{document}")
41 (looking-at "^\\\\end{document}")
42 (looking-at "^\\\\author")
43 (looking-at "^\\\\\\(med\\|big\\|small\\)skip")
44 (looking-at "^\\\\maketitle"))
45 (delete-region (point) (line-end-position)))
46 ((looking-at "^\\\\title{\\(.+\\)}")
47 (delete-region (match-end 1) (line-end-position))
48 (delete-region (point) (match-beginning 1))
51 (goto-char (point-min))
52 (while (re-search-forward "\\\\\\(l\\)?dots{}" nil t
)
53 (replace-match (concat (and (string= (match-string 1) "l") ".")
55 (goto-char (point-min))
56 (while (re-search-forward "\\(``\\|''\\)" nil t
)
58 (goto-char (point-min))
59 (while (re-search-forward "---" nil t
)
60 (replace-match " -- "))
61 (goto-char (point-min))
62 (while (re-search-forward "\\\\tableofcontents" nil t
)
63 (replace-match "<contents>"))
64 (goto-char (point-min))
65 (while (re-search-forward "\\\\\\\\" nil t
)
67 (goto-char (point-min))
68 (while (re-search-forward "\\\\\\(sub\\)?section{\\([^}]+\\)}" nil t
)
69 (replace-match (concat (if (string= (match-string 1) "sub")
71 " " (match-string 2))))
72 (goto-char (point-min))
73 (while (re-search-forward "\\\\\\(begin\\|end\\){verse}" nil t
)
74 (replace-match (concat "<" (if (string= (match-string 1) "end") "/")
76 (goto-char (point-min))
77 (while (re-search-forward "\\\\\\(begin\\|end\\){quote}\n" nil t
)
79 (goto-char (point-min))
80 (while (re-search-forward
81 "\\\\\\(emph\\|textbf\\){\\([^}]+?\\)\\(\\\\/\\)?}" nil t
)
83 (if (string= (match-string 1) "emph") "*\\2*" "**\\2**")))
84 (let ((footnote-index 1))
85 (goto-char (point-min))
86 (while (re-search-forward
87 (concat "\\\\\\(q\\)?\\(footnote\\|excerpt\\)\\(np\\)?"
88 "\\({\\([^}]+\\)}\\)?"
89 "\\({\\([^}]+\\)}{\\([^}]+\\)}\\)?{\\([^}]+\\)}") nil t
)
90 (let ((beg (match-beginning 0))
92 (unless (string= (match-string 2) "footnote")
93 (if (null (match-string 1))
94 (insert " " (match-string 9))
96 (insert "\"" (match-string 9) "\"")
97 (setq e
(point-marker))
102 (if (looking-at "\\s-+")
103 (delete-region (match-beginning 0)
106 (insert "[" (number-to-string footnote-index
) "]")
107 (if (string= (match-string 2) "footnote")
108 (muse-write-footnote footnote-index
(match-string 9))
109 (muse-write-citation footnote-index
(match-string 5)
110 (match-string 7) (match-string 8)))
111 (setq footnote-index
(1+ footnote-index
))
112 (delete-region beg end
))))
113 (goto-char (point-min))
114 (while (looking-at "\n") (delete-char 1))
115 (goto-char (point-min))
116 (while (re-search-forward "\n\n+" nil t
)
117 (replace-match "\n\n")))
119 (provide 'muse-convert
)