-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconvertfile
More file actions
executable file
·170 lines (126 loc) · 7.42 KB
/
convertfile
File metadata and controls
executable file
·170 lines (126 loc) · 7.42 KB
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/sh
# NOTE - the order of operations, especially for the first and last items, really matter
SOURCE=$1
DEST=$2
if [$DEST = ""]; then
DEST=$SOURCE.tex
SOURCE=$SOURCE.xml
fi
cp $SOURCE $DEST
perl -pi -e 's!\\!\\\\!gsi' $DEST
perl -pi -e 's!_!\\_!gsi' $DEST
perl -pi -e 's!\$!\\\$!gsi' $DEST
perl -pi -e 's!\#!\\\#!gsi' $DEST
perl -pi -e 's!\^!\\textasciicircum!gsi' $DEST
perl -pi -e 's!<!<!gsi' $DEST
perl -pi -e 's!>!>!gsi' $DEST
# Convert all &eax; to {\eax}
REGS=("rax ax ah al eax ebx ecx edx edi esp ebp esi eip cl dl eflags")
for REG in $REGS
do
perl -pi -e "s/&${REG};/{\\\\${REG}Reg}/g;" $DEST
perl -pi -e "s/&${REG}-indexed;/{\\\\${REG}RegIdx}/g;" $DEST
perl -pi -e "s/%${REG}/{\\\\${REG}Bare}/g;" $DEST
done
perl -pi -e 's!%!\\%!gsi' $DEST
perl -pi -e 's!&percent;!\\%!gsi' $DEST
# Strip comments
perl -pi -e 'BEGIN{undef $/;}s/<!--(.*?)-->/$1 =~ s!^(.*)$!% $1!grm . "\n" /gesi' $DEST
# Convert Code to use BP Code Listing
perl -pi -e 'BEGIN{undef $/;} s!<programlisting>\s*&(.*?);\s*</programlisting>!\\begin{simpletyping}\n\\lstinputlisting{$1}\n\\end{simpletyping}\n!gsi' $DEST
perl -pi -e 's!\\lstinputlisting{([\w-]+)-s}!"\\lstinputlisting{" . $1 . ".s}"!gesi' $DEST
perl -pi -e 'BEGIN{undef $/;}s!<programlisting>(.*?)</programlisting>!\\begin{simpletyping}\n\\begin{lstlisting}$1\\end{lstlisting}\n\\end{simpletyping}\n!gsi' $DEST
# Convert chapter
perl -pi -e "BEGIN{undef \$/;} s/<chapter\\s*id=\"([^\"]*?)\">\\s*<title>(.*?)<\\/title>/\\\\chapter{\$2}\n\\\\label{\$1}\n/gsi" $DEST
perl -pi -e "BEGIN{undef \$/;} s/<chapter>\s*<title>(.*?)<\\/title>/\\\\chapter{\$1}\n/gsi" $DEST
perl -pi -e 's!</chapter>!!' $DEST
# convert <index>
perl -pi -e 'BEGIN{undef $/;} s!<indexterm.*?>\s*<primary>\s*(.*?)\s*</primary>\s*</indexterm>!\\index{$1}!gsi' $DEST
# Convert appendix
perl -pi -e "BEGIN{undef \$/;} s/<appendix\s*id=\"([^\"]*?)\">\s*<title>(.*?)<\\/title>/\\\\chapter{\$2}\n\\\\label{\$1}\n/gsi" $DEST
perl -pi -e "BEGIN{undef \$/;} s/<appendix>\s*<title>(.*?)<\\/title>/\\\\chapter{\$1}\n/gsi" $DEST
perl -pi -e 's!</appendix>!!' $DEST
# Convert simplesect
perl -pi -e "BEGIN{undef \$/;} s/<simplesect id=\"([^\"]*?)\">\s*<title>(.*?)<\\/title>/\\\\section{\$2}\n\\\\label{\$1}\n/gsi" $DEST
perl -pi -e "BEGIN{undef \$/;} s/<simplesect>\s*<title>(.*?)<\\/title>/\\\\section{\$1}\n/gsi" $DEST
perl -pi -e 's!</simplesect>!!gsi' $DEST
# Convert sect1
perl -pi -e "BEGIN{undef \$/;} s/<sect1\s*id=\"([^\"]*?)\">\s*<title>(.*?)<\\/title>/\\\\section{\$2}\n\\\\label{\$1}\n/gsi" $DEST
perl -pi -e "BEGIN{undef \$/;} s/<sect1>\s*<title>(.*?)<\\/title>/\\\\section{\$1}\n/gsi" $DEST
perl -pi -e 's!</sect1>!!gsi' $DEST
# Convert xref linkend
perl -pi -e 's!<xref linkend="(.*?)" />!\\autoref{$1}!gsi;' $DEST
# Convert sect2
perl -pi -e "BEGIN{undef \$/;} s/<sect2 id=[\"'](.*?)[\"']>\s*<title>(.*?)<\\/title>/\\\\section{\$2}\n\\\\label{\$1}\n/gsi" $DEST
perl -pi -e "BEGIN{undef \$/;} s/<sect2>\s*<title>(.*?)<\\/title>/\\\\section{\$1}\n/gsi" $DEST
perl -pi -e 's!</sect2>!!gsi' $DEST
# Paragraphs with ids
# <formalpara>
# <itemizedlist mark="opencircle">
# <trademark class="registered">stuff</trademark>
perl -pi -e 's!<trademark\s*class="registered">(.*?)</trademark>!$1\\textregistered!gsi' $DEST
# <note> with <title>
perl -pi -e 'BEGIN{undef $/;} s!<note>\s*<title>\s*(.*?)</title>\s*<para>\s*(.*?)\s*</para>\s*</note>!\\begin{sidebar}[$1]\n$2\n\\end{sidebar}!gsi;' $DEST
# <warning> with <title>
perl -pi -e 'BEGIN{undef $/;} s!<warning>\s*<title>\s*(.*?)</title>\s*<para>\s*(.*?)\s*</para>\s*</warning>!\\begin{sidebar}[$1]\n$2\n\\end{sidebar}!gsi;' $DEST
# Warning no title
perl -pi -e 'BEGIN{undef $/;} s!<warning>\s*<para>\s*(.*?)\s*</para>\s*</warning>!\\begin{sidebar}[Warning]\n$1\n\\end{sidebar}!gsi;' $DEST
# Example
perl -pi -e 'BEGIN{undef $/;} s!<example>\s*<title>(.*?)</title>(.*?)</example>!\\begin{figure}\n$2\n\\caption{$1}\n\\end{figure}!gsi' $DEST
# <mediaobject>/imageobject/imagedata fileref format/caption
perl -pi -e 'BEGIN{undef $/;} s!<mediaobject>\s*<imageobject>\s*<imagedata\s*fileref="(.*?)"\s*format="PNG"\s*/>\s*</imageobject>\s*<caption>\s*<para>\s*<emphasis>\s*(.*?)\s*</emphasis>\s*</para>\s*</caption>\s*</mediaobject>!\\begin{figure}\n\\caption{$2}\n\\includegraphics[width=\\textwidth]{$1}\n\\end{figure}!gsi' $DEST
#perl -pi -e 'BEGIN{undef $/;} s!<mediaobject>\s*<imageobject>(.*?)</imageobject>\s*<caption>\s*<para>\s*<emphasis>\s*(.*?)\s*</mediaobject>!MYPNG!gsi' $DEST
# <screen>
perl -pi -e 'BEGIN{undef $/;} s!<screen>(.*?)</screen>!\\begin{simpletyping}\n\\begin{lstlisting}$1\\end{lstlisting}\n\\end{simpletyping}!gsi' $DEST
# <quote>
perl -pi -e "BEGIN{undef $/;} s!<quote>(.*?)</quote>!\`\`\$1''!gsi;" $DEST
# Convert footnote
perl -pi -e 'BEGIN{undef $/;} s!<footnote>\s*<para>\s*(.*?)</para>\s*</footnote>!\\footnote{$1}!gsi' $DEST
perl -pi -e 'BEGIN{undef $/;} s!<ulink.*?>(.*?)</ulink>!\\url{$1}!gsi' $DEST
# Conver literal
perl -pi -e 'BEGIN{undef $/;} s!<literal>(.*?)</literal>!\\icode{$1}!gsi;' $DEST
# Convert command
perl -pi -e 'BEGIN{undef $/;} s!<command>(.*?)</command>!\\icodecmd{$1}!gsi;' $DEST
# Convert filename
perl -pi -e 'BEGIN{undef $/;} s!<filename>(.*?)</filename>!\\icodefilename{$1}!gsi;' $DEST
# Convert citetitle
perl -pi -e 'BEGIN{undef $/;} s!<citetitle>(.*?)</citetitle>!\\documentname{$1}!gsi;' $DEST
# Convert productname
perl -pi -e 'BEGIN{undef $/;} s!<productname>(.*?)</productname>!\\documentname{$1}!gsi;' $DEST
# <variablelist>
perl -pi -e 's!<variablelist>\s*!\\begin{description}!gsi' $DEST
perl -pi -e 's!</variablelist>!\\end{description}!gsi' $DEST
perl -pi -e 'BEGIN{undef $/;} s!<varlistentry>\s*<term>(.*?)</term>\s*<listitem>\s*<para>\s*(.*?)\s*</para>\s*</listitem>\s*</varlistentry>\s*!\\item[$1] $2\n!gsi' $DEST
# <orderedlist>
perl -pi -e 's!<orderedlist>\s*!\\begin{enumerate}!gsi' $DEST
perl -pi -e 's!</orderedlist>!\\end{enumerate}!gsi' $DEST
# <itemizedlist>
perl -pi -e 's!<itemizedlist.*?>\s*!\\begin{itemize}!gsi' $DEST
perl -pi -e 's!</itemizedlist>!\\end{itemize}!gsi' $DEST
# List items
perl -pi -e 'BEGIN{undef $/;} s!<listitem>\s*<(?:formal)?para>\s*(.*?)\s*</(?:formal)?para>\s*</listitem>\s*!\\item $1 \n!gsi' $DEST
# Convert emphasis
perl -pi -e 'BEGIN{undef $/;} s!<emphasis>(.*?)</emphasis>!\\emph{$1}!gsi' $DEST
# blockquote
perl -pi -e 'BEGIN{undef $/;} s!<blockquote>(.*?)</blockquote>!\\begin{quote}$1\\end{quote}!gsi;' $DEST
# wordasword
perl -pi -e 's!<wordasword>(.*?)</wordasword>!\\icode{$1}!gsi' $DEST
# Tables
perl -pi -e 'BEGIN{undef $/;} s!<table>\s*<title>(.*?)</title>.*?<(?:thead|tbody)>(.*?)\s*</table>!\\begin{table}[h]\n\\begin{tabular}{FIXME}\n$2\n\\end{tabular}\n\\caption{$1}\n\\end{table}\n!gsi' $DEST
perl -pi -e 's!<thead>\s*!!gsi' $DEST
perl -pi -e 's!</thead>\s*!!gsi' $DEST
perl -pi -e 's!<tbody>\s*!!gsi' $DEST
perl -pi -e 's!</tbody>\s*!!gsi' $DEST
perl -pi -e 's!<tgroup.*?>\s*!!gsi' $DEST
perl -pi -e 's!</tgroup>\s*!!gsi' $DEST
perl -pi -e 's!<colspec.*? />\s*!!gsi' $DEST
perl -pi -e 'BEGIN{undef $/;}s!\s*<entry.*?>\s*(.*?)\s*</entry>\s*!$1 & !gsi' $DEST
perl -pi -e 'BEGIN{undef $/;}s!<row>\s*(.*?)</row>\s*!$1\n!gsi' $DEST
# NOTE - these are titles left over after all other conversions - basically they need to be bold:
perl -pi -e 's!<title>(.*?)</title>!\\textbf{$1}!gsi' $DEST
# After everything else, convert paras
perl -pi -e 's!<para>!\n\n!gsi' $DEST
perl -pi -e 's!<para id="(.*)">!\n\n!gsi' $DEST
perl -pi -e 's!</para>!\n\n!gsi' $DEST
# Clean up excessive spacing after paragraph removal
perl -pi -e 'BEGIN{undef $/;} s!\n\n\n+!\n\n!gsi' $DEST