-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtex2hugo.scm
126 lines (108 loc) · 3.39 KB
/
tex2hugo.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env gosh
(use gauche.record)
(use srfi-13)
(define (usage)
(print "usage: tex2hugo <filename>..."))
(define-record-type context #t #t
(chapter) (section) (subsection))
(define-syntax zero!
(syntax-rules ()
((_ loc)
(set! loc 0))))
(define (convert-text text)
(regexp-replace-all* text
#/\\chapter{(.*?)}/ "# \\1"
#/\\section{(.*?)}/ "## \\1"
#/\\subsection\*?{(.*?)}/ "### \\1"
#/\\begin{note}/ "*Note: *"
#/\\end{note}/ ""
#/\\begin{scheme}/ "```"
#/\\end{scheme}/ "```"
#/``([^\n]+?(\n[^\n]+?)?)''/ "\"\\1\""
#/\\begin{itemize}/ ""
#/\\end{itemize}/ ""
#/\\item / "- "
#/\\unsection/ ""
#/\\vest / ""
#/\\authorsc{(.*?)}/ "\\1"
#/\\label{(.*?)}/ "{{< label \"\\1\" >}}"
#/\\medskip/ "{{< medskip >}}"
#/{\\cf (.*?)}/ "``\\1``"
#/\\dotsfoo/ "..."
#/\\bibitem{(.*?)}/ "{{< bibitem \"\\1\" >}}"
#/~?\\cite{(.*?)}/ " {{< cite \"\\1\" >}}"
#/\\exprtype/ "syntax"
#/\\hyper{(.*?)}/ "{{< hyper \"\\1\" >}}"
#/\\index{(.*?)}/ ""
#/\\mainindex{(.*?)}/ ""
#/\\rthreers/ "{{< rnrs 3 >}}"
#/\\rfourrs/ "{{< rnrs 4 >}}"
#/\\rfivers/ "{{< rnrs 5 >}}"
#/\\rsixrs/ "{{< rnrs 6 >}}"
#/\\rsevenrs/ "{{< rnrs 7 >}}"
#/\\schfalse{}/ "{{< tt \"#f\" >}}"
#/\\schtrue{}/ "{{< tt \"#t\" >}}"
#/\\semantics/ "_Semantics:_"
#/\\syntax/ "_Syntax:_"
#/\\var{(.*?)}/ "_\\1_"
#/%+\n/ "\n"
#/\\ / " "
#/\\#/ "#"
#/\\_/ "_"
#/\\todo{(.*?)}/ ""
#/[ \t]+\n/ "\n"
))
(define (format-line line c)
(rxmatch-if (rxmatch #/^chapter = (.*)/ line) (#f chapter)
(context-chapter-set! c chapter) #f)
(regexp-replace* line
#/^# ([0-9]+\. )?([^ ].*)/
(lambda (m)
(if (context-chapter c)
(begin
(zero! (context-section c))
(format #f "# ~a. ~a" (context-chapter c) (m 2)))
(m 0)))
#/^## ([0-9]+\.[0-9]+\. )?([^ ].*)/
(lambda (m)
(if (context-chapter c)
(begin
(inc! (context-section c))
(zero! (context-subsection c))
(format #f "## ~a.~a. ~a"
(context-chapter c)
(context-section c)
(m 2)))
(m 0)))
#/^### ([0-9]+\.[0-9]+\.[0-9]+\. )?([^ ].*)/
(lambda (m)
(if (context-chapter c)
(begin
(inc! (context-subsection c))
(format #f "### ~a.~a.~a. ~a"
(context-chapter c)
(context-section c)
(context-subsection c)
(m 2)))
(m 0)))
))
(define (convert-file filename)
(let* ((tmpfile (string-append filename ".tmp"))
(in (open-input-file filename))
(out (open-output-file tmpfile :if-does-not-exist :create))
(c (make-context #f 0 0)))
(for-each (lambda (line)
(display (format-line line c) out)
(newline out))
(string-split (convert-text (string-trim-right
(port->string in)))
#\newline))
(close-port in)
(close-port out)
(sys-rename tmpfile filename)))
(define (main args)
(if (= (length args) 1)
(begin
(usage)
(exit 1)))
(for-each convert-file (cdr args)))