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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
# coding: utf-8
# Copyright 2019 DragonRuby LLC
# MIT License
# docs.rb has been released under MIT (*only this file*).
module DocsOrganizer
def self.sort_docs_classes!
$docs_classes.sort! do |l, r|
l_index = (class_sort_order.find_index l) || 50000
r_index = (class_sort_order.find_index r) || 50000
l_index = 51000 if l == :docs_classes
r_index = 51000 if r == :docs_classes
l_index <=> r_index
end
end
def self.reserved_methods
[
:docs_export_docs!,
:docs_all,
:docs_method_sort_order,
:docs_classes,
:docs_search
]
end
def self.class_sort_order
[
GTK::ReadMe,
GTK::Runtime,
Array,
GTK::Args,
GTK::Outputs,
GTK::Mouse,
GTK::OpenEntity,
Numeric,
Kernel,
]
end
def self.check_class_sort_order
unsorted = $docs_classes.find_all do |klass|
!class_sort_order.include? klass
end
unsorted.each do |k|
puts <<-S
* WARNING: #{klass.name} is not included in DocsOrganizer::class_sort_order. Please place this
module in it's correct topilogical order.
S
end
if unsorted.length == 0
puts <<-S
* INFO: Success. All documented classes have a sort order associated with them.
S
end
end
def self.sort_method_delegate l, r, method_sort_order
l_index = (method_sort_order.find_index l) || 50000
r_index = (method_sort_order.find_index r) || 50000
l_index = 51000 if l == :docs_classes
r_index = 51000 if r == :docs_classes
l_index = -51000 if l == :docs_class
r_index = -51000 if r == :docs_class
l_index <=> r_index
end
def self.find_methods_with_docs klass
klass_method_sort_order = klass.docs_method_sort_order
klass.methods.find_all { |m| m.start_with? 'docs_' }
.reject { |m| reserved_methods.include? m }
.sort do |l, r|
sort_method_delegate l, r, klass_method_sort_order
end
end
end
module Docs
def self.extended klass
$docs_classes ||= []
$docs_classes << klass
$docs_classes.uniq!
end
def docs_method_sort_order
[]
end
def docs_classes
DocsOrganizer.sort_docs_classes!
list = $docs_classes.map { |mod| "** #{mod.name}.docs" }.join "\n"
<<-S
* DOCS:
Here are the classes that have documentation. You can call the .docs method
on any of these classes:
#{list}
S
end
def docs_all
docs_methods = DocsOrganizer.find_methods_with_docs(self).map { |d| send d }.join "\n"
<<-S
#{docs_methods}
S
end
def docs
docs_methods = [DocsOrganizer.find_methods_with_docs(self), :docs_classes].flatten.map { |d| "** #{self.name}.#{d}" }.join "\n"
if self == Kernel
<<-S
* DOCS: #{self.name}
Some Classes in Game Toolkit have a method called docs. You can invoke this
method interactively to see information about functions within the engine.
For example, invoking ~Kernel.docs_tick_count~ will give you documentation
for the Kernel.tick_count method.
To export all documentation you can use ~Kernel.export_docs!~ (or just ~export_docs!~).
To search docs you can use Kernel.docs_search (or just `docs_search`) by providing it a search term.
For example:
#+begin_src
docs_search "array find remove nil"
#+end_src
You can do more advanced searches by providing a block:
#+begin_src
docs_search do |entry|
(entry.include? "Array") && (!entry.include? "Enumerable")
end
#+end_src
#{docs_methods}
** NOTE: Invoke any of the methods above on #{self.name} to see detailed documentation.
** NOTE: Calling the docs_classes method will give you all classes in Game Toolkit that contain docs.
S
else
<<-S
* DOCS: #{self.name}
#{docs_methods}
S
end
end
def self.__docs_search__ words = nil, &block
end
def __docs_search_help_text__
<<-S
* DOCS: How To Search The Docs
To search docs you can use Kernel.docs_search (or just ~docs_search~) by providing it a search term.
For example:
#+begin_src
docs_search "array find remove nil"
#+end_src
You can do more advanced searches by providing a block:
#+begin_src
docs_search do |entry|
(entry.include? "Array") && (!entry.include? "Enumerable")
end
#+end_src
S
end
def __docs_search_results__ words = nil, &block
words ||= ""
if words.strip.length != 0
each_word = words.split(' ').find_all { |w| w.strip.length > 3 }
block = lambda do |entry|
each_word.any? { |w| entry.downcase.include? w.downcase }
end
end
return [__docs_search_help_text__] if !block
DocsOrganizer.sort_docs_classes!
this_block = block
search_results = []
if self == Kernel
$docs_classes.each do |k|
DocsOrganizer.find_methods_with_docs(k).each do |m|
s = k.send m
search_results << s if block.call s
end
end
else
DocsOrganizer.find_methods_with_docs(self).each do |m|
s = send m
search_results << s if block.call s
end
end
search_results
end
def docs_search words = nil, &block
results = __docs_search_results__ words, &block
final_string = results.join "\n"
final_string = "* DOCS: No results found." if final_string.strip.length == 0
$gtk.write_file_root "docs/search_results.txt", final_string
if !final_string.include? "* DOCS: No results found."
log "* INFO: Search results have been written to docs/search_results.txt."
end
"\n" + final_string
end
def __export_docs__! opts = {}
DocsOrganizer.sort_docs_classes!
opts = defaults_export_docs!.merge opts
opts[:methods] = methods_with_docs.reject { |m| m == :docs_classes } if opts[:methods].include? :all
content = opts[:methods].map do |m|
puts "* INFO: Getting docs for #{m}."
(send m).ltrim + "\n"
end.join "\n"
file_path = "docs/#{self.name}.txt"
$gtk.write_file_root "#{file_path}", content
puts "* INFO: Documentation for #{self.name} has been exported to #{file_path}."
$gtk.console.set_system_command file_path
nil
end
def export_docs! opts = {}
__export_docs__! opts
end
def __docs_append_true_line__ true_lines, true_line, parse_log
true_line.rstrip!
parse_log << "*** True Line Result\n#{true_line}"
true_lines << true_line
end
# may god have mercy on your soul if you try to expand this
def __docs_to_html__ string
parse_log = []
html_start_to_toc_start = <<-S
<html>
<head>
<title>DragonRuby Game Toolkit Documentation</title>
<link href="docs.css?ver=#{Time.now.to_i}" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div id='toc'>
S
html_toc_end_to_content_start = <<-S
</div>
<div id='content'>
S
html_content_end_to_html_end = <<-S
</div>
</body>
</html>
S
true_lines = []
current_true_line = ""
inside_source = false
inside_ordered_list = false
inside_unordered_list = false
# PARSE TRUE LINES
parse_log << "* Processing True Lines"
string.strip.each_line do |l|
parse_log << "** Processing line: ~#{l.rstrip}~"
if l.start_with? "#+begin_src"
parse_log << "- Line was identified as the beginning of a code block."
inside_source = true
__docs_append_true_line__ true_lines, current_true_line, parse_log
__docs_append_true_line__ true_lines, l, parse_log
elsif l.start_with? "#+end_src"
parse_log << "- Line was identified as the end of a code block."
inside_source = false
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
elsif l.start_with? "#+"
parse_log << "- Line was identified as a literal block."
__docs_append_true_line__ true_lines, current_true_line, parse_log
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
elsif l.start_with? "- "
parse_log << "- Line was identified as a list."
inside_unordered_list = true
__docs_append_true_line__ true_lines, current_true_line, parse_log
current_true_line = l
elsif l.start_with? "1. "
parse_log << "- Line was identified as a start of a list."
inside_ordered_list = true
__docs_append_true_line__ true_lines, current_true_line, parse_log
current_true_line = l
elsif inside_ordered_list && (l[1] == "." || l[2] == "." || l[3] == ".")
parse_log << "- Line was identified as a continuation of a list."
__docs_append_true_line__ true_lines, current_true_line, parse_log
current_true_line = l
elsif inside_source
parse_log << "- Inside source: true"
inside_source = true
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
elsif l.strip.length == 0
parse_log << "- End of paragraph detected."
inside_ordered_list = false
inside_unordered_list = false
__docs_append_true_line__ true_lines, current_true_line, parse_log
current_true_line = ""
elsif l.start_with? "* "
parse_log << "- Header detected."
__docs_append_true_line__ true_lines, current_true_line, parse_log
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
elsif l.start_with? "** "
parse_log << "- Header detected."
__docs_append_true_line__ true_lines, current_true_line, parse_log
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
elsif l.start_with? "*** "
parse_log << "- Header detected."
__docs_append_true_line__ true_lines, current_true_line, parse_log
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
elsif l.start_with? "**** "
parse_log << "- Header detected."
__docs_append_true_line__ true_lines, current_true_line, parse_log
__docs_append_true_line__ true_lines, l, parse_log
current_true_line = ""
else
current_true_line += l.rstrip + " "
end
end
true_lines << current_true_line if current_true_line.length != 0
if true_lines[0].strip == ""
true_lines = true_lines[1..-1]
end
toc_html = ""
content_html = ""
inside_pre = false
inside_being_src = false
inside_paragraph = false
inside_literal = false
inside_h1 = false
inside_ordered_list = false
inside_ul = false
inside_ol = false
text_to_id = lambda do |text|
text = text.strip.downcase
text = text.gsub("*", "-")
text = text.gsub("~", "-")
text = text.gsub("[", "-")
text = text.gsub("]", "-")
text = text.gsub(":", "-")
text = text.gsub(" ", "-")
text
end
close_list_if_needed = lambda do |inside_ul, inside_ol|
begin
result = ""
if inside_ul
result = "</ul>\n"
elsif inside_ol
result = "</ol>\n"
else
result
end
rescue Exception => e
raise "* ERROR in determining close_list_if_needed lambda result. #{e}."
end
end
inside_ol = false
inside_ul = false
toc_html = "<h1>Table Of Contents</h1>\n<ul>\n"
parse_log << "* Processing Html Given True Lines"
true_lines.each do |l|
parse_log << "** Processing line: ~#{l.rstrip}~"
if l.start_with? "* "
parse_log << "- H1 detected."
content_html += close_list_if_needed.call inside_ul, inside_ol
inside_ol = false
inside_ul = false
formatted_html = __docs_line_to_html__ l, parse_log
link_id = text_to_id.call l
toc_html += "<li><a class='header-1' href='##{link_id}'>#{formatted_html}</a></li>\n"
content_html += "<h1 id='#{link_id}'>#{formatted_html}</h1>\n"
elsif l.start_with? "** "
parse_log << "- H2 detected."
content_html += close_list_if_needed.call inside_ul, inside_ol
inside_ol = false
inside_ul = false
formatted_html = __docs_line_to_html__ l, parse_log
link_id = text_to_id.call l
toc_html += "<ul><li><a class='header-2' href='##{link_id}'>#{formatted_html}</a></li></ul>"
content_html += "<h2 id='#{link_id}'>#{__docs_line_to_html__ l, parse_log}</h2>\n"
elsif l.start_with? "*** "
parse_log << "- H3 detected."
content_html += close_list_if_needed.call inside_ul, inside_ol
inside_ol = false
inside_ul = false
formatted_html = __docs_line_to_html__ l, parse_log
link_id = text_to_id.call l
toc_html += "<ul><ul><li><a class='header-3' href='##{link_id}'>#{formatted_html}</a></li></ul></ul>"
content_html += "<h3 id='#{link_id}'>#{__docs_line_to_html__ l, parse_log}</h3>\n"
elsif l.start_with? "**** "
parse_log << "- H4 detected."
content_html += close_list_if_needed.call inside_ul, inside_ol
inside_ol = false
inside_ul = false
formatted_html = __docs_line_to_html__ l, parse_log
link_id = text_to_id.call l
# toc_html += "<ul><ul><ul><li><a href='##{link_id}'>#{formatted_html}</a></li></ul></ul></ul>"
content_html += "<h4>#{__docs_line_to_html__ l, parse_log}</h4>\n"
elsif l.strip.length == 0 && !inside_pre
# do nothing
elsif l.start_with? "#+begin_src"
parse_log << "- PRE start detected."
content_html += close_list_if_needed.call inside_ul, inside_ol
inside_ol = false
inside_ul = false
inside_pre = true
content_html << '<pre><code class="language-ruby">'
elsif l.start_with? "#+end_src"
parse_log << "- PRE end detected."
inside_ol = false
inside_ul = false
inside_pre = false
content_html << "</code></pre>\n"
elsif l.start_with? "#+begin_quote"
parse_log << "- BLOCKQUOTE start detected."
content_html += close_list_if_needed.call inside_ul, inside_ol
inside_ol = false
inside_ul = false
content_html << "<blockquote>\n"
elsif l.start_with? "#+end_quote"
parse_log << "- BLOCKQUOTE end detected."
content_html << "</blockquote>\n"
elsif (l.start_with? "1. ") && !inside_ol
parse_log << "- OL start detected."
parse_log << "- LI detected."
inside_ol = true
content_html << "<ol>\n"
if l.split(".")[0].length == 1
l = l[2..-1]
elsif l.split(".")[0].length == 2
l = l[3..-1]
elsif l.split(".")[0].length == 3
l = l[4..-1]
end
content_html << "<li>#{__docs_line_to_html__ l, parse_log}</li>\n"
elsif inside_ol && (l[1] == "." || l[2] == "." || l[3] == ".")
parse_log << "- LI detected."
if l.split(".")[0].length == 1
l = l[2..-1]
elsif l.split(".")[0].length == 2
l = l[3..-1]
elsif l.split(".")[0].length == 3
l = l[4..-1]
elsif l.split(".")[0].length == 4
l = l[5..-1]
end
content_html << "<li>#{__docs_line_to_html__ l, parse_log}</li>\n"
elsif (l.start_with? "- ") && !inside_ul
parse_log << "- UL start detected."
parse_log << "- LI detected."
inside_ul = true
content_html << "<ul>\n"
l = l[2..-1]
content_html << "<li>#{__docs_line_to_html__ l, parse_log}</li>\n"
elsif (l.start_with? "- ") && inside_ul
parse_log << "- LI detected."
l = l[2..-1]
content_html << "<li>#{__docs_line_to_html__ l, parse_log}</li>\n"
else
if inside_ul
parse_log << "- UL end detected."
inside_ul = false
content_html << "</ul>\n"
end
if inside_ol
parse_log << "- OL end detected."
inside_ol = false
content_html << "</ol>\n"
end
if inside_pre
content_html << "#{l.rstrip[2..-1]}\n"
else
parse_log << "- P detected."
content_html << "<p>\n#{__docs_line_to_html__ l, parse_log}\n</p>\n"
end
end
end
toc_html += "</ul>"
final_html = html_start_to_toc_start +
toc_html +
html_toc_end_to_content_start +
content_html +
html_content_end_to_html_end
{
original: string,
html: final_html,
parse_log: parse_log
}
rescue Exception => e
$gtk.write_file_root 'docs/parse_log.txt', (parse_log.join "\n")
raise "* ERROR in Docs::__docs_to_html__. #{e}"
end
def __docs_line_to_html__ line, parse_log
parse_log << "- Determining if line is a header."
if line.start_with? "**** "
line = line.gsub "**** ", ""
parse_log << "- Line contains ~**** ~... gsub-ing empty string"
elsif line.start_with? "*** "
line = line.gsub "*** ", ""
parse_log << "- Line contains ~*** ~... gsub-ing empty string"
elsif line.start_with? "** "
line = line.gsub "** ", ""
parse_log << "- Line contains ~** ~... gsub-ing empty string"
elsif line.start_with? "* "
line = line.gsub "* ", ""
parse_log << "- Line contains ~* ~... gsub-ing empty string"
elsif line.start_with? "* DOCS: "
line = line.gsub "* DOCS: ", ""
parse_log << "- Line contains ~* DOCS:~... gsub-ing empty string"
else
parse_log << "- Line does not appear to be a header."
end
tilde_count = line.count "~"
line_has_link_marker = (line.include? "[[") && (line.include? "]]")
parse_log << "- Formatting line: ~#{line}~"
parse_log << "- Line's tilde count is: #{tilde_count}"
parse_log << "- Line contains link marker: #{line_has_link_marker}"
line_to_format = line.rstrip
# <code> logic
if tilde_count.even? && tilde_count != 0
parse_log << "- CODE detected."
temp = line_to_format
line_to_format = ""
in_literal = false
in_code = false
temp.each_char do |c|
if c == "~" && !in_code
in_code = true
line_to_format << "<code>"
elsif c == "~" && in_code
line_to_format << "</code>"
in_code = false
else
line_to_format << c
end
end
end
# <a> and <img> logic
if line_has_link_marker
line_to_format = line_to_format.gsub "[[", "["
line_to_format = line_to_format.gsub "]]", "]"
parse_log << "- LINK detected."
temp = line_to_format
line_to_format = ""
in_literal = false
in_link = false
link_url = ""
temp.each_char.with_index do |c, i|
next_c = temp[i + 1]
if !in_link && c == "["
in_link = true
link_url = ""
elsif in_link && c == "]"
in_link = false
if link_url.end_with? ".gif"
line_to_format << "<img src='#{link_url}'></img>"
else
line_to_format << "<a href='#{link_url}'>#{link_url}</a>"
end
elsif in_link
link_url << c
else
line_to_format << c
end
end
end
return line_to_format
rescue Exception => e
parse_log << "* ERROR: Failed to parse line: ~#{line}~, #{e}"
return line.rstrip
end
end
|