-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex_gen.e
353 lines (273 loc) · 8.36 KB
/
latex_gen.e
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
include std/console.e
include std/search.e
include std/search.e
include std/sequence.e
include std/text.e
include std/types.e
include common_gen.e
constant sectioning = {
"part",
"chapter",
"section",
"subsection",
"subsubsection",
"par\\textbf"
}
constant replacements = {
'\\', "{\\textbackslash}",
'$', "{\\textdollar}",
'_', "{\\textunderscore}",
'^', "{\\textasciicircum}",
'<', "{\\textless}",
'>', "{\\textgreater}",
$
}
sequence table_cells = {}, table_rows = {}
-- Path names are typically long and cannot be separated by LaTeX by
-- default. There is the \path{} command which will do intelligent
-- splitting of the path when the path runs off the side of the page
-- but we must know to do this. So, this function just takes a wild
-- guess as to if a given sequence is possibly a path.
function is_probably_path(sequence s)
if length(s) = 0 then
return 0
end if
for i = 1 to length(s) do
integer ch = s[i]
if find(ch, "/\\:.") = 0 and not t_alnum(ch) then
return 0
end if
end for
return 1
end function
function escape(sequence val)
integer i = 1
while i <= length(val) do
if find(val[i], "#&%") then
val = insert(val, '\\', i)
i += 1
else
integer tmp = find(val[i], replacements)
if tmp then
val = replace(val, replacements[tmp+1], i)
i += length(replacements[tmp+1]) - 1
end if
end if
i += 1
end while
return val
end function
function generator(integer action, sequence params, object context)
sequence doc_text = ""
switch action do
case SanitizeURL then
doc_text = params[1]
case Bookmark then
doc_text = sprintf("\\label{%s}\n", { params })
case InternalLink then
doc_text = escape(params[2]) & " (\\ref{" & params[1] & "})"
case QualifiedLink then
doc_text = "\\hyperref[" & params[2] & "]{" & escape(params[3]) & "}"
case InterWikiLink then
doc_text = "InterWikiLink % InterWikiLink " & params[2] & "\n"
case NormalLink then
doc_text = "\\href{" & params[1] & "}{" & escape(params[2]) & "}"
case InternalImage then
case InterWikiImage then
case NormalImage then
case Paragraph, Division then
-- We want the ability to call trim and make sure we are going to be
-- outputting data before outputting an empty paragraph. Creole sends
-- a {-1} for the Paragraph action to see what the beginning and ending
-- of the paragraph is going to be. Thus we have this special case.
if equal(params, {-1}) then
doc_text = "\\par " & params & "\n"
else
params = trim(params)
if length(params) then
doc_text = sprintf("\\par %s\n", { params })
end if
end if
case OrderedList then
doc_text = "\\begin{enumerate}\n" & params & "\\end{enumerate}\n"
case UnorderedList then
doc_text = "\\begin{itemize}\n" & params & "\\end{itemize}\n"
case ListItem then
doc_text = "\\item " & params & "\n"
case Heading then
doc_text = sprintf("\\%s{%s}\n", { sectioning[params[1]], escape(params[2]) })
case ItalicText then
doc_text = sprintf("\\textit{%s}", { params[1] })
case BoldText then
doc_text = sprintf("\\textbf{%s}", { params[1] })
case MonoText then
if is_probably_path(params[1]) then
doc_text = "\\texttt{\\path{" & params[1] & "}}"
else
doc_text = "\\texttt{" & params[1] & "}"
end if
case UnderlineText then
doc_text = sprintf("\\uline{%s}", { params[1] })
case Superscript then
doc_text = sprintf("\\textsuperscript{%s}", { params[1] })
case Subscript then
doc_text = sprintf("\\textsubscript{%s}", { params[1] })
case StrikeText then
doc_text = sprintf("\\sout{%s}", { params[1] })
case InsertText then
doc_text = sprintf("\\uwave{%s}", { params[1] })
case ColorText then
doc_text = params[2]
case CodeExample then
sequence numbers = "none"
if length(find_all('\n', params[1])) > 4 then
numbers = "left"
end if
doc_text = sprintf("\\lstset{language=Euphoria,numbers=%s}" &
"\\begin{lstlisting}\n" &
"%s\n" &
"\\end{lstlisting}\n",
{
numbers,
params[1]
})
case TableDef then
sequence def = ""
if length(table_rows) then
def = repeat_pattern("X|", length(table_rows[1]))
end if
doc_text = "\n\\begin{tabularx}{\\linewidth}{|" & def &
"}\n\\hline\n" & params[1] & "\n\\end{tabularx}\n\n"
table_rows = {}
case HeaderRow then
doc_text = trim(params[1][1..$ - 2]) & " \\\\\\hline\n"
table_rows = append(table_rows, table_cells)
table_cells = {}
case HeaderCell then
doc_text = "\\textbf{" & trim(params[1]) & "} & "
table_cells = append(table_cells, params[1])
case NormalRow then
doc_text = trim(params[1][1..$ - 2]) & " \\\\ \\hline\n"
table_rows = append(table_rows, table_cells)
table_cells = {}
case NormalCell then
doc_text = trim(params[1]) & " & "
table_cells = append(table_cells, params[1])
case NonBreakSpace then
doc_text = `\ `
case ForcedNewLine then
doc_text = " \\\\ \n"
case HorizontalLine then
doc_text = "\n\\hspace{5pt}\n\\hrule\n\\hspace{5pt}\n"
case NoWikiBlock then
doc_text = sprintf("\n\\lstset{language=,numbers=none,caption=}" &
"\\begin{lstlisting}\n" &
"%s\n" &
"\\end{lstlisting}\n",
{
params[1]
})
case NoWikiInline then
doc_text = "\\texttt{" & params[1] & "}"
case HostID then
doc_text = ""
case OptReparseHeadings then
doc_text = ""
case DefinitionList then
doc_text = "\n\\begin{description}"
for i = 1 to length(params) do
doc_text &= "\\item[" & params[i][1] & "] \\hfill \\\\\n" &
params[i][2] & "\n"
end for
doc_text &= "\\end{description}\n"
case BeginIndent then
case EndIndent then
case PassThru then
case Sanitize then
if params[1] then
-- We are sanitizing a eudoc, don't do it
doc_text = params[2]
else
doc_text = escape(params[2])
end if
case CamelCase then
case Plugin then
case Document then
-- Default action is to ust pass back the document text untouched.
doc_text = params[1]
case ContextChange then
case Comment then
doc_text = sprintf("%% %s\n", { params })
case Quoted then
doc_text = "\\begin{quotation}\n\\hrule\\vspace*{6pt}\n" & trim(params[2]) &
" \\textemdash \\textit{" & trim(params[1]) & "} " &
"\\vspace*{6pt}\\hrule\n\\end{quotation}\n"
case else
doc_text = sprintf("[BAD ACTION CODE %d]", action)
for i = 1 to length(params) do
doc_text &= params[i]
doc_text &= " "
end for
end switch
return doc_text
end function
function default_template(sequence title, sequence context, sequence body)
return `
\documentclass[openany]{book}
\usepackage{fixltx2e}
\usepackage{tabularx}
\usepackage{listings}
\usepackage{color}
\usepackage{ulem}
\usepackage[pagebackref=true,colorlinks]{hyperref}
\usepackage[all]{hypcap}
\usepackage[Lenny]{fncychap}
\usepackage{fancyhdr}
\usepackage{vmargin}
%\usepackage[titles]{tocloft}
\usepackage{graphicx}
\usepackage{fix-cm}
\setpapersize{USletter}
\setmarginsrb{0.75in}{0.5in}{0.75in}{0.5in}{10mm}{5mm}{20mm}{10mm}
\pagestyle{empty}
\fancyhf{}
\fancyhead[RO,RE]{\slshape \rightmark}
\fancyhead[LO,LE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.6pt}
\renewcommand{\footrulewidth}{0.4pt}
% Set the default font to serif
\renewcommand{\familydefault}{\sfdefault}
\definecolor{listinggray}{gray}{0.99}
\definecolor{stringgray}{rgb}{0.2,0.2,1.0}
\definecolor{keyword}{rgb}{0.05,0.35,0.05}
\lstset{%
xleftmargin=10pt,xrightmargin=10pt,%
showspaces=false,%
showtabs=false,%
showstringspaces=false,%
tabsize=4,%
backgroundcolor=\color{listinggray},%
rulecolor=\color{black},%
basicstyle=\ttfamily\small,%
frame=single,%
captionpos=b,%
stringstyle=\ttfamily\color{stringgray},%
keywordstyle=\color{keyword},%
numberstyle=\tiny%
}
\begin{document}
\frontmatter
\title{` & title & `}
\author{OpenEuphoria Group}
\maketitle
\setcounter{tocdepth}{1}
\tableofcontents
\pagestyle{fancy}
\mainmatter
` & body & `
\end{document}
`
end function
common_gen:register("LaTeX", "tex", routine_id("generator"),
routine_id("default_template"))