-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathreview-ext.rb
More file actions
229 lines (197 loc) · 6.5 KB
/
Copy pathreview-ext.rb
File metadata and controls
229 lines (197 loc) · 6.5 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
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
# Support "child" format
# https://review-knowledge-ja.readthedocs.io/ja/latest/reviewext/nest.html
module ReVIEW
module BuilderOverride
Compiler.defsingle :child, 1
def child(tag)
@children ||= []
puts "♬→#{tag}←♬"
if tag.start_with?('/')
if @children.empty?
error "#{tag} is shown but there isn't any opened //child"
elsif @children[-1] != tag.sub('/', '')
error "#{tag} is shown but previous '#{@children[-1]}' is not closed yet"
else
@children.pop
end
else
unless %w(dl ol ul).include?(tag)
error "#{tag} is invalid value for //child (dl, ol, or ul)"
end
@children.push(tag)
end
end
end
class Builder
prepend BuilderOverride
end
module LATEXBuilderOverride
def solve_nest(s)
s = s.
gsub("\\end{description}\n\n♬→dl←♬\n", "\n").
gsub('♬→/dl←♬', '\\end{description}←END♬').
gsub("\\end{itemize}\n\n♬→ul←♬\n", "\n").
gsub('♬→/ul←♬', '\\end{itemize}←END♬').
gsub("\\end{enumerate}\n\n♬→ol←♬\n", "\n").
gsub('♬→/ol←♬', '\\end{enumerate}←END♬').
gsub("\\end{description}←END♬\n\n\\begin{description}", '').
gsub("\\end{itemize}←END♬\n\n\\begin{itemize}", '').
gsub("\\end{enumerate}←END♬\n\n\\begin{enumerate}", '').
gsub('←END♬', '')
if s =~ /♬→(.+?)←♬/
error "Last //child[#{$1}] isn't closed yet."
end
s
end
def result
solve_nest(super)
end
end
class LATEXBuilder
prepend LATEXBuilderOverride
end
module HTMLBuilderOverride
def solve_nest(s)
s = s.
gsub("</dd>\n</dl>\n♬→dl←♬", '').
gsub('♬→/dl←♬', "</dd>\n</dl>←END♬").
gsub("</li>\n</ul>\n♬→ul←♬", '').
gsub('♬→/ul←♬', "</li>\n</ul>←END♬").
gsub("</li>\n</ol>\n♬→ol←♬", '').
gsub('♬→/ol←♬', "</li>\n</ol>←END♬").
gsub("</dl>←END♬\n<dl>", '').
gsub("</ul>←END♬\n<ul>", '').
gsub("</ol>←END♬\n<ol>", '').
gsub('←END♬', '')
if s =~ /♬→(.+?)←♬/
error "Last //child[#{$1}] isn't closed yet."
end
s
end
def result
solve_nest(super)
end
end
class HTMLBuilder
prepend HTMLBuilderOverride
end
module IDGXMLBuilderOverride
def solve_nest(s)
s = s.
gsub("</dd></dl>♬→dl←♬", '').
gsub('♬→/dl←♬', "</dd></dl>←END♬").
gsub("</li></ul>♬→ul←♬", '').
gsub('♬→/ul←♬', "</li></ul>←END♬").
gsub("</li></ol>♬→ol←♬", '').
gsub('♬→/ol←♬', "</li></ol>←END♬").
gsub("</dl>←END♬<dl>", '').
gsub("</ul>←END♬<ul>", '').
gsub("</ol>←END♬<ol>", '').
gsub('←END♬', '')
if s =~ /♬→(.+?)←♬/
error "Last //child[#{$1}] isn't closed yet."
end
s
end
def result
solve_nest(super)
end
end
class IDGXMLBuilder
prepend IDGXMLBuilderOverride
end
module PLAINTEXTBuilderOverride
def solve_nest(s)
lines = []
@children = []
s.split("\n").each do |l|
if l =~ /\A♬→(dl|ul|ol)←♬/
@children.push($1)
lines.push('♬→END←♬')
elsif l =~ /\A♬→\/(dl|ul|ol)←♬/
@children.pop
lines.push('♬→END←♬')
else
lines.push("\t" * @children.size + l)
end
end
s = lines.join("\n").gsub(/\n*♬→END←♬\n*/, "\n")
if s =~ /♬→(.+?)←♬/
error "Last //child[#{$1}] isn't closed yet."
end
s
end
def result
solve_nest(super)
end
end
class PLAINTEXTBuilder
prepend PLAINTEXTBuilderOverride
end
end
# Convert link to footnote
# https://gist.github.com/kauplan/973d5037e2cc0f8edb474bd0960c2dcc
ReVIEW::LATEXBuilder.class_eval do
def compile_href(url, label)
if /\A[a-z]+:/ =~ url
if label and url != label
#macro('href', escape_url(url), escape(label)) # original
escape(label) + macro('footnote', macro('url', escape_url(url)))
else
macro('url', escape_url(url))
end
else
macro('ref', url)
end
end
end
# Break lines in code block
# https://review-knowledge-ja.readthedocs.io/ja/latest/latex/linebreak-verbatim.html
module ReVIEW
module LATEXBuilderOverride
# gem install unicode-display_width
require 'unicode/display_width'
require 'unicode/display_width/string_ext'
CR = '' # 送り出し文字。LaTeXコードも可
ZWSCALE = 0.875 # 和文・欧文の比率。\setlength{\xkanjiskip}{\z@} しておいたほうがよさそう
JAPANESE_CHAR_ONLY_LINE_MATCHER = /\A(\p{Hiragana}|\p{Katakana}|[ー-、。≪≫「」『』()]|[一-龠々])\z/
def split_line(string, max_chars)
# 日本語の文字を含む行の改行は、自動折り返しに任せる
return [string] if JAPANESE_CHAR_ONLY_LINE_MATCHER =~ string
lines = []
line = ''
width = 0
string.each_char do |char|
char_width = char.display_width(2) # Ambiguousを全角扱い
char_width *= ZWSCALE if char_width == 2
if width + char_width > max_chars
lines << line
line = char
width = char_width
else
line << char
width += char_width
end
end
lines << line
lines
end
def code_line(type, line, idx, id, caption, lang)
# _typeには'emlist'などが入ってくるので、環境に応じて分岐は可能
max_chars = 78
max_chars = 70 if @doc_status[:column]
lines = split_line(unescape(detab(line)), max_chars)
# インラインopはこの時点でもう展開されたものが入ってしまっているので、escapeでエスケープされてしまう…
escape(lines.join("\x01\n")).gsub("\x01", CR) + "\n"
end
def code_line_num(type, line, first_line_num, idx, id, caption, lang)
max_chars = 78
max_chars = 70 if @doc_status[:column]
lines = split_line(unescape(detab(line)), max_chars)
(idx + first_line_num).to_s.rjust(2) + ': ' + escape(lines.join("\x01\n ")).gsub("\x01", CR) + "\n"
end
end
class LATEXBuilder
prepend LATEXBuilderOverride
end
end