Add experimental version of muse-blosxom.el to project
[muse-el.git] / muse-convert.el
blobdd97d461add8cafbcc469e96088cd9f18fd43f9d
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)
5 (save-excursion
6 (goto-char (point-max))
7 (if (= note 1)
8 (insert "\nFootnotes:\n\n"))
9 (let ((beg (point)))
10 (insert "\n[" (number-to-string note) "] " author)
11 (if (and citation pages)
12 (insert ", " citation ", " pages))
13 (insert "\n")
14 (goto-char beg)
15 (while (re-search-forward (concat "p.\\\\["
16 muse-regexp-space
17 "]+")
18 nil t)
19 (replace-match "p."))
20 (goto-char beg)
21 (while (re-search-forward "--" nil t)
22 (replace-match "-")))))
24 (defun muse-write-footnote (note text)
25 (save-excursion
26 (goto-char (point-max))
27 (if (= note 1)
28 (insert "\nFootnotes:\n\n"))
29 (let ((beg (point)))
30 (insert "\n[" (number-to-string note) "] " text ?\n))))
32 ;;;###autoload
33 (defun muse-latex-transform ()
34 (interactive)
35 (goto-char (point-min))
36 (while (not (eobp))
37 (cond
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))
49 (insert "#title ")))
50 (forward-line))
51 (goto-char (point-min))
52 (while (re-search-forward "\\\\\\(l\\)?dots{}" nil t)
53 (replace-match (concat (and (string= (match-string 1) "l") ".")
54 "...")))
55 (goto-char (point-min))
56 (while (re-search-forward "\\(``\\|''\\)" nil t)
57 (replace-match "\""))
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)
66 (replace-match ""))
67 (goto-char (point-min))
68 (while (re-search-forward "\\\\\\(sub\\)?section{\\([^}]+\\)}" nil t)
69 (replace-match (concat (if (string= (match-string 1) "sub")
70 "**" "*")
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") "/")
75 "verse>")))
76 (goto-char (point-min))
77 (while (re-search-forward "\\\\\\(begin\\|end\\){quote}\n" nil t)
78 (replace-match ""))
79 (goto-char (point-min))
80 (while (re-search-forward
81 "\\\\\\(emph\\|textbf\\){\\([^}]+?\\)\\(\\\\/\\)?}" nil t)
82 (replace-match
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))
91 (end (match-end 0)))
92 (unless (string= (match-string 2) "footnote")
93 (if (null (match-string 1))
94 (insert " " (match-string 9))
95 (let ((b (point)) e)
96 (insert "\"" (match-string 9) "\"")
97 (setq e (point-marker))
98 (save-match-data
99 (save-excursion
100 (goto-char b)
101 (while (< (point) e)
102 (if (looking-at "\\s-+")
103 (delete-region (match-beginning 0)
104 (match-end 0)))
105 (forward-line)))))))
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)