Module: Docs

Included in:
Array, GTK::Args, GTK::Mouse, GTK::Outputs, GTK::ReadMe, GTK::Runtime, Geometry, Kernel, Numeric
Defined in:
dragon/docs.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__docs_search__(words = nil, &block) ⇒ Object


151
152
153
# File 'dragon/docs.rb', line 151

def self.__docs_search__ words = nil, &block

end

.extended(klass) ⇒ Object


81
82
83
84
85
# File 'dragon/docs.rb', line 81

def self.extended klass
  $docs_classes ||= []
  $docs_classes << klass
  $docs_classes.uniq!
end

Instance Method Details

#__docs_append_true_line__(true_lines, true_line, parse_log) ⇒ Object


245
246
247
248
249
# File 'dragon/docs.rb', line 245

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

#__docs_line_to_html__(line, parse_log) ⇒ Object


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
# File 'dragon/docs.rb', line 548

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

#__docs_search_help_text__Object


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'dragon/docs.rb', line 155

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

#__docs_search_results__(words = nil, &block) ⇒ Object


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
# File 'dragon/docs.rb', line 175

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

#__docs_to_html__(string) ⇒ Object

may god have mercy on your soul if you try to expand this


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
# File 'dragon/docs.rb', line 252

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

#__export_docs__!(opts = {}) ⇒ Object


226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'dragon/docs.rb', line 226

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

#docsObject


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
# File 'dragon/docs.rb', line 110

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

#docs_allObject


103
104
105
106
107
108
# File 'dragon/docs.rb', line 103

def docs_all
  docs_methods = DocsOrganizer.find_methods_with_docs(self).map { |d| send d }.join "\n"
  <<-S
#{docs_methods}
S
end

#docs_classesObject


91
92
93
94
95
96
97
98
99
100
101
# File 'dragon/docs.rb', line 91

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

#docs_method_sort_orderObject


87
88
89
# File 'dragon/docs.rb', line 87

def docs_method_sort_order
  []
end

#docs_search(words = nil, &block) ⇒ Object


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'dragon/docs.rb', line 210

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

#export_docs!(opts = {}) ⇒ Object


241
242
243
# File 'dragon/docs.rb', line 241

def export_docs! opts = {}
  __export_docs__! opts
end