-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapi_generator.rb
498 lines (412 loc) · 15.6 KB
/
api_generator.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# For now this plugin is designed to work only with Kendo UI for jQuery
require 'kramdown'
module Jekyll
String.class_eval do
def indent(count, char = ' ')
gsub(/([^\n]*)(\n|$)/) do |match|
last_iteration = ($1 == "" && $2 == "")
line = ""
line << (char * count) unless last_iteration
line << $1
line << $2
line
end
end
end
class ApiPage < Page
@@header_api_marker = '<div class="api-article"></div>' + "\n\n"
def initialize(site, base, dir, page_schema)
@site = site
@base = base
@dir = File.join(dir, page_schema.category.downcase)
@name = page_schema.name.downcase + ".md"
self.process(@name)
self.content = @@header_api_marker + page_schema.content
self.data = page_schema.data
self.data['category'] = "default"
self.data['sub_category'] = page_schema.category
self.data['res_type'] = "api"
self.data['layout'] = "api-index"
self.data['parent_path'] = dir
self.data['is_api'] = true
self.data['publish'] = false
self.data.delete('previous_url')
end
end
class ApiPageGenerator < Jekyll::Generator
priority :high
safe true
def generate(site)
if site.config['has_web_api'] == true
api_groups = Array.new
site.config['api'].each_entry do |element|
children = Array.new
element['group_parents'].each_entry do |child|
children << child
end
api_groups << { "control" => element['file'], "group_parents" => children, "categories" => {} }
end
api_sub_pages = Array.new
site.pages.each_entry do |page|
if page.path.include? "api/javascript/effects/"
next
end
if page.path.include? "api/javascript"
dir = page.path
dir.sub!(".md", "")
api_group = api_groups.detect { |g| g["control"] === dir }
if api_group == nil
api_group = { "control" => dir, "group_parents" => Array.new, "categories" => {} }
api_groups << api_group
end
api_group["public_name"] = page['title']
pages = ApiPageParser.new(page.clone, api_group["group_parents"]).parse_pages()
page.data['is_main'] = true
page.data['is_api'] = true
new_content = ""
last_category =""
pages.each_entry do |page_schema|
api_sub_pages << ApiPage.new(site, site.source, dir, page_schema)
if new_content === ""
new_content += page_schema.main_page_header
new_content+= '<div id="api-filter">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 16 16" enable-background="new 0 0 16 16" xml:space="preserve"><path fill="#FFFFFF" d="M6.499,1.985C3.461,1.985,1,4.449,1,7.484c0,3.039,2.461,5.5,5.499,5.5c3.037,0,5.498-2.461,5.498-5.5 C11.996,4.449,9.535,1.985,6.499,1.985z M6.499,11.885c-2.429,0-4.399-1.97-4.399-4.401c0-2.429,1.97-4.399,4.399-4.399 c2.429,0,4.399,1.97,4.399,4.399C10.897,9.915,8.928,11.885,6.499,11.885z M13.828,13.746c0,1.179,0.109,1.067-1.069,1.067 l-1.678-1.678c0-1.177-0.109-1.067,1.069-1.067L13.828,13.746z"/></svg>
<input type="search" class="search" placeholder="Filter..." />
</div>'
end
if last_category != page_schema.category
last_category = page_schema.category
new_content += "\n## #{last_category}\n"
end
new_content += "#{get_link_for_sub_page(page, page_schema)}\n"
add_pages(api_group, page_schema)
end
page.content = new_content
end
end
api_sub_pages.each_entry do |new_page|
site.pages << new_page
end
export_groups_json(site, api_groups)
end
end
def add_pages(api_group, page_schema)
if api_group["categories"][page_schema.category] == nil
api_group["categories"][page_schema.category] = Array.new
end
api_group["categories"][page_schema.category] << page_schema.name
child_page_names = page_schema.data['group_page_names'];
if child_page_names
child_page_names.each do |child_page_name|
api_group["categories"][page_schema.category] << child_page_name
end
end
end
def export_groups_json(site, groups)
filename = "api.json"
FileUtils.mkdir_p(site.dest) unless File.exist?(site.dest)
File.write(File.join(site.dest, filename), groups.to_json)
site.keep_files << filename
end
def get_link_for_sub_page(page, sub_page_schema)
text = sub_page_schema.name
page_link = page.path.split('/').last
page_link.sub!(".md", "")
link = "* [#{text}](#{page_link.downcase}/#{sub_page_schema.category.downcase}/#{text.downcase})"
child_page_names = sub_page_schema.data['group_page_names'];
if child_page_names
child_page_names.each do |child_page_name|
link += "\n* [#{child_page_name}](#{page_link.downcase}/#{sub_page_schema.category.downcase}/#{text.downcase}##{child_page_name})"
end
end
link
end
end
class ApiPageSchema
attr_accessor :name, :content, :category, :data, :main_page_header
def initialize(name, content, category, data, main_page_header)
self.name = name
self.content = content
self.category = category
self.data = data
self.main_page_header = main_page_header
end
end
class ApiPageParser
@@known_types = [:codeblock, :p, :a, :header, :blank, :codespan, :blockquote, :ul, :li, :strong, :html_element]
@@block_containers = [:blockquote, :ul, :li, :p, :strong ]
@@categories = ['Constructor Parameters', 'Configuration', 'Fields', 'Properties', 'Methods', 'Class Methods', 'Events']
def initialize(page, groups)
name = page.name
@name = name.sub!(page.ext, "")
@data = page.data.clone
@markdown = page.content
@groups = groups
end
def parse_pages()
@pages = parse(@markdown)
end
def parse(markdown)
@pages = Array.new
root = Kramdown::Parser::Markdown.parse(markdown)[0]
header = parse_main_page_header(root)
@@categories.each_entry do |category|
build_pages_for_category(root, category, header)
end
return @pages;
end
def parse_element(element)
description = ""
if element.type === :codeblock
description += "\n#{element_value(element).indent(4)}"
end
if element.type === :html_element
description += "\n#{KramdownExtensions::KramdownHtmlConverter.convert(element).indent(4)}"
end
if element.type === :blank
description += element_text(element) * 2
end
if element.type === :header
description += element_header(element, element.options[:level])
end
if is_block_container_element(element.type)
description += element_block_container(element)
end
return description
end
def is_group_start(page_name)
@groups.include? page_name
end
def build_pages_for_category(root, category, header)
values = {}
category_content = get_category_content(root, category)
page_schemas = Array.new
group_parents = Array.new
each_section(category_content) do |element, index|
if group_parents.length == 0
page_name = parse_page_name(element).strip
body = read(element, index, category_content, 0)
data = @data.clone
data['page_title'] = get_page_title(page_name)
data['title'] = page_name
data['description'] = get_page_description(page_name, category)
page_schemas << ApiPageSchema.new(page_name, body, category, data, header)
if is_group_start(page_name)
page_schemas.last.data['group_page_names'] = Array.new
group_parents << page_schemas.last
next element
else
prepare_links_for_page(values, page_name, "")
end
end
if group_parents.length > 0
page_name = parse_page_name(element).strip
if is_group_start(page_name)
body = read(element, index, category_content, 2)
data = @data.clone
data['page_title'] = get_page_title(page_name)
data['title'] = page_name
data['description'] = get_page_description(page_name, category)
data['group_page_names'] = Array.new
page_schemas << ApiPageSchema.new(page_name, body, category, data, header)
group_parents << page_schemas.last
next element
end
current_group_page = group_parents.last
if page_name.include? current_group_page.name
# Continue group
current_group_page.content += read(element, index, category_content, 2)
current_group_page.data['group_page_names'] << page_name
next element
else
# End group
current_group_page = group_parents.pop
current_group_page.data['is_api_overview'] = true
prepare_links_for_page(values, current_group_page.name, current_group_page.data['group_page_names'])
# New ungrouped page
body = read(element, index, category_content, 0)
data = @data.clone
data['page_title'] = get_page_title(page_name)
data['title'] = page_name
data['description'] = get_page_description(page_name, category)
prepare_links_for_page(values, page_name, "")
page_schemas << ApiPageSchema.new(page_name, body, category, data, header)
end
end
end
add_links(page_schemas, values) do |p|
@pages << p
end
end
def read(element, index, category_content, leading_newlines_count)
content = "\n" * leading_newlines_count
content += "### #{element_raw(element)}\n#{parse_page_body(index, category_content)}"
content
end
def parse_page_body(index, siblings)
text = ""
siblings.slice(index + 1, siblings.size).each do |element|
break if element.type == :header && element.options[:level] <= 3
# uncomment to debug
# raise "#{element.type}" unless @@known_types.include?(element.type)
# puts("inspect: #{element.inspect}")
text += parse_element(element)
# end
end
text.strip
end
def parse_main_page_header(root)
header_content = ""
root.children.each_entry do |child|
break if child.type == :header && child.options[:level] > 1
header_content += parse_element(child)
end
return header_content
end
def get_category_content(element, category)
start_index = child_index(element, category)
end_index = -1
@@categories.each_entry do |categ|
end_index = child_index(element, categ)
break if end_index > start_index
end
if start_index <= end_index
end_index = element.children.size
end
element.children.slice(start_index..end_index)
end
def get_page_title(name)
return "#{name} - API Reference - Kendo UI #{@data['title']}"
end
def get_page_description(name, category)
categ = category.downcase
specific_description = ''
if categ == 'methods' || categ == 'class methods'
specific_description = "use the #{name} method"
elsif categ == 'properties' || categ == 'fields' || categ == 'configuration'
specific_description = "configure the #{name} property"
elsif categ == 'events'
specific_description = "use the #{name} event"
elsif categ == 'constructor parameters'
specific_description = "use the #{name} parameter of the constructor"
else
specific_description = "use the #{name}"
end
return "In this article you can see how to #{specific_description} of the Kendo UI #{@data['title']}.";
end
def child_index(element, text)
index = element.children.find_index {|e| e.options[:raw_text] == text}
index = element.children.size unless index
index
end
def prepare_links_for_page(values, page_name, children)
page_name_split = page_name.split(".")
previous_key = ""
page_name_split.each_entry do |key|
if previous_key != ""
previous_key += ".#{key}"
elsif
previous_key = key
end
if values[previous_key] == nil
values[previous_key] = Array.new
end
if previous_key != page_name
values[previous_key] << page_name
if children != ""
children.each do |child_page_name|
values[previous_key] << page_name + "#" + child_page_name
end
end
end
end
end
def add_links(pages, values)
pages.each_entry do |page|
links = values[page.name]
if links != nil
links.uniq.each_with_index do |link, index|
add_link(page, link, index)
# puts("page: #{page.name}; link: #{link}")
end
end
yield page
end
end
def add_link(page, link, index)
if index === 0
page.content += "\n\n### Related Properties\n"
end
page.content += "* [#{link.rpartition("#")[2]}](#{link.downcase})\n"
end
def each_section(section)
section.each_with_index do |element, index|
next if index == 0
break if element.type == :header && element.options[:level] < 3
if element.type == :header && element.options[:level] == 3
yield element, index
end
end
end
def is_block_container_element(element_type)
return @@block_containers.include?(element_type)
end
def element_block_container(element)
element_value = ""
if element.type === :blockquote
element_value += "> "
elsif element.type === :li
element_value += "* "
end
element.children.each_entry do |child|
if child.type === :codespan
element_value += element_codespan(child)
elsif child.type === :a
element_value += element_link(child)
elsif is_block_container_element(child.type)
element_value += parse_element(child)
elsif
element_value += element_text(child)
end
end
if element.type == :li
element_value += "\n"
end
return element_value
end
def find_text_child(element)
element.children.find {|e| e.type == :text } if element
end
def element_text(element)
if element.children.any?
element.children.map { |child| element_text(child) }.join
elsif element.value.is_a? Kramdown::Utils::Entities::Entity
element.value.char()
elsif element.value
element.value
else
""
end
end
def element_link(element)
url = element.attr['href']
text = element_text(element)
element_value = "[#{text}](#{url})"
end
def element_codespan(element)
element_value = "```#{element_text(element)}```"
end
def element_header(element, level)
element_value = "#" * level + " #{element_raw(element)}"
end
def element_raw(element)
element_value = element.options[:raw_text] if element
end
def element_value(element)
element.value if element
end
def parse_page_name(element)
element_value find_text_child(element)
end
end
end