-
Notifications
You must be signed in to change notification settings - Fork 0
/
puppetjson2markdown.rb
202 lines (173 loc) · 4.85 KB
/
puppetjson2markdown.rb
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
#!/usr/bin/env ruby
require 'json'
def quote_block(text)
puts '```'
puts text
puts '```'
end
def single_line(text)
puts text
puts
end
def write_docstring_summary(docstring)
summary_tag = get_docstring_elems_with_tag(docstring, 'summary')
return if summary_tag.empty?
puts '### Summary'
summary_tag.each do |summ|
remove_docstring_elem_with_tag(docstring, 'summary')
single_line(summ['text'])
end
end
def write_docstring_overview(docstring)
overview = docstring['text']
return if overview.empty?
puts '### Overview'
single_line(overview)
end
def write_docstring_examples(docstring)
example_tag = get_docstring_elems_with_tag(docstring, 'example')
return if example_tag.empty?
example_tag.each do |example|
remove_docstring_elem_with_tag(docstring, 'example')
puts '#### ' + example['name']
quote_block(example['text'])
end
end
def write_params_table_header
puts '### Parameters'
puts '| Name | Description | Default |'
puts '| --- | --- | --- |'
end
def write_params_table_row(name, text, default_value)
puts '| ' + name + ' | ' + text + ' | ' + default_value + ' |'
end
def remove_docstring_elem_with_tag(docstring, tag_to_del)
docstring['tags'].delete_if { |element| element['tag_name'].eql?tag_to_del }
end
def get_docstring_elems_with_tag(docstring, tag_to_get)
docstring['tags'].select { |tag| tag['tag_name'].eql?tag_to_get }
end
def write_docstring_params(docstring, defaults)
param_tag = get_docstring_elems_with_tag(docstring, 'param')
return if param_tag.empty?
write_params_table_header
param_tag.each do |param|
name = param['name']
remove_docstring_elem_with_tag(docstring, 'param')
write_params_table_row(name, param['text'], defaults.fetch(name))
end
puts
end
def write_docstring_see(docstring)
see_tag = get_docstring_elems_with_tag(docstring, 'see')
return if see_tag.empty?
puts '### See'
see_tag.each do |see|
remove_docstring_elem_with_tag(docstring, 'see')
name = see['name']
single_line('[' + name + '](' + name + ')')
end
puts
end
def write_docstring_authors(docstring)
author_tag = get_docstring_elems_with_tag(docstring, 'author')
return if author_tag.empty?
puts '### Authors'
author_tag.each do |author|
remove_docstring_elem_with_tag(docstring, 'author')
puts author['text']
end
puts
end
def write_docstring_extratags(docstring)
((docstring['tags'].select { |s| s['tag_name'] })
.uniq { |r| r['tag_name'] }).each do |tag|
puts tag['tag_name'] + ': ' + tag['text']
end
end
def write_docstring(docstring, defaults)
write_docstring_summary(docstring)
write_docstring_overview(docstring)
write_docstring_examples(docstring)
write_docstring_params(docstring, defaults)
write_docstring_see(docstring)
write_docstring_authors(docstring)
write_docstring_extratags(docstring)
end
def write_header_name(puppetclass)
name = puppetclass['name']
return unless name.is_a?(String)
puts '## ' + name
end
def write_file(puppetclass)
file = puppetclass['file']
return unless file.is_a?(String)
puts '### Source code'
single_line('[' + file + '](' + file + ')')
end
def write_inherits(puppetclass)
inherits = puppetclass['inherits']
return unless inherits.is_a?(String)
puts '### Inherits from'
single_line(inherits)
end
def document_class(puppetclass)
write_header_name(puppetclass)
write_file(puppetclass)
write_inherits(puppetclass)
docstring = puppetclass['docstring']
write_docstring(docstring, puppetclass['defaults'])
end
def document_defined_type(defined_type)
puts 'Not supported yet'
end
def document_resource_type(resource_typ)
puts 'Not supported yet'
end
def document_provider(provider)
puts 'Not supported yet'
end
def document_function(function)
puts 'Not supported yet'
end
def document_classes(puppet_classes)
return if puppet_classes.empty?
puts '# Puppet Classes'
puppet_classes.each do |puppet_class|
document_class(puppet_class)
end
end
def document_defined_types(defined_types)
return if defined_types.empty?
puts '# Defined Types'
defined_types.each do |defined_type|
document_class(defined_type)
end
end
def document_resource_types(resource_types)
return if resource_types.empty?
puts '# Resource Types'
resource_types.each do |resource_type|
document_class(resource_type)
end
end
def document_providers(providers)
return if providers.empty?
puts '# Providers'
providers.each do |provider|
document_class(provider)
end
end
def document_functions(puppet_functions)
return if puppet_functions.empty?
puts '# Functions'
puppet_functions.each do |puppet_function|
document_class(puppet_function)
end
end
doc = JSON.parse(ARGF.read)
document_classes(doc['puppet_classes'])
document_defined_types(doc['defined_types'])
document_resource_types(doc['resource_types'])
document_providers(doc['providers'])
document_functions(doc['puppet_functions'])