/
review-ext.rb
436 lines (378 loc) · 13.1 KB
/
review-ext.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# -*- coding: utf-8 -*-
##
## Re:VIEWを拡張し、インライン命令とブロック命令を追加する
##
require_relative './lib/hooks/monkeypatch' ## 諸々の修正(モンキーパッチ)
module ReVIEW
## インライン命令「@<clearpage>{}」を宣言
Compiler.definline :clearpage ## 改ページ
Compiler.definline :nop ## 引数をそのまま表示 (No Operation)
Compiler.definline :letitgo ## (nopのエイリアス名)
Compiler.definline :B ## @<strong>{}のショートカット
Compiler.definline :foldhere ## 折り返し箇所を手動で指定
Compiler.definline :hearts ## ハートマーク
Compiler.definline :TeX ## TeX のロゴマーク
Compiler.definline :LaTeX ## LaTeX のロゴマーク
Compiler.definline :cursor ## ターミナルでのカーソル
## ブロック命令「//textleft{ ... //}」等を宣言
## (ここでは第2引数が「0」なので、引数なしのブロック命令になる。
## もし第2引数が「1..3」なら、//listのように必須引数が1つで
## 非必須引数が2という意味になる。)
Compiler.defblock :textleft, 0 ## 左寄せ
Compiler.defblock :textright, 0 ## 右寄せ
Compiler.defblock :textcenter, 0 ## 中央揃え
Compiler.defblock :abstract, 0 ## 章の概要
Compiler.defblock :makechaptitlepage, 0..1, true ## 章タイトルを独立したページに
#
Compiler.defblock :list, 0..3 ## (上書き)
Compiler.defblock :listnum, 0..3 ## (上書き)
#
Compiler.defsingle :clearpage, 0 ## 改ページ (\clearpage)
Compiler.defsingle :resultbegin, 0 ## (出力結果開始部、Starterドキュメントで使用)
Compiler.defsingle :resultend, 0 ## (出力結果終了部、Starterドキュメントで使用)
## LaTeX用の定義
class LATEXBuilder
## 改ページ(インライン命令)
def inline_clearpage(str)
'\clearpage'
end
## 改ページ(ブロック命令)
def clearpage()
puts ''
puts '\\clearpage'
puts ''
end
## 引数をそのまま表示
## 例:
## //emlist{
## @<b>{ABC} ← 太字の「ABC」が表示される
## @<nop>$@<b>{ABC}$ ← 「@<b>{ABC}」がそのまま表示される
## //}
def inline_nop(str)
escape(str)
end
alias inline_letitgo inline_nop
## @<strong>{} のショートカット
def inline_B(str)
inline_strong(str)
end
## @<b>{} が //terminal{ ... //} で効くように上書き
def inline_b(str)
##macro('textbf', escape(str)) # original
if within_codeblock?()
#"{\\bfseries #{escape(str)}}" # \seqsplit{} 内では余計な空白が入る
#"{\\bfseries{}#{escape(str)}}" # \seqsplit{} 内では後続も太字化する
"\\bfseries{}#{escape(str)}\\mdseries{}" # \seqsplit{} 内でうまく効く
else
macro('textbf', escape(str))
end
end
## @<del>{} が //list や //terminal で効くように上書き
def inline_del(str)
##macro('reviewstrike', escape(str)) # original
if within_codeblock?()
#"\\reviewstrike{#{escape(str)}}" # \seqsplit{} 内でエラーになる
#"{\\reviewstrike{#{escape(str)}}}" # \seqsplit{} 内でもエラーにならないが折り返しされない
"{\\reviewstrike{\\seqsplit{#{escape(str)}}}}" # エラーにならないし、折り返しもされる
else
macro('reviewstrike', escape(str))
end
end
## コードブロック中で折り返し箇所を手動で指定する
## (\seqsplit による自動折り返し機能が日本語には効かないので、
## 長い行を日本語の箇所で折り返したいときは @<foldhere>{} を使う)
def inline_foldhere(arg)
return '\starterfoldhere{}'
end
## ハートマーク
def inline_hearts(str)
'$\heartsuit$'
end
## TeXのロゴマーク
def inline_TeX(str)
'\TeX{}'
end
## LaTeXのロゴマーク
def inline_LaTeX(str)
'\LaTeX{}'
end
## ターミナルでのカーソル(背景が白、文字が黒)
def inline_cursor(str)
"{\\startercursor{#{escape(str)}}}"
end
## 左寄せ
def textleft(lines)
puts '\begin{flushleft}'
puts lines
puts '\end{flushleft}'
end
## 右寄せ
## (注:Re:VIEWにはすでに //flushright{ ... //} があったので、今後はそちらを推奨)
def textright(lines)
puts '\begin{flushright}'
puts lines
puts '\end{flushright}'
end
## 中央揃え
## (注:Re:VIEWにはすでに //centering{ ... //} があったので、今後はそちらを推奨)
def textcenter(lines)
puts '\begin{center}'
puts lines
puts '\end{center}'
end
## 導入文(//lead{ ... //})のデザインをLaTeXのスタイルファイルで
## 変更できるよう、マクロを使う。
def lead(lines)
puts '\begin{starterlead}' # オリジナルは \begin{quotation}
puts lines
puts '\end{starterlead}'
end
## 章 (Chapter) の概要
## (導入文 //lead{ ... //} と似ているが、導入文では詩や物語を
## 引用するのが普通らしく、概要 (abstract) とは違うみたいなので、
## 概要を表すブロックを用意した。)
def abstract(lines)
puts '\begin{starterabstract}'
puts lines
puts '\end{starterabstract}'
end
## 章タイトルを独立したページに
def makechaptitlepage(lines, option)
case option
when nil, "" ;
when 'toc=section', 'toc=subsection' ;
when 'toc'
else
raise ArgumentError.new("//makechaptitlepage[#{option}]: unknown option (expected 'toc=section' or 'toc=subsection').")
end
puts "\\makechaptitlepage{#{option}}"
end
## 引用(複数段落に対応)
## (入れ子対応なので、中に箇条書きや別のブロックを入れられる)
def on_quote_block()
if within_context?(:note)
yes = truncate_if_endwith?("\\begin{starternoteinner}\n")
puts "\\end{starternoteinner}" unless yes
end
#
puts '\begin{starterquote}'
yield
puts '\end{starterquote}'
#
puts "\\begin{starternoteinner}" if within_context?(:note)
end
def quote(lines)
on_quote_block() do
puts lines
end
end
## 引用 (====[quote] ... ====[/quote])
## (ブロック構文ではないので、中に箇条書きや別のブロックを入れられる)
def quote_begin(level, label, caption)
puts '\begin{starterquote}'
end
def quote_end(level)
puts '\end{starterquote}'
end
## ノート(//note[caption]{ ... //})
## (入れ子対応なので、中に箇条書きや別のブロックを入れられる)
def on_note_block(caption=nil)
s = compile_inline(caption || "")
puts "\\begin{starternote}{#{s}}"
puts "\\begin{starternoteinner}"
yield
yes = truncate_if_endwith?("\\begin{starternoteinner}\n")
puts "\\end{starternoteinner}" unless yes
puts "\\end{starternote}"
end
def note(lines, caption=nil)
on_note_block(caption) do
puts lines
end
end
## ノート (====[note] ... ====[/note])
## (ブロック構文ではないので、中に箇条書きや別のブロックを入れられる)
def note_begin(level, label, caption)
enter_context(:note)
s = compile_inline(caption || "")
puts "\\begin{starternote}{#{s}}"
puts "\\begin{starternoteinner}"
end
def note_end(level)
yes = truncate_if_endwith?("\\begin{starternoteinner}\n")
puts "\\end{starternoteinner}" unless yes
puts "\\end{starternote}"
exit_context(:note)
end
## コードリスト(//list, //emlist, //listnum, //emlistnum, //cmd, //source)
## TODO: code highlight support
def list(lines, id=nil, caption=nil, lang=nil)
program(lines, id, caption, _codeblock_optstr(lang, false))
end
def listnum(lines, id=nil, caption=nil, lang=nil)
program(lines, id, caption, _codeblock_optstr(lang, true))
end
def emlist(lines, caption=nil, lang=nil)
program(lines, nil, caption, _codeblock_optstr(lang, false))
end
def emlistnum(lines, caption=nil, lang=nil)
program(lines, nil, caption, _codeblock_optstr(lang, true))
end
def source(lines, caption=nil, lang=nil)
program(lines, nil, caption, _codeblock_optstr(lang, false))
end
def cmd(lines, caption=nil, lang=nil)
terminal(lines, nil, caption, _codeblock_optstr(lang, false))
end
def _codeblock_optstr(lang, lineno_flag)
arr = []
arr << lang if lang
if lineno_flag
first_line_num = line_num
arr << "lineno=#{first_line_num}"
arr << "linenowidth=0"
end
return arr.join(",")
end
private :_codeblock_optstr
## 出力結果の開始部と終了部(Starterのドキュメントで使用)
## (Re:VIEWではブロックの入れ子も「===[xxx]」の入れ子もできないため)
def resultbegin()
#puts "\\begin{starterresult}" # error in note block
puts "\\starterresult"
end
def resultend()
#puts "\\end{starterresult}" # error in note block
puts "\\endstarterresult"
end
end
## HTML(ePub)用の定義
class HTMLBuilder
## 改ページはHTMLにはない
def inline_clearpage(str) # インライン命令
puts '<p></p>'
puts '<hr />'
puts '<p></p>'
end
def clearpage(str) # ブロック命令
puts '<p></p>'
puts '<hr />'
puts '<p></p>'
end
## 引数をそのまま表示 (No Operation)
def inline_nop(str)
escape_html(str)
end
alias inline_letitgo inline_nop
## @<strong>{} のショートカット
def inline_B(str)
inline_strong(str)
end
## コードブロック中で折り返し箇所を手動で指定する
def inline_foldhere(arg)
'<br>'
end
## ハートマーク
def inline_hearts(str)
#'♥'
'♥'
end
## TeXのロゴマーク
def inline_TeX(str)
'TeX'
end
## LaTeXのロゴマーク
def inline_LaTeX(str)
'LaTeX'
end
## ターミナルでのカーソル(背景が白、文字が黒)
def inline_cursor(str)
"<span class=\"cursor\">#{escape_html(str)}</span>"
end
## 左寄せ
def textleft(lines)
puts '<div style="text-align:left">'
puts lines
puts '</div>'
end
## 右寄せ
def textright(lines)
puts '<div style="text-align:right">'
puts lines
puts '</div>'
end
## 中央揃え
def textcenter(lines)
puts '<div style="text-align:center">'
puts lines
puts '</div>'
end
## 章 (Chapter) の概要
def abstract(lines)
puts '<div class="abstract">'
puts lines
puts '</div>'
end
## 章タイトルを独立したページに
def makechaptitlepage(lines)
puts '' # HTMLでは特に何もしない
end
## 引用(複数段落に対応)
def blockquote(lines)
puts '<blockquote class="blockquote">'
puts lines
puts '</blockquote>'
end
## 引用(//quote{ ... //})
## (入れ子対応なので、中に箇条書きや別のブロックを入れられる)
def on_quote_block()
puts '<blockquote class="blockquote">'
yield
puts '</blockquote>'
end
def quote(lines)
on_quote_block() do
puts lines
end
end
## 引用 (====[quote] ... ====[/quote])
## (ブロック構文ではないので、中に別のブロックや箇条書きを入れられる)
def quote_begin(level, label, caption)
puts '<blockquote class="blockquote">'
end
def quote_end(level)
puts '</blockquote>'
end
## ノート(//note{ ... //})
## (入れ子対応なので、中に箇条書きや別のブロックを入れられる)
def on_note_block(caption=nil)
puts "<div class=\"note\">"
puts "<h5>#{caption}</h5>" if caption.present?
yield
puts "</div>"
end
def note(lines, caption=nil)
on_quote_block(caption) do
puts lines
end
end
## ノート (====[note] ... ====[/note])
## (ブロック構文ではないので、中に別のブロックや箇条書きを入れられる)
def note_begin(level, label, caption)
s = compile_inline(caption || "")
puts "<div class=\"note\">"
puts "<h5>#{s}</h5>" if s.present?
end
def note_end(level)
puts "</div>"
end
## 出力結果の開始部と終了部(Starterのドキュメントで使用)
## (Re:VIEWではブロックの入れ子も「===[xxx]」の入れ子もできないため)
def resultbegin()
puts "<hr/>"
end
def resultend()
puts "<hr/>"
end
end
end