Class: TomDoc::Generators::Shomen

Inherits:
Generator
  • Object
show all
Defined in:
lib/shomen/tomdoc.rb

Overview

Shomen generator for TomDoc.

IMPORTANT: Unfortunately TomDoc’s parser does not yet provide enough information to generate a substantial Shomen model. As things currently stand THIS GENERATOR IS NOT USABLE. Hopefully in time the missing information will be added, and this can become a real option.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, scopes = {}) ⇒ Shomen

Returns a new instance of Shomen.



18
19
20
21
# File 'lib/shomen/tomdoc.rb', line 18

def initialize(options = {}, scopes = {})
  super(options, scopes)
  @table = {}
end

Instance Method Details

#clean(comment) ⇒ Object (private)

Remove hash prefixes from raw comment.



140
141
142
143
144
145
# File 'lib/shomen/tomdoc.rb', line 140

def clean(comment)
  clean = comment.split("\n").map do |line|
    line =~ /^(\s*# ?)/ ? line.sub($1, '') : nil
  end.compact.join("\n")
  clean
end

#parse_interface(method, doc) ⇒ Object (private)

Parse method interface.



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
# File 'lib/shomen/tomdoc.rb', line 102

def parse_interface(method, doc)
  args, block = [], {}

  #interface, returns = interface.split(/[=-]\>/)
  #interface = interface.strip
  #if i = interface.index(/\)\s*\{/)
  #  block['signature'] = interface[i+1..-1].strip
  #  interface = interface[0..i].strip
  #end

  #arguments = interface.strip.sub(/^.*?\(/,'').chomp(')')
  #arguments = arguments.split(/\s*\,\s*/)
  doc.args.each do |a|
    name = a.name.to_s
    desc = a.description

    if name.start_with?('&')
      block['name'] = name
    else
      h = {'name'=>name,'comment'=>desc}
      # TODO: doesn't look like tomdoc is providing argument defaults :(
      if e = method.args.find{ |x| /^#{name}/ =~ x }
        n,v = e.to_s.split('=')
        h['default'] = v if v
      end
      args << h
    end
  end

  result = {}
  result['signature'] = "#{method.name}(#{method.args.join(',')})"
  result['arguments'] = args
  result['block']     = block unless block.empty?
  #result['returns']   = returns.strip if returns
  return result
end

#tableObject



24
# File 'lib/shomen/tomdoc.rb', line 24

def table; @table; end

#write_class_methods(scope, prefix) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/shomen/tomdoc.rb', line 40

def write_class_methods(scope, prefix)
  prefix ="#{prefix}#{scope.name}."

  scope.class_methods.map do |method|
    next if !valid?(method, prefix)
    write_method(method, prefix, 'class')
  end.compact
end

#write_method(tomdoc_method, prefix = '', kind = 'instance') ⇒ Object



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
# File 'lib/shomen/tomdoc.rb', line 50

def write_method(tomdoc_method, prefix='', kind='instance')
  model = ::Shomen::Model::Method.new

  doc = ::TomDoc::TomDoc.new(tomdoc_method.comment)

  model.path        = prefix + tomdoc_method.name.to_s
  model.name        = tomdoc_method.name.to_s
  model.namespace   = prefix.chomp('#').chomp('.')
  # TODO: add examples to description or should shomen support examples?
  model.comment     = doc.description

  model.format      = 'tomdoc'
  #model.aliases     = tomdoc_method.aliases.map{ |a| method_name(a) }
  #model.alias_for   = method_name(tomdoc_method.is_alias_for)
  model.singleton   = (kind == 'class')

  model.declarations << kind

  # TODO: how to get visibility?
  #model.declarations << tomdoc_method.visibility.to_s

  #model.interfaces = []
  #if tomdoc_method.call_seq
  #  tomdoc_method.call_seq.split("\n").each do |cs|
  #    cs = cs.to_s.strip
  #    model.interfaces << parse_interface(cs) unless cs == ''
  #  end
  #end

  model.interfaces = []
  model.interfaces << parse_interface(tomdoc_method, doc)

  model.returns = doc.returns
  model.raises  = doc.raises

  # TODO: tomdoc doesn't provide these, so we are S.O.L.
  # model.file       = '/'+tomdoc_method.source_code_location.first
  # model.line       = tomdoc_method.source_code_location.last.to_i
  # model.source     = tomdoc_method.source_code_raw

  #if tomdoc_method.respond_to?(:c_function)
  #  model.language = tomdoc_method.c_function ? 'c' : 'ruby'
  #else
    model.language = 'ruby'
  #end

  @table[model.path] = model.to_h
end

#write_scope_header(scope, prefix) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shomen/tomdoc.rb', line 27

def write_scope_header(scope, prefix)
  # TODO: is it a module or class ?
  model = ::Shomen::Model::Module.new
  model = ::Shomen::Model::Class.new

  model.name    = scope.name.to_s
  model.path    = prefix.to_s + scope.name.to_s
  model.comment = clean(scope.comment.strip)

  @table[model.path] = model.to_h
end