Class: TomDoc::Generator
- Inherits:
-
Object
show all
- Defined in:
- lib/tomdoc/generator.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#constant?(const) ⇒ Boolean
-
#constant_names ⇒ Object
-
#generate(text_or_sexp) ⇒ Object
-
#initialize(options = {}, scopes = {}) ⇒ Generator
constructor
-
#matches_pattern?(prefix, name) ⇒ Boolean
-
#process(scopes = {}, prefix = nil) ⇒ Object
-
#pygments(text, *args) ⇒ Object
-
#valid?(object, prefix) ⇒ Boolean
-
#valid_tomdoc?(comment) ⇒ Boolean
-
#write(*things) ⇒ Object
-
#write_class_methods(scope, prefix = nil) ⇒ Object
-
#write_instance_methods(scope, prefix = nil) ⇒ Object
-
#write_method(method, prefix = '') ⇒ Object
-
#write_scope(scope, prefix) ⇒ Object
-
#write_scope_footer(scope, prefix) ⇒ Object
-
#write_scope_header(scope, prefix) ⇒ Object
Constructor Details
#initialize(options = {}, scopes = {}) ⇒ Generator
Creates a Generator.
options - Optional Symbol-keyed Hash:
:validate - Whether or not to validate TomDoc.
scopes - Optional Symbol-keyed Hash.
Returns an instance of TomDoc::Generator
13
14
15
16
17
18
19
20
21
|
# File 'lib/tomdoc/generator.rb', line 13
def initialize(options = {}, scopes = {})
@options = {
:validate => true
}
@options.update(options)
@scopes = {}
@buffer = ''
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3
4
5
|
# File 'lib/tomdoc/generator.rb', line 3
def options
@options
end
|
#scopes ⇒ Object
Returns the value of attribute scopes.
3
4
5
|
# File 'lib/tomdoc/generator.rb', line 3
def scopes
@scopes
end
|
Class Method Details
.generate(text_or_sexp) ⇒ Object
23
24
25
|
# File 'lib/tomdoc/generator.rb', line 23
def self.generate(text_or_sexp)
new.generate(text_or_sexp)
end
|
Instance Method Details
#constant?(const) ⇒ Boolean
104
105
106
107
|
# File 'lib/tomdoc/generator.rb', line 104
def constant?(const)
const = const.split('::').first if const.include?('::')
constant_names.include?(const.intern) || Object.const_defined?(const)
end
|
#constant_names ⇒ Object
109
110
111
112
|
# File 'lib/tomdoc/generator.rb', line 109
def constant_names
name = @scopes.name if @scopes.respond_to?(:name)
[ :Boolean, :Test, name ].compact + @scopes.keys
end
|
#generate(text_or_sexp) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/tomdoc/generator.rb', line 27
def generate(text_or_sexp)
if text_or_sexp.is_a?(String)
sexp = SourceParser.parse(text_or_sexp)
else
sexp = text_or_sexp
end
process(sexp)
end
|
#matches_pattern?(prefix, name) ⇒ Boolean
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/tomdoc/generator.rb', line 118
def matches_pattern?(prefix, name)
if pattern = options[:pattern]
if pattern =~ /^\/.+\/$/
pattern = pattern.sub(/^\//, '').sub(/\/$/, '')
regexp = Regexp.new(pattern)
else
regexp = Regexp.new(Regexp.escape(pattern))
end
regexp =~ name.to_s || regexp =~ prefix.to_s
else
true
end
end
|
#process(scopes = {}, prefix = nil) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/tomdoc/generator.rb', line 37
def process(scopes = {}, prefix = nil)
old_scopes = @scopes
@scopes = scopes
scopes.each do |name, scope|
write_scope(scope, prefix)
process(scope, "#{name}::")
end
@buffer
ensure
@scopes = old_scopes || {}
end
|
#pygments(text, *args) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/tomdoc/generator.rb', line 92
def pygments(text, *args)
out = ''
Open3.popen3("pygmentize", *args) do |stdin, stdout, stderr|
stdin.puts text
stdin.close
out = stdout.read.chomp
end
out
end
|
#valid?(object, prefix) ⇒ Boolean
114
115
116
|
# File 'lib/tomdoc/generator.rb', line 114
def valid?(object, prefix)
matches_pattern?(prefix, object.name) && valid_tomdoc?(object.tomdoc)
end
|
#valid_tomdoc?(comment) ⇒ Boolean
134
135
136
|
# File 'lib/tomdoc/generator.rb', line 134
def valid_tomdoc?()
options[:validate] ? TomDoc.valid?() : true
end
|
#write(*things) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/tomdoc/generator.rb', line 84
def write(*things)
things.each do |thing|
@buffer << "#{thing}\n"
end
nil
end
|
#write_class_methods(scope, prefix = nil) ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/tomdoc/generator.rb', line 63
def write_class_methods(scope, prefix = nil)
prefix ="#{prefix}#{scope.name}."
scope.class_methods.map do |method|
next if !valid?(method, prefix)
write_method(method, prefix)
end.compact
end
|
#write_instance_methods(scope, prefix = nil) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/tomdoc/generator.rb', line 72
def write_instance_methods(scope, prefix = nil)
prefix = "#{prefix}#{scope.name}#"
scope.instance_methods.map do |method|
next if !valid?(method, prefix)
write_method(method, prefix)
end.compact
end
|
#write_method(method, prefix = '') ⇒ Object
81
82
|
# File 'lib/tomdoc/generator.rb', line 81
def write_method(method, prefix = '')
end
|
#write_scope(scope, prefix) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/tomdoc/generator.rb', line 50
def write_scope(scope, prefix)
(scope, prefix)
write_class_methods(scope, prefix)
write_instance_methods(scope, prefix)
(scope, prefix)
end
|
60
61
|
# File 'lib/tomdoc/generator.rb', line 60
def (scope, prefix)
end
|
57
58
|
# File 'lib/tomdoc/generator.rb', line 57
def (scope, prefix)
end
|