Class: Luaof::LaTeXRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/luaof/latex_renderer.rb

Constant Summary collapse

SECTION_LEVEL_TO_CONTROL_SEQUENCE =
[nil, 'chapter', 'section', 'subsection', 'subsubsection'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(output, labels, lang) ⇒ LaTeXRenderer



9
10
11
12
13
# File 'lib/luaof/latex_renderer.rb', line 9

def initialize(output, labels, lang)
  @output = output
  @labels = labels
  @lang = lang
end

Instance Method Details

#escape(target) ⇒ Object



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
# File 'lib/luaof/latex_renderer.rb', line 336

def escape(target)
  escaped = +''
  target.each_char do |char|
    escaped << case char
               when '_'
                 '\\_{}'
               when '^'
                 '\\textasciicircum{}'
               when '&'
                 '\\&'
               when '#'
                 '\\#'
               when '%'
                 '\\%'
               when '$'
                 '\\${}'
               when '\\'
                 '\\textbackslash{}'
               when '~'
                 '\\textasciitilde{}'
               else
                 char
               end
  end
  escaped
end

#linktext(node) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/luaof/latex_renderer.rb', line 414

def linktext(node)
  case node
  in label: { tag: :section, title: [String => title] }
    title
  in label: { tag: :APIEntry, param: }
    name = signaturetext(param)
    name.match(/ (lua L? [\w\\{}]+) [)]? [ ]+ [(] /x) or
      name.match(/ (lua L? [\w\\{}]+) /x)
    name = ::Regexp.last_match(1) or raise [name, node].inspect[..500]
    "\\texttt{#{name}}"
  else
    raise node.inspect[..500]
  end
end


429
430
431
432
433
434
435
436
# File 'lib/luaof/latex_renderer.rb', line 429

def print(target)
  target.is_a?(Symbol) and target = target.to_s
  case target
  in String
    target.scrub!
    @output.print(target)
  end
end

#render(document) ⇒ Object



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
# File 'lib/luaof/latex_renderer.rb', line 15

def render(document)
  case document
  in Array
    document.each { |child| render(child) }
  in Node[label: { tag: :ANSI }, children:]
    case @lang
    in :ja
      print('ISO~Cの関数\\texttt{')
    end
    render(children)
    print('}')
  in Node[label: { tag: :manual }, children:]
    children.each { |child| render(child) }

  # EBNF
  in Node[label: { tag: :Produc }, children:]
    print("\\begin{align*}\n")
    render_ebnf(children)
    print('\\end{align*}')
  in Node[label: { tag: :bnfNter |
                   :bnfrep |
                   :bnfopt |
                   :bnfter }]
    print('\\(')
    render_ebnf(document)
    print('\\)')

  in Node[label: { tag: :section, level:, title: } => label, children:]
    control_sequence = SECTION_LEVEL_TO_CONTROL_SEQUENCE[level] or raise
    print("\\#{control_sequence}{")
    render(title)
    print('}')
    id = label&.dig(:id) and print("\\label{#{id}}")
    print("\n\n")
    render(children)
  in Node[label: { tag: :simplesect }, children:]
    render(children)
  in Node[label: { tag: :verbatim }, children:]
    print("\n\n\\vskip.5\\baselineskip\\noindent\\fcolorbox{gray!30}{gray!10}{\\parbox{.95\\textwidth}{\\texttt{")
    render_verbatim_content(children)
    print("}}}\\hfill\\vskip.5\\baselineskip\n\n")
  in Node[label: { tag: :emphx | :emph | :def | :rep }, children:]
    print('\\emph{')
    render(children)
    print('}')
  in Node[label: { tag: :id | :T | :idx }, children:]
    print('\\texttt{')
    render(children)
    print('}')
  in Node[label: { tag: :Q }, children:]
    print('')
    render(children)
    print('')

  in Node[label: { tag: :x }, children: [String => index]]
    render(index)
    print('\\index{')
    render(index)
    print('}')
  in Node[label: { tag: :x }, children: [String => index, :linebreak, String => index2] => content]
    render(content)
    print('\\index{')
    render(index)
    print(' ')
    render(index2)
    print('}')

  in Node[label: { tag: :M | :num }, children:]
    print('\\(')
    render(children)
    print('\\)')
  in Node[label: { tag: :sp }, children:]
    print('^{')
    render(children)
    print('}')
  in Node[label: { tag: :description }, children:]
    print("\\begin{description}\n")
    render(children)
    print("\\end{description}\n")
  in Node[label: { tag: :itemize }, children:]
    print("\\begin{itemize}\n")
    render(children)
    print("\\end{itemize}\n")
  in Node[label: { tag: :item, param: }, children:]
    print('\\item[')
    render(param)
    print(']')
    render(children)
  in Node[label: { tag: :item }, children:]
    print('\\item{}')
    render(children)
  in Node[label: { tag: :defid }, children: [String => id]]
    print('\\texttt{')
    render(id)
    print("}\\label{#{id}}")
  in Node[label: { tag: :see, param: { label: { id: String => id } } => param }]
    id or raise
    print("(「#{linktext(param)}」\\pageref{#{id}}ページ)")
  in Node[label: { tag: :seeF }, children: [String => id]]
    case @lang
    in :ja
      print('(関数\\texttt{')
      render(id)
      print("}は\\pageref{#{id}}ページ)")
    end
  in Node[label: { tag: :seeC }, children: [String => id]]
    case @lang
    in :ja
      print('(C関数\\texttt{')
      render(id)
      print("}は\\pageref{#{id}}ページ)")
    end
  in Node[label: { tag: :refsec }, children: [String => id]]
    id = "sec:#{id}"
    node = @labels[id] or raise "section reference #{id} not found"
    case @lang
    in :ja
      print("\\ref{#{id}}節「#{linktext(node)}」(\\pageref{#{id}}ページ)")
    end
  in Node[label: { tag: :See, param: { label: { id: } } => param }]
    id or raise
    case @lang
    in :ja
      print("#{linktext(param)}(\\pageref{#{id}}ページ)")
    end
  in Node[label: { tag: :link, param: [String => id] }, children:]
    @labels["sec:#{id}"] and id = "sec:#{id}"
    render(children)
    case @lang
    in :ja
      print("(\\pageref{#{id}}ページ)")
    end
  in Node[label: { tag: :Lid }, children: [String => id]]
    print('\\texttt{')
    render(id)
    case @lang
    in :ja
      print("}(\\pageref{#{id}}ページ)")
    end

  in Node[label: { tag: :index }, children: [String => index]]
    print('\\index{')
    render(index)
    print('}')
  in Node[label: { tag: :index }, children: [String => index, :linebreak, String => index2]]
    print('\\index{')
    render(index)
    print(' ')
    render(index2)
    print('}')

  in Node[label: { tag: :Char }, children:]
    print('`\\texttt{')
    render(children)
    print("}'")
  in Node[label: { tag: :St }, children:]
    print('``\\texttt{')
    render(children)
    print("}''")
  in Node[label: { tag: :rw | :Rw }, children:]
    print('\\textbf{')
    render(children)
    print('}')

  in Node[label: { tag: :apii, pop: String => pop, push: String => push, error: String => error }]
    print('\\dotfill\\(\\begin{smallmatrix}\\texttt{')
    render(pop)
    print('}\\\\\\texttt{')
    render(push)
    print('}\\\\\\texttt{')
    render(error)
    print("}\\end{smallmatrix}\\)\n\n")

  in Node[label: { tag: :APIEntry, param:, indicator: { pop:, push:, error: } }, children:]
    signature = signaturetext(param)
    signature.match(/ (lua L? [\w\\{}]+) [)]? [ ]+ [(] /x) or
      signature.match(/ (lua L? [\w\\{}]+) /x)
    name = ::Regexp.last_match(1) or raise signature.inspect[..500]
    id = name.gsub('\\_{}', '_')
    print("\n\n\\vskip.5\\baselineskip\\noindent\\fcolorbox{gray!30}{gray!10}{\\parbox{.9\\textwidth}{\\texttt{")
    render_verbatim_content(param)
    print("}}}\\label{#{id}}\\index{\\texttt{#{name}}}\\dotfill\\(\\begin{smallmatrix}\\texttt{")
    render(pop)
    print('}\\\\\\texttt{')
    render(push)
    print('}\\\\\\texttt{')
    render(error)
    print("}\\end{smallmatrix}\\)\\vskip.5\\baselineskip\n\n")
    render(children)
  in Node[label: { tag: :APIEntry, param: }, children:]
    signature = signaturetext(param)
    signature.match(/ (lua L? [\w\\{}]+) [)]? [ ]+ [(] /x) or
      signature.match(/ (lua L? [\w\\{}]+) /x)
    name = ::Regexp.last_match(1) or raise signature.inspect[..500]
    id = name.gsub('\\_{}', '_')
    print("\n\n\\vskip.5\\baselineskip\\noindent\\fcolorbox{gray!30}{gray!10}{\\parbox{.9\\textwidth}{\\texttt{")
    render_verbatim_content(param)
    print("}}}\\label{#{id}}\\index{\\texttt{#{name}}}\\hfill\\vskip.5\\baselineskip\n\n")
    render(children)
  in Node[label: { tag: :LibEntry, param: }, children:]
    signature = signaturetext(param)
    name = signature.sub(/ [ ] [(] .* /x, '')
    id = name.gsub('\\_{}', '_')
    print("\n\n\\vskip.5\\baselineskip\\noindent\\hfill\\fcolorbox{gray!30}{gray!10}{\\parbox{.95\\textwidth}{\\texttt{")
    render_verbatim_content(param)
    print("}}}\\label{#{id}}\\index{\\texttt{#{name}}}\\hfill\\vskip.5\\baselineskip\n\n")
    render(children)

  in Node[label: { tag: :N }, children:]
    render(children)
  in Node[label: { tag: :At }, children: []]
    print('@')
  in Node[label: { tag: :En }, children: []]
    print('--')
  in Node[label: { tag: :CId }, children: ]
    case @lang
    in :ja
      print('Cの関数')
    end
    render(children)
  in { pipe: }
    render(pipe)
  in { label: :leftbrace, children: }
    print('{')
    render(children)
    print('}')
  in String
    print(escape(document))
  in :linebreak
    print("\n")
  in :emptyline
    print("\n\n")
  in keyword: :Cdots
    print('\\dots{}')
  in keyword: :En
    print('--')
  in keyword: :VerBar
    print('\\textbar{}')
  in keyword: :leq
    print('\\(\\leq\\)')
  in keyword: :At
    print('@')
  in keyword: :pi
    print('\\pi{}')
  in keyword: :log
    print('\\log{}')
  in keyword: :cpp
    print('\\cpluspluslogo{}')
  in keyword: :nil | :false | :true | :fail => name
    print('\\textbf{')
    render(name)
    print('}')
  in Symbol => name
    print(name)
  else
    raise document.inspect[..500]
  end
end

#render_ebnf(target) ⇒ Object



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
# File 'lib/luaof/latex_renderer.rb', line 274

def render_ebnf(target)
  case target
  in Array
    target.each do |child|
      render_ebnf(child)
    end
  in Node[label: { tag: :producname }, children:]
    render_ebnf(children)
  in keyword: :Or
    print(' \\quad\\text{\\textbar{}} ')
  in keyword: :OrNL
    print("\\\\\n\\text{\\textbar{}} &")
  in Node[label: { tag: :producbody }, children:]
    print(' \\rightarrow{} &')
    render_ebnf(children)
    print("\\\\\n")

  in keyword: :Open
    print('\\text{\\{}')
  in keyword: :Close
    print('\\text{\\}}')
  in :emptyline
  # nop
  in { pipe: }
    print(pipe.gsub('|', '\\text{\\textbar{}}'))
  in String
    print("\\text{#{escape(target)}}")
  in Node[label: { tag: :bnfNter }, children:]
    render_ebnf(children)
  in Node[label: { tag: :bnfter }, children:]
    print('\\text{\\textquoteleft{}\\texttt{')
    render_ebnf(children)
    print('}\\textquoteright{}}')
  in Node[label: { tag: :bnfrep }, children:]
    print('\\left\\{')
    render_ebnf(children)
    print('\\right\\}')
  in Node[label: { tag: :bnfopt }, children:]
    print('\\left[')
    render_ebnf(children)
    print('\\right]')

  in Node[label: { tag: :rep }, children:]
    print(' \\text{\\emph{')
    render_ebnf(children)
    print('}} ')
  in Node[label: { tag: :rw | :Rw }, children:]
    print(' \\text{\\textbf{')
    render_ebnf(children)
    print('}} ')

  in Node[label: :leftbrace, children:]
    print('\\text{\\{}')
    render_ebnf(children)
    print('\\text{\\}}')
  in :linebreak
  # nop
  else
    raise target.inspect[..500]
  end
end

#render_verbatim_content(target) ⇒ Object



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
# File 'lib/luaof/latex_renderer.rb', line 363

def render_verbatim_content(target)
  case target
  in Array
    target.each { |child| render_verbatim_content(child) }
  in String
    escaped = +''
    target.each_char { |char| escaped << (char == ' ' ? '\\ ' : escape(char)) }
    print(escaped)
  in keyword: :Cdots
    print('\\dots{}')
  in Node[label: :leftbrace, children:]
    print('\\{')
    render_verbatim_content(children)
    print('\\}')
  in Node[label: { tag: :rep }, children:]
    print('\\emph{')
    render_verbatim_content(children)
    print('}')
  in :linebreak
    print("\\\\\n")
  in :emptyline
    print("\\\\\n\\\\\n")
  in keyword: :ldots
    print('\\dots{}')
  else
    raise target.inspect[..500]
  end
end

#signaturetext(node) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/luaof/latex_renderer.rb', line 392

def signaturetext(node)
  case node
  in Array
    node.map { |n| signaturetext(n) }.join
  in String
    escape(node)
  in :linebreak
    "\n"
  in { keyword: :ldots }
    '\\ldots{}'
  in { keyword: :Cdots }
    '\\dots{}'
  in Node[label: :leftbrace, children:]
    content = children.map { |c| signaturetext(c) }.join
    "{#{content}}"
  in Node[label: { tag: :rep }, children:]
    "\\emph{#{signaturetext(children)}}"
  else
    raise node.inspect[..500]
  end
end