Skip to content

Commit 1374b67

Browse files
committed
(PDOC-184) generate markdown
This change does a few things: 1. Fixes up new api handler to return the stuff we want 2. Adds all the logic to parse YARD registries into markdown 3. Adds templates for markdown 4. Changes Face cli to use a --format option that can be used for either markdown or json
1 parent 59aa812 commit 1374b67

26 files changed

+1259
-27
lines changed

lib/puppet-strings.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module PuppetStrings
1414
# @option options [Boolean] :debug Enable YARD debug output.
1515
# @option options [Boolean] :backtrace Enable YARD backtraces.
1616
# @option options [String] :markup The YARD markup format to use (defaults to 'markdown').
17-
# @option options [String] :json Enables JSON output to the given file. If the file is nil, STDOUT is used.
17+
# @option options [String] :format Specify output format (markdown or json)
18+
# @option options [String] :path Write the selected format to a file path
19+
# @option options [Boolean] :stdout Use this switch to pipe the selected format to STDOUT
1820
# @option options [Array<String>] :yard_args The arguments to pass to yard.
1921
# @return [void]
2022
def self.generate(search_patterns = DEFAULT_SEARCH_PATTERNS, options = {})
@@ -27,15 +29,13 @@ def self.generate(search_patterns = DEFAULT_SEARCH_PATTERNS, options = {})
2729
args << '--backtrace' if options[:backtrace]
2830
args << "-m#{options[:markup] || 'markdown'}"
2931

30-
render_as_json = options.key? :json
31-
json_file = nil
32-
if render_as_json
33-
json_file = options[:json]
32+
if options[:json] || options[:markdown]
33+
file = options[:path]
3434
# Disable output and prevent stats/progress when writing to STDOUT
3535
args << '-n'
36-
args << '-q' unless json_file
37-
args << '--no-stats' unless json_file
38-
args << '--no-progress' unless json_file
36+
args << '-q' unless file
37+
args << '--no-stats' unless file
38+
args << '--no-progress' unless file
3939
end
4040

4141
yard_args = options[:yard_args]
@@ -46,10 +46,24 @@ def self.generate(search_patterns = DEFAULT_SEARCH_PATTERNS, options = {})
4646
YARD::CLI::Yardoc.run(*args)
4747

4848
# If outputting JSON, render the output
49-
if render_as_json
50-
require 'puppet-strings/json'
51-
PuppetStrings::Json.render(json_file)
49+
if options[:json]
50+
render_json(options[:path])
5251
end
52+
53+
# If outputting Markdown, render the output
54+
if options[:markdown]
55+
render_markdown(options[:path])
56+
end
57+
end
58+
59+
def self.render_json(path)
60+
require 'puppet-strings/json'
61+
PuppetStrings::Json.render(path)
62+
end
63+
64+
def self.render_markdown(path)
65+
require 'puppet-strings/markdown'
66+
PuppetStrings::Markdown.render(path)
5367
end
5468

5569
# Runs the YARD documentation server.

lib/puppet-strings/markdown.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'puppet-strings/json'
2+
3+
# module for parsing Yard Registries and generating markdown
4+
module PuppetStrings::Markdown
5+
require_relative 'markdown/puppet_classes'
6+
require_relative 'markdown/puppet_functions'
7+
require_relative 'markdown/puppet_defined_types'
8+
require_relative 'markdown/puppet_resource_types'
9+
require_relative 'markdown/table_of_contents'
10+
11+
# generates markdown documentation
12+
# @return [String] markdown doc
13+
def self.generate
14+
final = "# Reference\n\n"
15+
final << PuppetStrings::Markdown::TableOfContents.render
16+
final << PuppetStrings::Markdown::PuppetClasses.render
17+
final << PuppetStrings::Markdown::PuppetDefinedTypes.render
18+
final << PuppetStrings::Markdown::PuppetResourceTypes.render
19+
final << PuppetStrings::Markdown::PuppetFunctions.render
20+
21+
final
22+
end
23+
24+
def self.render(path = nil)
25+
if path.nil?
26+
puts generate
27+
exit
28+
else
29+
File.open(path, 'w') { |file| file.write(generate) }
30+
end
31+
end
32+
end

