Class: TomDoc::Generators::Console

Inherits:
TomDoc::Generator show all
Defined in:
lib/tomdoc/generators/console.rb

Instance Attribute Summary

Attributes inherited from TomDoc::Generator

#options, #scopes

Instance Method Summary collapse

Methods inherited from TomDoc::Generator

#constant?, #constant_names, generate, #generate, #initialize, #matches_pattern?, #process, #pygments, #valid?, #valid_tomdoc?, #write, #write_class_methods, #write_instance_methods, #write_scope, #write_scope_footer

Constructor Details

This class inherits a constructor from TomDoc::Generator

Instance Method Details

#args(method) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/tomdoc/generators/console.rb', line 19

def args(method)
  return '' if !method.respond_to?(:args)
  if method.args.any?
    '(' + method.args.join(', ') + ')'
  else
    ''
  end
end

#format_comment(comment) ⇒ Object



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
# File 'lib/tomdoc/generators/console.rb', line 28

def format_comment(comment)
  comment = comment.to_s

  # Strip leading comments
  comment.gsub!(/^# ?/, '')

  # Example code
  comment.gsub!(/(\s*Examples\s*(.+?)\s*Returns)/m) do
    $1.sub($2, highlight($2))
  end

  # Param list
  comment.gsub!(/^(\s*(\w+) +- )/) do
    param = $2
    $1.sub(param, param.green)
  end

  # true/false/nil
  comment.gsub!(/(true|false|nil)/, '\1'.magenta)

  # Strings
  comment.gsub!(/('.+?')/, '\1'.yellow)
  comment.gsub!(/(".+?")/, '\1'.yellow)

  # Symbols
  comment.gsub!(/(\s+:\w+)/, '\1'.red)

  # Constants
  comment.gsub!(/(([A-Z]\w+(::)?)+)/) do
    if constant?($1.strip)
      $1.split('::').map { |part| part.cyan }.join('::')
    else
      $1
    end
  end

  comment
end

#highlight(text) ⇒ Object



4
5
6
# File 'lib/tomdoc/generators/console.rb', line 4

def highlight(text)
  pygments(text, '-l', 'ruby')
end

#write_method(method, prefix = '') ⇒ Object



13
14
15
16
17
# File 'lib/tomdoc/generators/console.rb', line 13

def write_method(method, prefix = '')
  write '-' * 80
  write "#{prefix}#{method.name}#{args(method)}".bold, ''
  write format_comment(method.tomdoc)
end

#write_scope_header(scope, prefix = '') ⇒ Object



8
9
10
11
# File 'lib/tomdoc/generators/console.rb', line 8

def write_scope_header(scope, prefix = '')
  return if scope.tomdoc.to_s.empty?
  write_method(scope, prefix)
end