-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert-latex-markdown.sh
executable file
·170 lines (148 loc) · 5.13 KB
/
convert-latex-markdown.sh
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
#
# Tiny little script to convert a given latex file to markdown
#
SCRIPT_DIR="$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)"
source="$1"
target="$2"
acronyms="${SCRIPT_DIR}/acronyms.tex"
function replace() {
(
set -x
gsed -i "$1" "${target}"
)
}
function replaceAcronyms() {
local acronym
local replace_with
while read -r acronym ; do
replace_with=$(
gsed -n -e "s@\\\newacronym{${acronym}}{\([^}]*\)}{\([^}]*\)}@\2@p" "${acronyms}"
)
if [[ -z "${replace_with}" ]] ; then
echo "WARNING: Could not find replacement for acronym ${acronym}"
continue
fi
echo "replace ${acronym} with ${replace_with}"
replace "s@\\\glsfmtlong{${acronym}}@${replace_with}@g"
done < <(gsed -n -e "s/.*\\\glsfmtlong{\([^}]*\)}.*/\1/p" "${target}")
while read -r acronym ; do
replace_with=$(
gsed -n -e "s@\\\newacronym{${acronym}}{\([^}]*\)}{\([^}]*\)}@\2@p" "${acronyms}"
)
if [[ -z "${replace_with}" ]] ; then
echo "WARNING: Could not find replacement for acronym ${acronym}"
continue
fi
echo "replace ${acronym} with ${replace_with}"
replace "s@\\\glspl{${acronym}}@${replace_with}s@g"
done < <(gsed -n -e "s/.*\\glspl{\([^}]*\)}.*/\1/p" "${target}")
while read -r acronym ; do
replace_with=$(
gsed -n -e "s@\\\newacronym{${acronym}}{\([^}]*\)}{\([^}]*\)}@\2@p" "${acronyms}"
)
if [[ -z "${replace_with}" ]] ; then
echo "WARNING: Could not find replacement for acronym ${acronym}"
continue
fi
echo "replace ${acronym} with ${replace_with}"
replace "s@\\\glsfmtlongpl{${acronym}}@${replace_with}s@g"
done < <(gsed -n -e "s/.*\\glsfmtlongpl{\([^}]*\)}.*/\1/p" "${target}")
while read -r acronym ; do
replace_with=$(
gsed -n -e "s@\\\newacronym{${acronym}}{\([^}]*\)}{\([^}]*\)}@\2@p" "${acronyms}"
)
if [[ -z "${replace_with}" ]] ; then
echo "WARNING: Could not find replacement for acronym ${acronym}"
continue
fi
echo "replace ${acronym} with ${replace_with}"
replace "s@\\\Glsfmtlongpl{${acronym}}@${replace_with}s@g"
done < <(gsed -n -e "s/.*\\Glsfmtlongpl{\([^}]*\)}.*/\1/p" "${target}")
while read -r acronym ; do
replace_with=$(
gsed -n -e "s@\\\newacronym{${acronym}}{\([^}]*\)}{\([^}]*\)}@\2@p" "${acronyms}"
)
if [[ -z "${replace_with}" ]] ; then
echo "WARNING: Could not find replacement for acronym ${acronym}"
continue
fi
echo "replace ${acronym} with ${replace_with}"
replace "s@\\\glsxtrfull{${acronym}}@${replace_with}s@g"
done < <(gsed -n -e "s/.*\\glsxtrfull{\([^}]*\)}.*/\1/p" "${target}")
while read -r acronym ; do
replace_with=$(
gsed -n -e "s@\\\newacronym{${acronym}}{\([^}]*\)}{\([^}]*\)}@\2@p" "${acronyms}"
)
if [[ -z "${replace_with}" ]] ; then
echo "WARNING: Could not find replacement for acronym ${acronym}"
continue
fi
echo "replace ${acronym} with ${replace_with}"
replace "s@\\\glsfirst{${acronym}}@${replace_with}s@g"
done < <(gsed -n -e "s/.*\\glsfirst{\([^}]*\)}.*/\1/p" "${target}")
}
function replaceStuff() {
replaceAcronyms || return $?
replace 's@\\'\''---\\'\''@---@g'
replace 's@\\,---\\,@---@g'
replace 's@\\glsfmtshort{\(.*\)}@\\gls{\1}@g'
replace 's@\\glsfmtshort{\(.*\)}@\\gls{\1}@g'
replace 's@\\glsxtrshort{\(.*\)}@\\gls{\1}@g'
replace 's@\\gls{ekg}@EKG@g'
replace 's@\\gls{ekgmm}@EKG/Maturity@g'
replace 's@\\gls{ekgprinciples}@EKG/Principles@g'
replace 's@\\gls{ekgmethod}@EKG/Method@g'
replace 's@\\stardogcompany@Stardog@g'
replace 's@\\stardog@Stardog@g'
replace 's@\\ontotext@Ontotext@g'
replace 's@\\dataworld@data.world@g'
replace 's@\\cambridgesemantics@Cambridge Semantics@g'
replace 's@\\agnos@agnos.ai@g'
replace 's@\\eccenca@Eccenca@g'
replace 's@\\gls{ekg:coe}@EKG Center of Excellence@g'
replace 's@\\glsfirst{ekgmm}@EKG/Maturity@g'
replace 's@\\chapter{\(.*\)}@# \1@g'
replace 's@\\section{\(.*\)}@## \1@g'
replace 's@\\section\*{\(.*\)}@## \1@g'
replace 's@\\paragraph{\(.*\)}@### \1@g'
replace 's@\\label{\(.*\)}@@g'
replace 's@\\index{\(.*\)}@@g'
replace 's@\\iindex{\(.*\)}@\1@g'
replace 's@\\href{\([^}]*\)}{\([^}]*\)}@[\2](\1)@g'
replace 's@\\textit{\([^}]*\)}@_\1_@g'
replace 's@\\textbf{\([^}]*\)}@**\1**@g'
replace 's@\\myuline{\([^}]*\)}@<ins>\1</ins>@g'
replace 's@\\underline{\([^}]*\)}@<ins>\1</ins>@g'
replace 's@\\enquote{\([^}]*\)}@"\1"@g'
replace 's@\\texttt{\([^}]*\)}@\`\1\`@g'
replace 's@\\item @- @g'
replace '/^\(.*\)\\begin{itemize}$/d'
replace '/^\(.*\)\\end{itemize}$/d'
}
function main() {
if [[ ! -f "${acronyms}" ]] ; then
echo "ERROR: ${acronyms} not found" >&2
return 1
fi
if [[ ! -f "${source}" ]] ; then
source="${source}.tex"
if [[ ! -f "${source}" ]] ; then
echo "ERROR: ${source} not found" >&2
return 1
fi
fi
if ! command -v gsed ; then
echo "ERROR: GNU Sed not installed, please use 'brew install gnu-sed'"
return 1
fi
if ! command -v ggrep ; then
echo "ERROR: GNU Find not installed, please use 'brew install grep'"
return 1
fi
cp -v "${source}" "${target}"
replaceStuff
return $?
}
main $@
exit $?