forked from Moutix/idmef_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_html.rb
124 lines (102 loc) · 3.57 KB
/
gen_html.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
require 'haml'
require 'yaml'
require_relative 'graph_generator'
require 'fileutils'
class HTMLGenerator
FOLDER = "templates"
FOOTER = File.read "#{FOLDER}/footer.html.haml"
BODY = File.read "#{FOLDER}/body.html.haml"
LAYOUT = File.read "#{FOLDER}/layout.html.haml"
HEAD = File.read "#{FOLDER}/head.html.haml"
def initialize
@conf = YAML.load(File.read("conf.yml"))
@generators = @conf["generators"].map{|k, g|
generator = GraphGenerator.new(
k,
g['classes'],
g['type']
)
[k, {
top_class: g["top_class"],
rfc: g["rfc"],
generator: generator,
graphes: generator.classes.map { |cls, _| [cls, generator.generate_graph(cls, "/#{@conf['location']}/#{k}")] }.to_h
}]}.to_h
end
def gen_site! folder="html"
FileUtils.rm_r folder if File.exist? folder
FileUtils.mkdir folder
FileUtils.cp_r(Dir.glob("#{FOLDER}/assets/*"), "#{folder}")
FileUtils.mkdir "#{folder}/img" unless File.exist? "#{folder}/img"
write_template folder, "index"
@generators.each do |name, generator|
FileUtils.mkdir "#{folder}/#{name}"
FileUtils.mkdir "#{folder}/img/#{name}"
generator[:generator].classes.each_key do |cls|
puts "Generate #{name}: #{cls} class"
generator[:graphes][cls].gen_all! "#{folder}/img/#{name}/#{cls}", name
File.open("#{folder}/#{name}/#{cls}.html", "w") do |f|
f.write(render_index cls, name)
end
end
File.open("#{folder}/#{name}/index.html", "w") do |f|
f.write(render_index generator[:top_class], name)
end
end
end
private
def write_template folder, template, dataset={}
File.open("#{folder}/#{template}.html", "w") do |f|
f.write(render_template template, dataset)
end
end
def render haml, dataset={}
return Haml::Engine.new(haml).render(Object.new, **dataset)
end
def render_footer
return render FOOTER, {generators: @generators,
location: @conf["location"]}
end
def render_head active_class=nil, generator=nil
return render HEAD, {title: @conf["title"],
generators: @generators,
active_class: active_class,
location: @conf["location"],
links: @conf["links"],
generator: generator}
end
def render_body active_class, generator
return render BODY, {active_class: @generators[generator][:generator].classes[active_class],
classes: @generators[generator][:generator].classes,
location: @conf["location"],
generator: generator,
graph: @generators[generator][:graphes][active_class]}
end
def render_index active_class, generator
dataset = {footer: render_footer,
head: render_head(active_class, generator),
body: render_body(active_class, generator),
title: @conf["title"],
location: @conf["location"],
generator: generator
}
return render LAYOUT, dataset
end
def render_template template, dataset
dataset.merge!({
title: @conf["title"],
location: @conf["location"],
generators: @generators
})
haml = File.read("#{FOLDER}/#{template}.html.haml")
return render(LAYOUT, {
footer: render_footer,
head: render_head,
body: render(haml, dataset),
title: @conf["title"],
location: @conf["location"],
generators: @generators
})
end
end
HTMLGenerator.new.gen_site!