/
hikidoc2tex.rb
407 lines (349 loc) · 8.25 KB
/
hikidoc2tex.rb
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# -*- coding: utf-8 -*-
require 'hikidoc'
class HikiDoc
class TexOutput
def initialize
@f = nil
end
def reset
@f = StringIO.new
@f.puts <<-'EOS'
\documentclass[a4paper]{jarticle}
\usepackage[dvips]{graphics}
\usepackage[dvips]{graphicx}
\usepackage{multirow}
\usepackage{enumerate}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{ascmac}
%\usepackage{times,latexsym,mathptm}
\usepackage{alltt}
\usepackage{framed,color}
\definecolor{shadecolor}{gray}{0.90}
\usepackage{ascmac}
\usepackage{listings}
\lstset{%
language={C},
basicstyle={\small},%
identifierstyle={\small},%
commentstyle={\small\itshape},%
keywordstyle={\small\bfseries},%
ndkeywordstyle={\small},%
stringstyle={\small\ttfamily},
frame={tb},
breaklines=true,
columns=[l]{fullflexible},%
numbers=left,%
xrightmargin=0zw,%
xleftmargin=3zw,%
numberstyle={\scriptsize},%
stepnumber=1,
numbersep=1zw,%
lineskip=-0.5ex%
}
%\numberwithin{equation}{section}
%\topmargin = 0mm
\oddsidemargin = 0mm
\headheight = 0mm
\marginparwidth = 0mm
\textwidth = 160mm
% newcommand \mathbf for mathbold (usage $\mathbf{x}$)
%\DeclareMathAlphabet{\mathbf}{OT1}{ptm}{b}{it}
% slanted Greek letter in math-mode
%\DeclareSymbolFont{letters}{OML}{ztmcm}{m}{it}
% use Computer-Modern tt-font for typewriterfont
%\renewcommand{\ttdefault}{cmtt}
\newdimen\sheetwidth \newdimen\sheetheight
\def\Afoursheet{\sheetwidth=210mm \sheetheight=297mm}
\def\AfourRsheet{\sheetwidth=297mm \sheetheight=210mm}
\def\Afivesheet{\sheetwidth=148mm \sheetheight=210mm}
\def\Bfoursheet{\sheetwidth=257mm \sheetheight=363mm}
\def\Bfivesheet{\sheetwidth=182mm \sheetheight=257mm}
\def\Lettersheet{\sheetwidth=8.5in \sheetheight=11in}
\def\sheetcenter{ \oddsidemargin=\sheetwidth
\advance\oddsidemargin-\textwidth \oddsidemargin=0.5\oddsidemargin
\advance\oddsidemargin-1in \evensidemargin=\oddsidemargin
\topmargin=\sheetheight \advance\topmargin-\textheight
\topmargin=0.5\topmargin \advance\topmargin-\headheight
\advance\topmargin-\headsep \advance\topmargin-1in }
\Afoursheet\textwidth=48zw\textheight=250mm\sheetcenter
% indent of paragraph
\setlength{\parindent}{1zw}
\usepackage{fancyhdr}
\pagestyle{fancy}
\begin{document}
EOS
end
def finish
@f.puts '\end{document}'
@f.string
end
def container(_for=nil)
case _for
when :paragraph
[]
else
""
end
end
#
# Procedures
#
def headline(level, title)
case level
when 1
@f.puts "\\part{#{title}}"
when 2
@f.puts "\\section{#{title}}"
when 3
@f.puts "\\subsection{#{title}}"
when 4
@f.puts "\\subsubsection{#{title}}"
else
@f.puts "\\paragraph{#{title}}"
end
end
def hrule
@f.puts '\hrule'
end
def list_begin(type)
case type
when ULIST
@f.puts '\begin{itemize}'
when OLIST
@f.puts '\begin{enumerate}'
end
end
def list_end(type)
case type
when ULIST
@f.puts '\end{itemize}'
when OLIST
@f.puts '\end{enumerate}'
end
end
def listitem_open
@f.print '\item '
end
def listitem_close
end
def listitem(item)
@f.puts item
end
def dlist_open
@f.puts '\begin{description}'
end
def dlist_close
@f.puts '\end{description}'
end
def dlist_item(dt, dd)
@f.puts "\\item #{dt}" unless dt.empty?
@f.puts "\\item #{dd}" unless dd.empty?
end
def table_open
@f.puts '\begin{tabular} \hline'
end
def table_close
@f.puts '\end{tabular}'
end
def table_record_open
end
def table_record_close
end
def table_head(item, rs, cs)
# TODO
end
def table_data(item, rs, cs)
# TODO
end
def tdattr(rs, cs)
buf = ""
buf << %Q( rowspan="#{rs}") if rs
buf << %Q( colspan="#{cs}") if cs
buf
end
private :tdattr
def blockquote_open
@f.puts '\begin{shadebox}'
@f.puts '\begin{quote}'
end
def blockquote_close
@f.puts '\end{quote}'
@f.puts '\end{shadebox}'
end
def block_preformatted(str, syntax)
if syntax
begin
source_code(str, syntax)
return
rescue NameError, RuntimeError
end
end
preformatted(str)
end
def source_code(str, syntax)
@f.puts "\\begin{lstlisting}[language=#{syntax}]"
@f.print str
@f.print '\end{lstlisting}'
end
def preformatted(str)
@f.print '\begin{screen}'
@f.print '\begin{verbatim}'
@f.print str
@f.puts '\end{verbatim}'
@f.print '\end{screen}'
end
def paragraph(lines)
@f.puts "#{lines.join("\n")}\\\\"
end
def block_plugin(str)
str
end
#
# Functions
#
def hyperlink(uri, title)
%Q(#{title} - #{uri})
end
def wiki_name(name)
hyperlink(name, text(name))
end
def image_hyperlink(uri, alt = nil)
alt ||= uri.split(/\//).last
str = "\\includegraphics{#{url}}"
str += "\\caption{#{alt}}" unless alt.empty?
end
def strong(item)
"\\bf{#{item}}"
end
def em(item)
"\\em{#{item}}"
end
def del(item)
# TODO
end
def text(str)
escape(str)
end
def inline_plugin(src)
escape(src)
end
#
# Utilities
#
ALLTT_META_CHAR = {
'%' => '\\%',
'{' => '\\{',
'}' => '\\}',
'\\' => '\\textbackslash{}',
}
META_CHAR = ALLTT_META_CHAR.merge({
'_' => '\\textunderscore{}',
'~' => '\\textasciitilde{}',
'#' => '\\#',
'&' => '\\&',
'$' => '\\$', #'
'<' => '$<$',
'>' => '$>$',
'LaTeX' => '\\LaTeX{}',
'TeX' => '\\TeX{}',
})
ALLTT_META_CHAR_RE = /[#{ALLTT_META_CHAR.keys.collect{|x| Regexp.escape(x)}}]/
META_CHAR_RE = /(?:#{META_CHAR.keys.collect{|x| Regexp.escape(x)}.join('|')})/
def alltt_meta_char_escape(str)
str.gsub(ALLTT_META_CHAR_RE) do |x|
ALLTT_META_CHAR[x]
end
end
def meta_char_escape(str)
str.gsub(META_CHAR_RE) do |x|
META_CHAR[x]
end
end
def escape(str)
meta_char_escape(str)
end
end
end
class HikiDoc2Tex < HikiDoc
def HikiDoc2Tex.to_tex(src, options = {})
new(TexOutput.new, options).compile(src)
end
ULIST_RE = /\A#{Regexp.union(ULIST)}+/
OLIST_RE = /\A#{Regexp.union(OLIST)}+/
def compile_blocks(src)
f = LineInput.new(StringIO.new(src))
while line = f.peek
case line
when COMMENT_RE
f.gets
when HEADER_RE
compile_header f.gets
when HRULE_RE
f.gets
compile_hrule
when ULIST_RE
compile_list f, ULIST
when OLIST_RE
compile_list f, OLIST
when DLIST_RE
compile_dlist f
when TABLE_RE
compile_table f
when BLOCKQUOTE_RE
compile_blockquote f
when INDENTED_PRE_RE
compile_indented_pre f
when BLOCK_PRE_OPEN_RE
compile_block_pre f
else
if /^$/ =~ line
f.gets
next
end
compile_paragraph f
end
end
end
def compile_list(f, list_type = nil)
typestack = []
level = 0
@output.list_begin(list_type)
f.while_match(LIST_RE) do |line|
new_level = line.slice(LIST_RE).size
item = strip(line.sub(LIST_RE, ""))
if new_level > level
(new_level - level).times do
typestack.push list_type
@output.listitem_open
end
@output.listitem compile_inline(item)
elsif new_level < level
(level - new_level).times do
@output.listitem_close
end
@output.listitem_close
@output.listitem_open
@output.listitem compile_inline(item)
elsif list_type == typestack.last
@output.listitem_close
@output.listitem_open
@output.listitem compile_inline(item)
else
@output.listitem_close
@output.listitem_open
@output.listitem compile_inline(item)
typestack.push list_type
end
level = new_level
skip_comments f
end
level.times do
@output.listitem_close
end
@output.list_end(list_type)
end
end
if __FILE__ == $0
puts HikiDoc2Tex.to_tex(ARGF.read(nil))
end