mdextab 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +31 -8
- data/.rubocop_todo.yml +8 -551
- data/Gemfile +2 -0
- data/bin/makemdtab +46 -283
- data/bin/mdextab +12 -20
- data/lib/mdextab/layer.rb +157 -0
- data/lib/mdextab/makemdtab.rb +119 -0
- data/lib/mdextab/table.rb +11 -14
- data/lib/mdextab/tbody.rb +9 -9
- data/lib/mdextab/td.rb +22 -22
- data/lib/mdextab/th.rb +7 -7
- data/lib/mdextab/token.rb +98 -4
- data/lib/mdextab/tr.rb +1 -1
- data/lib/mdextab/version.rb +1 -1
- data/lib/mdextab.rb +418 -533
- data/mdextab.gemspec +4 -0
- metadata +60 -4
- data/lib/mdextab/loggerx.rb +0 -58
- data/lib/mdextab/yamlx.rb +0 -67
data/bin/makemdtab
CHANGED
@@ -1,304 +1,67 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# -*- coding: utf-8 -*-
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mdextab"
|
5
|
+
require "messagex"
|
7
6
|
|
8
|
-
require
|
9
|
-
require 'byebug'
|
7
|
+
require "simpleoptparse"
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
EXIT_CODE_CANNOTFIND_YAMLFILE_OR_EMPTY=105
|
17
|
-
EXIT_CODE_YAMLFILE_IS_EMPTY=106
|
18
|
-
EXIT_CODE_CANNOTFIND_ERUBYFILE_OR_EMPTY=107
|
19
|
-
EXIT_CODE_ERUBYFILE_IS_EMPTY=108
|
20
|
-
EXIT_CODE_CANNOTFIND_MDFILE_OR_EMPTY=109
|
21
|
-
EXIT_CODE_MDFILE_IS_EMPTY=110
|
22
|
-
|
23
|
-
class Makemdtab
|
24
|
-
def initialize(opts, erubyfnames)
|
25
|
-
@dataop=opts["dataop"]
|
26
|
-
@datayamlfname=opts["data"]
|
27
|
-
@yamlop=opts["yamlop"]
|
28
|
-
@yamlfname=opts["setting"]
|
29
|
-
@auxyamlfname=opts["auxsetting"]
|
30
|
-
@erubyfnames=erubyfnames
|
31
|
-
@outputfname=opts["output"]
|
32
|
-
|
33
|
-
@exit_cannot_find_file=1
|
34
|
-
@exit_cannot_write_file=2
|
35
|
-
|
36
|
-
@erubies = {}
|
37
|
-
|
38
|
-
@logger=Logger.new(STDOUT)
|
39
|
-
@logger.level = Logger::INFO
|
40
|
-
@logger.level = Logger::DEBUG if opts["debug"]
|
41
|
-
@logger.formatter = proc do |severity, datetime, progname, msg|
|
42
|
-
"#{msg}\n"
|
43
|
-
end
|
44
|
-
|
45
|
-
[@datayamlfname, @yamlfname, @auxyamlfname, @erubyfnames].flatten.each do |fname|
|
46
|
-
next if File.exist?(fname)
|
47
|
-
|
48
|
-
mes="Can't find #{fname}"
|
49
|
-
outputError(mes)
|
50
|
-
exit(EXIT_CODE_CANNOT_FILE_FILE)
|
51
|
-
end
|
52
|
-
|
53
|
-
begin
|
54
|
-
@output = File.open(@outputfname, 'w')
|
55
|
-
rescue RuntimeError => ex
|
56
|
-
mes2 = "Can't write #{@outputfname}"
|
57
|
-
outputError(mes2)
|
58
|
-
exit(EXIT_CODE_CANNOT_WRITE_FILE)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def outputError(mes)
|
63
|
-
if @logger
|
64
|
-
@logger.error(mes)
|
65
|
-
else
|
66
|
-
STDERR.puts(mes)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def makeMd()
|
71
|
-
load(@dataop, @datayamlfname, @yamlop, @auxyamlfname, @yamlfname, @erubyfnames).map{|x|
|
72
|
-
@output.puts(x)
|
73
|
-
}
|
74
|
-
end
|
75
|
-
|
76
|
-
def load(dataop, datayamlfname, yamlop, auxyamlfname, yamlfname, erubyfnames)
|
77
|
-
eruby0 = nil
|
78
|
-
eruby1 = nil
|
79
|
-
obj = {}
|
80
|
-
obj0 = YAML.load_file(auxyamlfname) if auxyamlfname
|
81
|
-
obj = obj0 if obj0
|
82
|
-
|
83
|
-
case yamlop
|
84
|
-
when :MERGE
|
85
|
-
obj2 = YAML.load_file(yamlfname)
|
86
|
-
if obj2
|
87
|
-
if obj
|
88
|
-
objx = obj.merge(obj2)
|
89
|
-
else
|
90
|
-
objx = obj2
|
91
|
-
end
|
92
|
-
else
|
93
|
-
objx = obj
|
94
|
-
end
|
95
|
-
when :REPLACE
|
96
|
-
str = File.read(yamlfname)
|
97
|
-
str2 = Erubis::Eruby.new(str).result(obj)
|
98
|
-
objx0 = YAML.load(str2)
|
99
|
-
if objx0
|
100
|
-
objx = objx
|
101
|
-
else
|
102
|
-
objx = {}
|
103
|
-
end
|
104
|
-
else
|
105
|
-
# do nothing
|
106
|
-
end
|
107
|
-
|
108
|
-
erubystr=erubyfnames.map{|x| checkAndLoadErubyfile(x)}.join("\n")
|
109
|
-
if @dataop == :PATTERN_FOUR
|
110
|
-
mdfname=datayamlfname
|
111
|
-
objx["PARENT_DIR"] = ENV['MDEXTAB_MAKE']
|
112
|
-
|
113
|
-
mdstr=checkAndLoadMdfile(mdfname)
|
114
|
-
|
115
|
-
dx = [erubystr, mdstr].join("\n")
|
116
|
-
@erubies[mdfname] ||= Erubis::Eruby.new(dx)
|
117
|
-
array=[@erubies[mdfname].result(objx)]
|
118
|
-
else
|
119
|
-
puts "datayamlfname=#{datayamlfname}"
|
120
|
-
strdata2=checkAndExpandYamlfile(datayamlfname, objx)
|
121
|
-
data = YAML.load(strdata2)
|
122
|
-
if data.class != Hash
|
123
|
-
puts "strdata2=#{strdata2}"
|
124
|
-
p data
|
125
|
-
exit(300)
|
9
|
+
opts = {}
|
10
|
+
banner = "Usage: bundle exec ruby bin/makemdtab [--debug|--verbose] (-o|--output) outfname (-d|--data) datafname yamlfname --dataop (fileinclude|yamltomd) -es erubystatic -ev erubyvariable *]"
|
11
|
+
if ARGV.empty?
|
12
|
+
puts(banner)
|
13
|
+
exit(@mes.ec("EXIT_CODE_ARGV"))
|
126
14
|
end
|
127
|
-
erubyfname=erubyfnames.last
|
128
|
-
case dataop
|
129
|
-
when :PATTERN_ONE
|
130
|
-
array=loadWithPattern1(data, erubyfname, erubystr)
|
131
|
-
when :PATTERN_TWO
|
132
|
-
array=loadWithPattern2(data, erubyfname, erubystr)
|
133
|
-
when :PATTERN_THREE
|
134
|
-
dir=File.dirname(datayamlfname)
|
135
|
-
array=loadWithPattern3(data, erubyfname, erubystr, dir)
|
136
|
-
else
|
137
|
-
array=[]
|
138
|
-
# do nothing
|
139
|
-
end
|
140
|
-
end
|
141
|
-
array
|
142
|
-
end
|
143
15
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
16
|
+
Simpleoptparse::Simpleoptparse.parse(ARGV, opts, banner, Mdextab::VERSION, nil) do |parser|
|
17
|
+
parser.on("--debug") {|_x| opts["debug"] = true }
|
18
|
+
parser.on("--verbose") {|_x| opts["verbose"] = true }
|
19
|
+
parser.on("--es path") {|x| opts["fnameStatic"] = x }
|
20
|
+
parser.on("--ev path") {|x| opts["fnameVariable"] = x }
|
21
|
+
parser.on("-t path", "--template") {|x| opts["template"] = opts["t"] = x }
|
22
|
+
parser.on("-y path", "--yaml") {|x| opts["yaml"] = opts["y"] = x }
|
23
|
+
parser.on("-o path", "--output") {|x| opts["output"] = opts["o"] = x }
|
24
|
+
parser.on("-d path", "--data") {|x| opts["data"] = opts["d"] = x }
|
25
|
+
parser.on("--dataop op") do |x|
|
26
|
+
if /^f/.match?(x.downcase)
|
27
|
+
opts["dataop"] = :FILE_INCLUDE
|
148
28
|
else
|
149
|
-
|
150
|
-
outputError(mes)
|
151
|
-
exit(EXIT_CODE_CANNOTFIND_ERUBYFILE_OR_EMPTY)
|
29
|
+
opts["dataop"] = :YAML_TO_MD
|
152
30
|
end
|
153
|
-
if erubystr.strip.empty?
|
154
|
-
mes=%Q!#{erubyfname} is empty!
|
155
|
-
outputError(mes)
|
156
|
-
exit(EXIT_CODE_ERUBYFILE_IS_EMPTY)
|
157
|
-
end
|
158
|
-
erubystr
|
159
|
-
end
|
160
|
-
|
161
|
-
def checkAndLoadMdfile(mdfname)
|
162
|
-
size2=File.size?(mdfname)
|
163
|
-
if size2 and size2 > 0
|
164
|
-
mdstr=File.read(mdfname)
|
165
|
-
else
|
166
|
-
mes=%Q!Can not find #{mdfname} or is empty!
|
167
|
-
outputError(mes)
|
168
|
-
exit(EXIT_CODE_CANNOTFIND_MDFILE_OR_EMPTY)
|
169
|
-
end
|
170
|
-
if mdstr.strip.empty?
|
171
|
-
mes=%Q!#{mdfname} is empty!
|
172
|
-
outputError(mes)
|
173
|
-
exit(EXIT_CODE_MDFILE_IS_EMPTY)
|
174
|
-
end
|
175
|
-
mdstr
|
176
|
-
end
|
177
|
-
|
178
|
-
def checkAndExpandYamlfile(yamlfname, objx)
|
179
|
-
size=File.size?(yamlfname)
|
180
|
-
if size and size > 0
|
181
|
-
puts File.mtime(yamlfname)
|
182
|
-
strdata = File.read(yamlfname)
|
183
|
-
else
|
184
|
-
mes=%Q!Can not find #{yamlfname} or is empty!
|
185
|
-
outputError(mes)
|
186
|
-
exit(EXIT_CODE_CANNOTFIND_YAMLFILE_OR_EMPTY)
|
187
|
-
end
|
188
|
-
|
189
|
-
if strdata.strip.empty?
|
190
|
-
mes=%Q!#{yamlfname} is empty!
|
191
|
-
outputError(mes)
|
192
|
-
exit(EXIT_CODE_YAMLFILE_IS_EMPTY)
|
193
|
-
else
|
194
|
-
puts Digest::MD5.hexdigest(strdata)
|
195
|
-
#
|
196
|
-
strdata2 = Erubis::Eruby.new(strdata).result(objx)
|
197
|
-
end
|
198
|
-
|
199
|
-
strdata2
|
200
|
-
end
|
201
|
-
|
202
|
-
def loadWithPattern1(data, erubyfname, erubystr)
|
203
|
-
@erubies[erubyfname] ||= Erubis::Eruby.new(erubystr)
|
204
|
-
[@erubies[erubyfname].result(data)]
|
205
|
-
end
|
206
|
-
|
207
|
-
def loadWithPattern2(data, erubyfname, erubystr)
|
208
|
-
data.map{ |x|
|
209
|
-
@erubies[erubyfname] ||= Erubis::Eruby.new(erubystr)
|
210
|
-
@erubies[erubyfname].result(x)
|
211
|
-
}
|
212
|
-
end
|
213
|
-
|
214
|
-
def loadWithPattern3(data, erubyfname, erubystr, dir)
|
215
|
-
hs={}
|
216
|
-
data.each do |k,v|
|
217
|
-
if /items/.match(k)
|
218
|
-
hs[k]=[]
|
219
|
-
v.each do |v2|
|
220
|
-
if /^path=(.*)/.match(v2)
|
221
|
-
hs[k] << File.join(dir, $1)
|
222
|
-
else
|
223
|
-
hs[k] << v2
|
224
|
-
end
|
225
|
-
end
|
226
|
-
else
|
227
|
-
hs[k]=v
|
228
|
-
end
|
229
|
-
end
|
230
|
-
@erubies[erubyfname] ||= Erubis::Eruby.new(erubystr)
|
231
|
-
[@erubies[erubyfname].result(hs)]
|
232
31
|
end
|
32
|
+
end
|
233
33
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
34
|
+
if opts["debug"]
|
35
|
+
mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :debug)
|
36
|
+
elsif opts["verbose"]
|
37
|
+
mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :verbose)
|
38
|
+
else
|
39
|
+
mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0)
|
238
40
|
end
|
239
41
|
|
240
|
-
opts
|
241
|
-
|
242
|
-
if ARGV.size == 0
|
42
|
+
unless opts["data"]
|
43
|
+
puts("Not specified -data")
|
243
44
|
puts(banner)
|
244
|
-
exit(
|
45
|
+
exit(mes.ec("EXIT_CODE_ARGV_ERROR"))
|
245
46
|
end
|
246
47
|
|
247
|
-
|
248
|
-
|
249
|
-
parser.on('--contest' ) {|x| opts["kind"] = :contest}
|
250
|
-
parser.on('-d value', '--data' ,'data file') {|x| opts["data"] = x}
|
251
|
-
parser.on('-s value', '--setting') {|x| opts["setting"] = x}
|
252
|
-
parser.on('-a [value]', '--auxsetting') {|x| opts["auxsetting"] = x}
|
253
|
-
parser.on('-o value', '--output') {|x| opts["output"] = x}
|
254
|
-
parser.on('-y value', '--yamlop') {|x|
|
255
|
-
case x.upcase
|
256
|
-
when /^MERGE/
|
257
|
-
opts["yamlop"] = :MERGE
|
258
|
-
when /^REPLACE/
|
259
|
-
opts["yamlop"] = :REPLACE
|
260
|
-
else
|
261
|
-
# do nothing
|
262
|
-
end
|
263
|
-
}
|
264
|
-
parser.on('-p value', '--dataop') {|x|
|
265
|
-
case x.upcase
|
266
|
-
when /^PATTERN_ONE/
|
267
|
-
opts["dataop"] = :PATTERN_ONE
|
268
|
-
when /^PATTERN_TWO/
|
269
|
-
opts["dataop"] = :PATTERN_TWO
|
270
|
-
when /^PATTERN_THREE/
|
271
|
-
opts["dataop"] = :PATTERN_THREE
|
272
|
-
when /^PATTERN_FOUR/
|
273
|
-
opts["dataop"] = :PATTERN_FOUR
|
274
|
-
else
|
275
|
-
# do nothing
|
276
|
-
end
|
277
|
-
}
|
278
|
-
}
|
279
|
-
|
280
|
-
unless opts["output"] and opts["yamlop"] and opts["dataop"]
|
48
|
+
unless opts["output"]
|
49
|
+
puts("Not specified -oputut")
|
281
50
|
puts(banner)
|
282
|
-
|
283
|
-
puts "don't be specified -y value" unless opts["yamlop"]
|
284
|
-
puts "don't be specified -p value" unless opts["dataop"]
|
285
|
-
p ARGV
|
286
|
-
p opts
|
287
|
-
exit(EXIT_CODE_MISSING_REQUIRED_ARGUMENT)
|
51
|
+
exit(mes.ec("EXIT_CODE_ARGV_ERROR"))
|
288
52
|
end
|
289
53
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
54
|
+
opts["data"] = File.expand_path(opts["data"])
|
55
|
+
opts["output"] = File.expand_path(opts["output"])
|
56
|
+
opts["yaml"] = File.expand_path(opts["yaml"]) if opts["yaml"]
|
57
|
+
|
58
|
+
if opts["yaml"]
|
59
|
+
auxhs = Filex::Filex.checkAndLoadYamlfile(opts["yaml"], mes)
|
60
|
+
else
|
61
|
+
auxhs = {}
|
295
62
|
end
|
296
63
|
|
297
|
-
x
|
298
|
-
x.makeMd()
|
64
|
+
x.makeMd2(opts["template"], auxhs)
|
299
65
|
x.postProcess
|
300
|
-
|
301
|
-
exit(EXIT_CODE_NORMAL_EXIT)
|
302
|
-
|
303
|
-
|
304
|
-
|
66
|
+
mes.outputInfo("# makemdtab End #")
|
67
|
+
exit(mes.ec("EXIT_CODE_NORMAL_EXIT"))
|
data/bin/mdextab
CHANGED
@@ -1,28 +1,20 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mdextab"
|
5
5
|
|
6
|
-
require
|
6
|
+
require "simpleoptparse"
|
7
7
|
|
8
|
-
opts = {
|
8
|
+
opts = {}
|
9
9
|
banner = "Usage: bundle exec ruby bin/mdextab in_md_fname out_md_fname settng_yaml auxiliary_setting_yaml"
|
10
10
|
|
11
|
-
Simpleoptparse::Simpleoptparse.parse(ARGV
|
12
|
-
|
13
|
-
|
11
|
+
Simpleoptparse::Simpleoptparse.parse(ARGV, opts, banner, Mdextab::VERSION, nil) do |parser|
|
12
|
+
parser.on("-d", "--debug") {|_x| opts["debug"] = true }
|
13
|
+
end
|
14
14
|
|
15
15
|
fname = ARGV[0]
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
m = Mdextab::Mdextab.new(opts,fname,
|
20
|
-
m.
|
21
|
-
m.end
|
22
|
-
|
23
|
-
puts "##### Mdextab fname=#{fname}"
|
24
|
-
puts "##### Mdextab oFname=#{oFname}"
|
25
|
-
puts "##### Mdextab yamlFname=#{yamlFname}"
|
26
|
-
puts "##### Mdextab auxiliaryYamlFname=#{auxiliaryYamlFname}"
|
27
|
-
|
28
|
-
exit(0)
|
16
|
+
o_fname = ARGV[1]
|
17
|
+
yaml_fname = ARGV[2]
|
18
|
+
opts["yaml_fname"] = yaml_fname
|
19
|
+
m = Mdextab::Mdextab.new(opts, fname, o_fname)
|
20
|
+
m.parse2(yaml_fname)
|
@@ -0,0 +1,157 @@
|
|
1
|
+
module Mdextab
|
2
|
+
class Layer
|
3
|
+
attr_accessor :return_from_nested_env, :cur_layer, :size
|
4
|
+
|
5
|
+
def initialize(mes, output)
|
6
|
+
@mes = mes
|
7
|
+
@output = output
|
8
|
+
@return_from_nested_env = false
|
9
|
+
|
10
|
+
@layer_struct = Struct.new(:table, :star, :cur_state, :fname, :lineno)
|
11
|
+
@cur_layer = nil
|
12
|
+
@layers = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def cur_state=(val)
|
16
|
+
# raise if val.class != Symbol
|
17
|
+
@cur_layer.cur_state = val
|
18
|
+
end
|
19
|
+
|
20
|
+
def cur_state
|
21
|
+
raise if @cur_layer.cur_state.class != Symbol
|
22
|
+
@cur_layer.cur_state
|
23
|
+
end
|
24
|
+
|
25
|
+
def table
|
26
|
+
@cur_layer.table
|
27
|
+
end
|
28
|
+
|
29
|
+
def table=(val)
|
30
|
+
@cur_layer.table = val
|
31
|
+
end
|
32
|
+
|
33
|
+
def star=(val)
|
34
|
+
@cur_layer.star = val
|
35
|
+
end
|
36
|
+
|
37
|
+
def star
|
38
|
+
@cur_layer.star
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_layer(fname, lineno, state=:START)
|
42
|
+
new_layer = @layer_struct.new(nil, nil, nil, fname, lineno)
|
43
|
+
@layers << new_layer
|
44
|
+
@size = @layers.size
|
45
|
+
# raise if state.class != Symbol
|
46
|
+
new_layer.cur_state = state
|
47
|
+
if @cur_layer
|
48
|
+
new_layer.star = @cur_layer.star
|
49
|
+
else
|
50
|
+
new_layer.star = false
|
51
|
+
end
|
52
|
+
@cur_layer = new_layer
|
53
|
+
end
|
54
|
+
|
55
|
+
def pop_prev_layer
|
56
|
+
tmp_ = @layers.pop
|
57
|
+
@size = @layers.size
|
58
|
+
@cur_layer = @layers.last
|
59
|
+
|
60
|
+
tmp_
|
61
|
+
end
|
62
|
+
|
63
|
+
def peek_prev_layer
|
64
|
+
return nil unless @layers.size > 1
|
65
|
+
|
66
|
+
@layers[@layers.size - 2]
|
67
|
+
end
|
68
|
+
|
69
|
+
def process_nested_table_start(token, lineno, fname)
|
70
|
+
if table.tbody.nil?
|
71
|
+
table.add_tbody(lineno)
|
72
|
+
end
|
73
|
+
@mes.output_debug("B process_nested_table_start 1 @cur_layer.table=#{@cur_layer.table.object_id} token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} cur_state=#{@cur_layer.cur_state}")
|
74
|
+
add_layer(fname, lineno, :OUT_OF_TABLE)
|
75
|
+
@cur_layer.table = Table.new(token.opt[:lineno], @mes, token.opt[:attr])
|
76
|
+
@mes.output_debug("process_nested_table_start 3 token.kind=#{token.kind} cur_state=#{@cur_layer.cur_state}")
|
77
|
+
end
|
78
|
+
|
79
|
+
def process_table_end(token)
|
80
|
+
prev_layer = peek_prev_layer
|
81
|
+
if prev_layer
|
82
|
+
process_table_end_for_prev_env(token, prev_layer)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def process_table_end_for_prev_env(token, prev_layer)
|
87
|
+
tmp_table = @cur_layer.table
|
88
|
+
pop_prev_layer
|
89
|
+
@return_from_nested_env = true
|
90
|
+
|
91
|
+
case @cur_layer.cur_state
|
92
|
+
when :IN_TD
|
93
|
+
prev_layer.table.td_append(tmp_table, prev_layer.star)
|
94
|
+
when :IN_TD_NO_TBODY
|
95
|
+
prev_layer.table.td_append(tmp_table, prev_layer.star)
|
96
|
+
when :IN_TH
|
97
|
+
prev_layer.table.th_append(tmp_table, prev_layer.star)
|
98
|
+
when :IN_TH_NO_TBODY
|
99
|
+
prev_layer.table.th_append(tmp_table, prev_layer.star)
|
100
|
+
when :IN_TABLE
|
101
|
+
if prev_layer.table.nil?
|
102
|
+
@mes.output_debug("In process_nested_table_env_for_prev_env: table=nil token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} cur_state=#{@cur_layer.cur_state}")
|
103
|
+
raise
|
104
|
+
end
|
105
|
+
prev_layer.table.add(tmp_table)
|
106
|
+
when :IN_TABLE_BODY
|
107
|
+
prev_layer.table.add(tmp_table)
|
108
|
+
when :START
|
109
|
+
@mes.output_debug("In process_nested_table_env_for_prev_env: table=nil token.kind=#{token.kind} token.opt[:lineno]=#{token.opt[:lineno]} cur_state=#{@cur_layer.cur_state}")
|
110
|
+
raise
|
111
|
+
else
|
112
|
+
v = prev_layer.cur_state || "nil"
|
113
|
+
@mes.output_fatal("E100 cur_state=#{v}")
|
114
|
+
@mes.output_fatal("table=#{prev_layer.table}")
|
115
|
+
@mes.output_fatal("IllegalState(#{@cur_layer.cur_state} in process_table_end(#{token})")
|
116
|
+
exit(@mes.ec("EXIT_CODE_TABLE_END"))
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def check_layers(fname)
|
121
|
+
case @cur_layer.cur_state
|
122
|
+
when :OUT_OF_TABLE
|
123
|
+
if @layers.size > 1
|
124
|
+
@mes.output_fatal("illeagal nested env after parsing|:OUT_OF_TABLE")
|
125
|
+
@mes.output_fatal("@layers.size=#{@layers.size} :TABLE_START #{fname} #{table.lineno}")
|
126
|
+
@layers.map {|x| @mes.output_debug("== @layers.cur_state=#{x.cur_state} :TABLE_START #{fname} #{x.table.lineno}") }
|
127
|
+
@mes.output_debug("== table")
|
128
|
+
@mes.output_info(table)
|
129
|
+
exit(@mes.ec("EXIT_CODE_EXCEPTION"))
|
130
|
+
end
|
131
|
+
when :START
|
132
|
+
if @layers.size > 1
|
133
|
+
@mes.output_fatal("illeagal nested env after parsing|:START")
|
134
|
+
@mes.output_fatal("@layers.size=#{@layers.size}")
|
135
|
+
@layers.map {|x| @mes.output_error("== @layers.cur_state=#{x.cur_state} :TABLE_START #{fname} #{x.table.lineno}") }
|
136
|
+
@mes.output_error("== table")
|
137
|
+
@mes.output_error(table)
|
138
|
+
exit(@mes.ec("EXIT_CODE_EXCEPTION"))
|
139
|
+
end
|
140
|
+
else
|
141
|
+
@mes.output_fatal("illeagal state after parsing(@cur_layer.cur_state=#{@cur_layer.cur_state}|fname=#{fname}")
|
142
|
+
@mes.output_fatal("@layers.size=#{@layers.size}")
|
143
|
+
@mes.output_error("== cur_state=#{@cur_layer.cur_state}")
|
144
|
+
@layers.map {|x| @mes.output_error("== @layers.cur_state=#{x.cur_state} #{fname}:#{x.table.lineno}") }
|
145
|
+
@mes.output_error("")
|
146
|
+
exit(@mes.ec("EXIT_CODE_ILLEAG<AL_STATE"))
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def debug(nth, token)
|
151
|
+
@mes.output_debug("***#{nth}")
|
152
|
+
@layers.each_with_index {|_x, ind| @mes.output_debug("@layers[#{ind}]=#{@layers[ind]}") }
|
153
|
+
@mes.output_debug("******#{nth}")
|
154
|
+
@mes.output_debug("Layer#debug 1 token.kind=#{token.kind} @layer.cur_state=#{@cur_layer.cur_state}")
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
module Mdextab
|
2
|
+
require "digest"
|
3
|
+
require "pp"
|
4
|
+
require "filex"
|
5
|
+
|
6
|
+
class Makemdtab
|
7
|
+
def initialize(opts, eruby_variable_str, eruby_static_str, obj_by_yaml, mes=nil)
|
8
|
+
@yamlfiles = {}
|
9
|
+
@str_yamlfiles = {}
|
10
|
+
@str_mdfiles = {}
|
11
|
+
@str_erubyfiles = {}
|
12
|
+
@dataop = opts["dataop"]
|
13
|
+
@datayamlfname = opts["data"]
|
14
|
+
@eruby_variable_str = eruby_variable_str
|
15
|
+
@eruby_static_str = eruby_static_str
|
16
|
+
@outputfname = opts["output"]
|
17
|
+
@obj_by_yaml = obj_by_yaml
|
18
|
+
|
19
|
+
@exit_cannot_find_file = 1
|
20
|
+
@exit_cannot_write_file = 2
|
21
|
+
|
22
|
+
@mes = mes
|
23
|
+
unless @mes
|
24
|
+
if opts["debug"]
|
25
|
+
@mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :debug)
|
26
|
+
elsif opts["verbose"]
|
27
|
+
@mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :verbose)
|
28
|
+
else
|
29
|
+
@mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
@mes.add_exitcode("EXIT_CODE_ILLEGAL_DATAOP")
|
33
|
+
Filex::Filex.setup(@mes)
|
34
|
+
|
35
|
+
@output = @mes.exc_file_write(@outputfname) { File.open(@outputfname, "w") }
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.create(opts, fname_variable, fname_static, root_settingfile, mes)
|
39
|
+
Filex::Filex.setup(mes)
|
40
|
+
|
41
|
+
unless File.exist?(opts["output"])
|
42
|
+
mes.output_fatal("Can't find #{opts['output']}")
|
43
|
+
exit(mes.ec("EXIT_CODE_CANNOT_FIND_FILE"))
|
44
|
+
end
|
45
|
+
obj_by_yaml = Filex::Filex.check_and_load_yamlfile(root_settingfile, mes)
|
46
|
+
|
47
|
+
str_variable = Filex::Filex.check_and_load_file(fname_variable, mes) if fname_variable
|
48
|
+
str_static = ["<% ", Filex::Filex.check_and_expand_file(fname_static, obj_by_yaml, mes), "%>"].join("\n") if fname_static
|
49
|
+
|
50
|
+
Makemdtab.new(opts, str_variable, str_static, obj_by_yaml, mes)
|
51
|
+
end
|
52
|
+
|
53
|
+
def make_md2(root_dir, templatefile=nil, auxhs={})
|
54
|
+
objx = @obj_by_yaml.merge(auxhs)
|
55
|
+
case @dataop
|
56
|
+
when :FILE_INCLUDE
|
57
|
+
array = load_file_include(root_dir, @datayamlfname, objx)
|
58
|
+
when :YAML_TO_MD
|
59
|
+
unless templatefile
|
60
|
+
@mes.output_fatal("Not specified templatefile")
|
61
|
+
exit(@mes.ec("EXIT_CODE_NOT_SPECIFIED_FILE"))
|
62
|
+
end
|
63
|
+
if templatefile.strip.empty?
|
64
|
+
@mes.output_fatal("Not specified templatefile")
|
65
|
+
exit(@mes.ec("EXIT_CODE_NOT_SPECIFIED_FILE"))
|
66
|
+
end
|
67
|
+
|
68
|
+
array = load_yaml_to_md(@datayamlfname, templatefile, objx)
|
69
|
+
else
|
70
|
+
array = []
|
71
|
+
end
|
72
|
+
array.map {|x| @mes.exc_file_write(@outputfname) { @output.puts(x) } }
|
73
|
+
end
|
74
|
+
|
75
|
+
def load_file_include(root_dir, datayamlfname, objx)
|
76
|
+
mdfname = datayamlfname
|
77
|
+
objy = { "parentDir" => "%q!" + root_dir + "!" }
|
78
|
+
eruby_exanpded_str = ""
|
79
|
+
if @eruby_variable_str
|
80
|
+
if @eruby_variable_str.empty?
|
81
|
+
eruby_exanpded_str = ""
|
82
|
+
else
|
83
|
+
eruby_exanpded_str = ["<% ", Filex::Filex.expand_str(@eruby_variable_str, objy, @mes), " %>"].join("\n")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
mbstr = Filex::Filex.check_and_load_file(mdfname, @mes)
|
87
|
+
dx = [eruby_exanpded_str, @eruby_static_str, mbstr].join("\n")
|
88
|
+
if dx.strip.empty?
|
89
|
+
puts "empty mdfname=#{mdfname}"
|
90
|
+
else
|
91
|
+
array = [Filex::Filex.expand_str(dx, objx, @mes, { "mdfname" => mdfname })]
|
92
|
+
end
|
93
|
+
|
94
|
+
array
|
95
|
+
end
|
96
|
+
|
97
|
+
def load_yaml_to_md(datayamlfname, templatefile, objx)
|
98
|
+
@mes.output_debug("datayamlfname=#{datayamlfname}")
|
99
|
+
@mes.output_debug("objx=#{objx}")
|
100
|
+
|
101
|
+
objy = Filex::Filex.check_and_expand_yamlfile(datayamlfname, objx, @mes)
|
102
|
+
@mes.output_debug("objy=#{objy}")
|
103
|
+
@mes.output_debug("templatefile=#{templatefile}")
|
104
|
+
|
105
|
+
erubystr = Filex::Filex.check_and_load_file(templatefile, @mes)
|
106
|
+
@mes.output_debug("erubystr=#{erubystr}")
|
107
|
+
dx = [@eruby_static_str, erubystr].join("\n")
|
108
|
+
@mes.output_debug("dx=#{dx}")
|
109
|
+
array = [Filex::Filex.expand_str(dx, objy, @mes, { "datayamlfname" => datayamlfname, "templatefile" => templatefile })]
|
110
|
+
|
111
|
+
array
|
112
|
+
end
|
113
|
+
|
114
|
+
def post_process
|
115
|
+
@mes.exc_file_close(@outputfname) { @output&.close }
|
116
|
+
@output = nil
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|