forked from gamesensical/docs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_docs.rb
260 lines (214 loc) · 8.33 KB
/
build_docs.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
require "bundler/inline"
gemfile do
source "https://rubygems.org"
require "json"
require "pathname"
require "fileutils"
require "date"
gem "pry"
gem "activesupport", :require => ["active_support/core_ext/hash", "active_support/core_ext/string/inflections"]
gem "mustache"
end
class Template < Mustache
self.template_extension = "md"
def initialize(name, template_path: nil)
self.template_path = template_path.to_s unless template_path.nil?
self.template_name = name
end
end
class DocumentationFormat
def initialize(directory_name: "gitbook", build_dir: nil, src_dir: nil, docs_dir: nil)
@input_dir = src_dir + directory_name
@output_dir = build_dir + directory_name
@docs_dir = docs_dir || @output_dir
@globals_dir = @docs_dir + "globals"
@props_dir = @docs_dir + "netprops"
@template_dir = (@input_dir + ".templates").to_s
FileUtils.rm_r(Dir[@output_dir + "*"]) if @output_dir.directory?
@output_dir.mkpath
@props_dir.mkpath
FileUtils.cp_r(@input_dir, build_dir)
FileUtils.rm_r(@output_dir + ".templates") if (@output_dir + ".templates").directory?
@globals_template = Template.new("globals", template_path: @template_dir)
@netprops_class_template = Template.new("netprops_class", template_path: @template_dir)
@netprops_group_template = Template.new("netprops_group", template_path: @template_dir)
end
def build_global(name, data)
(@globals_dir + "#{name}.md").write(@globals_template.render(data).chomp)
end
def build_netprops_class(classname, data)
File.open("#{@props_dir}/#{classname}.md", "w").close
(@props_dir + "#{classname}.md").open("w+").write(@netprops_class_template.render(data))
end
def build_netprops_group(filename, data)
(@props_dir + "#{filename}.md").write(@netprops_group_template.render(data))
end
def build_events(data)
(@docs_dir + "development" + "events.md").write(Template.new("events", template_path: @template_dir).render(data))
end
def build_summary(data)
(@docs_dir + "SUMMARY.md").write(Template.new("summary", template_path: @template_dir).render(data))
end
end
SHORT_ARG_TYPES = true
BUILD_DIR = Pathname.new("build/")
SRC_DIR = Pathname.new("src/")
documentation_formats = []
documentation_formats << DocumentationFormat.new(directory_name: "gitbook", build_dir: BUILD_DIR, src_dir: SRC_DIR)
documentation_formats << DocumentationFormat.new(directory_name: "mkdocs", build_dir: BUILD_DIR, src_dir: SRC_DIR, docs_dir: BUILD_DIR + "mkdocs/docs")
globals = JSON.parse((BUILD_DIR + "globals.json").read)
extra_docs = JSON.parse((SRC_DIR + "extra_docs.json").read)
events = JSON.parse((SRC_DIR + "events.json").read)
globals_descriptions = extra_docs["globals_descriptions"]
globals_examples = extra_docs["globals_examples"]
globals.sort.to_h.map do |global, functions|
functions_list = functions.sort_by{|name, data| (data.key?("name") && data["name"].include?(":")) ? "\xFF#{name}" : name}.map do |name, function|
# name
function["name"] ||= "#{global}.#{name}"
function["display_name"] = function["name"].include?(":") ? (":" + function["name"].split(":").last) : function["name"]
# args text
arg_names = ""
if function.key? "args"
function["args"].each_with_index do |arg, i|
arg_text = arg["name"]
arg_text += ": #{SHORT_ARG_TYPES ? arg["type"].split(" ")[0] : arg["type"]}" if arg.key? "type"
arg_text = i == 0 ? "#{arg_text}" : ", #{arg_text}"
arg_names << (arg["optional"] ? ("[#{arg_text}]#{(i == function["args"].length-1) ? "" : " "}") : arg_text)
end
end
function["args_text"] = arg_names
# args
function["args"] = {list: function["args"]} unless function["args"].empty?
function
end
data = {
description: globals_descriptions[global],
functions: functions_list,
global: global,
examples: globals_examples.key?(global) ? {list: globals_examples[global]} : nil
}
documentation_formats.each do |documentation_format|
documentation_format.build_global(global, data)
end
end
# Generate netprops
classes = Hash.new
classes_type = Hash.new
current_class = nil
current_table = nil
current_table_type = nil
first_line = nil
current_table_nums = []
number_regex = /^[0-9]*$/
(SRC_DIR + "props.txt").open("r").each do |line|
first_line ||= line
next if line == "" || line.start_with?("//")
unless line.start_with? " "
current_class = line.split(" ")[0]
classes[current_class] = []
classes_type[current_class] = line.split("(type ")[1].split(")")[0] rescue nil
next
end
cleaned = line.gsub(" ", "")
current_table_new = nil
table_finished = true
if cleaned.start_with? "Member:"
prop = line.split("Member: ")[1].split(" (offset ")[0]
type = line.split("(type ")[1].split(")")[0]
if number_regex.match? prop
current_table_type = type if current_table_type.nil?
current_table_nums << prop.to_i
table_finished = false
else
classes[current_class] << {"name" => prop, "type" => type} unless number_regex.match? current_table
end
elsif cleaned.start_with? "Table:"
current_table_new = line.split("Table: ")[1].split(" (offset ")[0]
end
if table_finished && !current_table.nil? && current_table_nums.length > 0
classes[current_class].insert(classes[current_class].length > 1 ? -2 : -1, {"name" => current_table, "type" => "#{current_table_type}[#{current_table_nums.min == current_table_nums.max ? current_table_nums.min.to_s : "#{current_table_nums.min}-#{current_table_nums.max}"}]"})
current_table = nil
current_table_type = nil
current_table_nums = []
end
current_table = current_table_new if !current_table_new.nil?
end
def get_group(classname)
weapon_classnames = ["CAK47", "CDEagle", "CFists", "CFlashbang", "CKnife", "CKnifeGG", "CMelee", "CC4", "CSCAR17", "CTablet", "CBreachCharge", "CBumpMine", "CSnowball", "CEconEntity"]
important_classnames = ["CCSPlayer", "CCSPlayerResource", "CCSGameRulesProxy"]
return "Important" if important_classnames.include? classname
return "Temp Entities" if classname.start_with? "CTE"
return "Projectiles" if classname.include?("Projectile")
return "Items" if weapon_classnames.include?(classname) || classname.start_with?("CWeapon") || classname.include?("Grenade") || classname.include?("CItem")
return "Base Entities" if classname.start_with?("CBase")
return "Environment" if classname.include?("CColorCorrection") || classname.include?("CSun") || classname.start_with?("CEnv")
return "Controllers" if classname.include?("Control") || classname.include?("CTeam")
return "Other"
end
netprops_groups = {
"Important" => [],
"Items" => [],
"Projectiles" => [],
"Environment" => [],
"Controllers" => [],
"Temp Entities" => [],
"Base Entities" => [],
"Other" => [],
}
netprops_class_template = Template.new("netprops_class")
classes.each do |classname, props|
next if classname.nil?
data = {
type: classes_type[classname],
classname: classname,
props: props
}
documentation_formats.each do |documentation_format|
documentation_format.build_netprops_class(classname, data)
end
group = get_group(classname)
netprops_groups[group] ||= []
netprops_groups[group] << classname
end
group_filename = Hash[netprops_groups.map{|a, b| [a, a.parameterize.gsub("-", "")]}]
date = Date.parse(first_line.split(" ").last).strftime("%d.%m.%Y")
netprops_groups.each do |group, classnames|
data = {
group: group,
last_updated: date,
classnames: classnames
}
documentation_formats.each do |documentation_format|
documentation_format.build_netprops_group(group_filename[group], data)
end
end
# Write event list
events_data = {
events: events["events"].map do |name, data|
data["name"] = name
if data.dig("properties", 0).is_a? String
data["string_properties"] = data["properties"].map.with_index {|prop, i| {index: i+1, prop: prop}}
data["has_string_properties"] = true
elsif data.key? "properties"
data["table_properties"] = data["properties"].map{|table| table.map{|key, description| {key: key, description: description}}}
data["has_table_properties"] = true
end
data["has_examples"] = !data.dig("examples", 0).nil?
data
end
}
documentation_formats.each do |documentation_format|
documentation_format.build_events(events_data)
end
# Write formatted list of globals and netprops to SUMMARY.md
summary_data = {
globals: globals.keys,
netprops: netprops_groups.map{|group, classnames| {
group: group,
group_filename: group_filename[group],
classnames: classnames
}}
}
documentation_formats.each do |documentation_format|
documentation_format.build_summary(summary_data)
end