-
Notifications
You must be signed in to change notification settings - Fork 97
/
collector.rb
169 lines (137 loc) · 4.71 KB
/
collector.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
require 'redcarpet'
require 'json'
class HTMLWithPants < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
end
class Collector
def initialize(source_path, target_path)
@source_path = source_path
@target_path = target_path
end
def collect
get_files
collect_sources if !@target_file || source_is_newer?
end
private
def get_files
@target_file = Dir.glob(@target_path)
@target_file = !@target_file.empty? ? @target_file[0] : nil
raise "Collector: Targetfile (#{@target_file}) is not writable" if @target_file && !File.writable?(@target_file)
@sources = Dir.glob(@source_path)
# sort by numerical index
@sources = @sources.sort_by do |f|
f.scan(/\d/).join.to_i
end
end
end
def source_is_newer?
target_time = File.mtime(@target_file)
@sources.each do |f|
return true if File.mtime(f) > target_time
end
return false
end
def collect_sources
return if @sources.empty?
#@markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {})
@markdown = Redcarpet::Markdown.new(HTMLWithPants, extensions = {})
# Open all source files, parse and convert to json
# Generating a new sequence of id's starting with one
# This allows for easy renumbering of source files
output = {}
@sources.each_with_index do |f, idx|
output[idx + 1] = read_parse_file(f, idx + 1)
end
# Encode output as a JSON file
output = JSON.generate(output)
# Add new lines between examples - this won't break examples as they
# can't contain unescaped quotes
output.gsub!('},"', %/},\n"/)
# Overwrite output file
File.open(@target_path, 'w') do |f|
f.write(output)
end
# Report back
puts "Collector: converted #{@target_path}"
end
def read_parse_file(filename, step)
lang = ''
#step = filename.scan(/\d/).join.to_i
title = ''
chapter = 'N'
answer = ''
ok = ''
error = ''
text = ''
load_code = ''
dashline_count = 0
IO.readlines(filename).each do |line|
if dashline_count < 2
line.match('---') { |m|
dashline_count += 1
}
line.match(/^lang:\s+(.*)/) { |m|
lang = m[1].strip
}
line.match(/^title:\s+(.*)/) { |m|
title = m[1].strip
}
line.match(/^class:(.*)chapmark$/) { |m|
chapter = 'Y'
}
line.match(/^answer:\s+(.*)/) { |m|
answer = m[1].strip
}
line.match(/^ok:\s+(.*)/) { |m|
ok = m[1].strip
}
line.match(/^error:\s+(.*)/) { |m|
error = m[1].strip
}
line.match(/^load:\s+(.*)/) { |m|
load_code = m[1].strip
}
else
text += line
end
end
text.strip!
return convert(lang, step, title, chapter, answer, ok, error, text, load_code)
end
def convert(lang, step, title, chapter, answer, ok, error, text, load_code)
{
lang: lang,
title: title,
chapter: chapter,
answer: answer,
ok: ok,
error: error,
text: @markdown.render(text),
load_code: load_code
}
end
end
# Attach Collector to Middleman
class MiddlemanCollector < Middleman::Extension
def initialize(app, options_hash={}, &block)
super
app.before do
Collector.new('translations/en/try_ruby_*.md', 'source/try_ruby_en.json').collect
Collector.new('translations/nl/try_ruby_*.md', 'source/try_ruby_nl.json').collect
Collector.new('translations/es/try_ruby_*.md', 'source/try_ruby_es.json').collect
Collector.new('translations/pt-br/try_ruby_*.md', 'source/try_ruby_pt-br.json').collect
Collector.new('translations/ja/try_ruby_*.md', 'source/try_ruby_ja.json').collect
Collector.new('translations/ru/try_ruby_*.md', 'source/try_ruby_ru.json').collect
Collector.new('translations/ua/try_ruby_*.md', 'source/try_ruby_ua.json').collect
Collector.new('translations/mk/try_ruby_*.md', 'source/try_ruby_mk.json').collect
Collector.new('translations/zh/try_ruby_*.md', 'source/try_ruby_zh.json').collect
Collector.new('translations/de/try_ruby_*.md', 'source/try_ruby_de.json').collect
Collector.new('translations/tr/try_ruby_*.md', 'source/try_ruby_tr.json').collect
Collector.new('translations/fr/try_ruby_*.md', 'source/try_ruby_fr.json').collect
# TODO: add any new translations here
true
end
end
end
::Middleman::Extensions.register(:collector, MiddlemanCollector)