lib/puppet-strings/markdown/base.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
require 'puppet-strings'
2+
require 'puppet-strings/json'
3+
require 'puppet-strings/yard'
4+
5+
module PuppetStrings::Markdown
6+
class Base
7+
def initialize(registry, component_type)
8+
@type = component_type
9+
@registry = registry
10+
@tags = registry[:docstring][:tags] || []
11+
end
12+
13+
def name
14+
@registry[:name].to_s unless @registry[:name].nil?
15+
end
16+
17+
def text
18+
@registry[:docstring][:text] unless @registry[:docstring][:text].empty?
19+
end
20+
21+
def return_val
22+
@tags.select { |tag| tag[:tag_name] == 'return' }[0][:text] unless @tags.select { |tag| tag[:tag_name] == 'return' }[0].nil?
23+
end
24+
25+
def return_type
26+
@tags.select { |tag| tag[:tag_name] == 'return' }[0][:types][0] unless @tags.select { |tag| tag[:tag_name] == 'return' }[0].nil?
27+
end
28+
29+
# @return [String] text from @since tag
30+
def since
31+
@tags.select { |tag| tag[:tag_name] == 'since' }[0][:text] unless @tags.select { |tag| tag[:tag_name] == 'since' }[0].nil?
32+
end
33+
34+
# return [Array] array of @see tag hashes
35+
def see
36+
@tags.select { |tag| tag[:tag_name] == 'see' } unless @tags.select { |tag| tag[:tag_name] == 'see' }[0].nil?
37+
end
38+
39+
# return [String] text from @summary tag
40+
def summary
41+
@tags.select { |tag| tag[:tag_name] == 'summary' }[0][:text] unless @tags.select { |tag| tag[:tag_name] == 'summary' }[0].nil?
42+
end
43+
44+
# return [Array] array of parameter tag hashes
45+
def params
46+
@tags.select { |tag| tag[:tag_name] == 'param' } unless @tags.select { |tag| tag[:tag_name] == 'param' }[0].nil?
47+
end
48+
49+
# return [Array] array of example tag hashes
50+
def examples
51+
@tags.select { |tag| tag[:tag_name] == 'example' } unless @tags.select { |tag| tag[:tag_name] == 'example' }[0].nil?
52+
end
53+
54+
def toc_info
55+
{
56+
name: name.to_s,
57+
link: link,
58+
desc: summary || @registry[:docstring][:text].gsub("\n", ". ")
59+
}
60+
end
61+
62+
def link
63+
name.delete('::').strip.gsub(' ','-').downcase
64+
end
65+
66+
def defaults
67+
@registry[:defaults] unless @registry[:defaults].nil?
68+
end
69+
70+
def value_string(value)
71+
to_symbol = %w[undef true false]
72+
if to_symbol.include? value
73+
return "`#{value}`"
74+
else
75+
return value
76+
end
77+
end
78+
79+
def render(template)
80+
file = File.join(File.dirname(__FILE__),"templates/#{template}")
81+
ERB.new(File.read(file), nil, '-').result(binding)
82+
end
83+
end
84+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'puppet-strings/markdown/base'
2+
3+
module PuppetStrings::Markdown
4+
class PuppetClass < Base
5+
def initialize(registry)
6+
@template = 'puppet_resource.erb'
7+
super(registry, 'class')
8+
end
9+
10+
def render
11+
super(@template)
12+
end
13+
end
14+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative 'puppet_class'
2+
3+
module PuppetStrings::Markdown
4+
module PuppetClasses
5+
def self.in_classes
6+
YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash)
7+
end
8+
9+
def self.render
10+
final = "## Classes\n\n"
11+
in_classes.each do |klass|
12+
final << PuppetStrings::Markdown::PuppetClass.new(klass).render
13+
end
14+
final
15+
end
16+
17+
def self.toc_info
18+
final = []
19+
20+
in_classes.each do |klass|
21+
final.push(PuppetStrings::Markdown::PuppetClass.new(klass).toc_info)
22+
end
23+
24+
final
25+
end
26+
end
27+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'puppet-strings/markdown/base'
2+
3+
module PuppetStrings::Markdown
4+
class PuppetDefinedType < Base
5+
def initialize(registry)
6+
@template = 'puppet_resource.erb'
7+
super(registry, 'defined type')
8+
end
9+
10+
def render
11+
super(@template)
12+
end
13+
end
14+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative 'puppet_defined_type'
2+
3+
module PuppetStrings::Markdown
4+
module PuppetDefinedTypes
5+
def self.in_dtypes
6+
YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash)
7+
end
8+
9+
def self.render
10+
final = "## Defined types\n\n"
11+
in_dtypes.each do |type|
12+
final << PuppetStrings::Markdown::PuppetDefinedType.new(type).render
13+
end
14+
final
15+
end
16+
17+
def self.toc_info
18+
final = []
19+
20+
in_dtypes.each do |type|
21+
final.push(PuppetStrings::Markdown::PuppetDefinedType.new(type).toc_info)
22+
end
23+
24+
final
25+
end
26+
end
27+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'puppet-strings/markdown/base'
2+
3+
module PuppetStrings::Markdown
4+
class PuppetFunction < Base
5+
attr_reader :signatures
6+
7+
def initialize(registry)
8+
@template = 'puppet_function.erb'
9+
super(registry, 'function')
10+
@signatures = []
11+
registry[:signatures].each do |sig|
12+
@signatures.push(Signature.new(sig))
13+
end
14+
end
15+
16+
def render
17+
super(@template)
18+
end
19+
end
20+
21+
class PuppetFunction::Signature < Base
22+
def initialize(registry)
23+
@registry = registry
24+
super(@registry, 'function signature')
25+
end
26+
27+
def signature
28+
@registry[:signature]
29+
end
30+
end
31+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative 'puppet_function'
2+
3+
module PuppetStrings::Markdown
4+
module PuppetFunctions
5+
def self.in_functions
6+
YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash)
7+
end
8+
9+
def self.render
10+
final = "## Functions\n\n"
11+
in_functions.each do |func|
12+
final << PuppetStrings::Markdown::PuppetFunction.new(func).render
13+
end
14+
final
15+
end
16+
17+
def self.toc_info
18+
final = []
19+
20+
in_functions.each do |func|
21+
final.push(PuppetStrings::Markdown::PuppetFunction.new(func).toc_info)
22+
end
23+
24+
final
25+
end
26+
end
27+
end
28+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'puppet-strings/markdown/base'
2+
3+
module PuppetStrings::Markdown
4+
class PuppetResourceType < Base
5+
def initialize(registry)
6+
@template = 'puppet_resource_type.erb'
7+
super(registry, 'resource type')
8+
end
9+
10+
def render
11+
super(@template)
12+
end
13+
14+
def properties
15+
@registry[:properties]
16+
end
17+
18+
def parameters
19+
@registry[:parameters]
20+
end
21+
22+
def regex_in_data_type?(data_type)
23+
data_type.match(/\w+\[\/.*\/\]/).length > 0
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)