|
| 1 | +require 'puppet-strings' |
| 2 | +require 'puppet-strings/json' |
| 3 | +require 'puppet-strings/yard' |
| 4 | + |
| 5 | +module PuppetStrings::Markdown |
| 6 | + # This class makes elements in a YARD::Registry hash easily accessible for templates. |
| 7 | + # |
| 8 | + # Here's an example hash: |
| 9 | + #{:name=>:klass, |
| 10 | + # :file=>"(stdin)", |
| 11 | + # :line=>16, |
| 12 | + # :inherits=>"foo::bar", |
| 13 | + # :docstring=> |
| 14 | + # {:text=>"An overview for a simple class.", |
| 15 | + # :tags=> |
| 16 | + # [{:tag_name=>"summary", :text=>"A simple class."}, |
| 17 | + # {:tag_name=>"since", :text=>"1.0.0"}, |
| 18 | + # {:tag_name=>"see", :name=>"www.puppet.com"}, |
| 19 | + # {:tag_name=>"example", |
| 20 | + # :text=> |
| 21 | + # "class { 'klass':\n" + |
| 22 | + # " param1 => 1,\n" + |
| 23 | + # " param3 => 'foo',\n" + |
| 24 | + # "}", |
| 25 | + # :name=>"This is an example"}, |
| 26 | + # {:tag_name=>"author", :text=>"eputnam"}, |
| 27 | + # {:tag_name=>"option", :name=>"opts"}, |
| 28 | + # {:tag_name=>"raise", :text=>"SomeError"}, |
| 29 | + # {:tag_name=>"param", |
| 30 | + # :text=>"First param.", |
| 31 | + # :types=>["Integer"], |
| 32 | + # :name=>"param1"}, |
| 33 | + # {:tag_name=>"param", |
| 34 | + # :text=>"Second param.", |
| 35 | + # :types=>["Any"], |
| 36 | + # :name=>"param2"}, |
| 37 | + # {:tag_name=>"param", |
| 38 | + # :text=>"Third param.", |
| 39 | + # :types=>["String"], |
| 40 | + # :name=>"param3"}]}, |
| 41 | + # :defaults=>{"param1"=>"1", "param2"=>"undef", "param3"=>"'hi'"}, |
| 42 | + # :source=> |
| 43 | + # "class klass (\n" + |
| 44 | + # " Integer $param1 = 1,\n" + |
| 45 | + # " $param2 = undef,\n" + |
| 46 | + # " String $param3 = 'hi'\n" + |
| 47 | + # ") inherits foo::bar {\n" + |
| 48 | + # "}"} |
| 49 | + class Base |
| 50 | + def initialize(registry, component_type) |
| 51 | + @type = component_type |
| 52 | + @registry = registry |
| 53 | + @tags = registry[:docstring][:tags] || [] |
| 54 | + end |
| 55 | + |
| 56 | + # generate 1:1 tag methods |
| 57 | + # e.g. {:tag_name=>"author", :text=>"eputnam"} |
| 58 | + { :return_val => 'return', |
| 59 | + :since => 'since', |
| 60 | + :summary => 'summary' }.each do |method_name, tag_name| |
| 61 | + # @return [String] unless the tag is nil or the string.length == 0 |
| 62 | + define_method method_name do |
| 63 | + @tags.select { |tag| tag[:tag_name] == "#{tag_name}" }[0][:text] unless @tags.select { |tag| tag[:tag_name] == "#{tag_name}" }[0].nil? || @tags.select { |tag| tag[:tag_name] == "#{tag_name}" }[0][:text].length.zero? |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + # @return [String] top-level name |
| 68 | + def name |
| 69 | + @registry[:name].to_s unless @registry[:name].nil? |
| 70 | + end |
| 71 | + |
| 72 | + # @return [String] 'Overview' text (untagged text) |
| 73 | + def text |
| 74 | + @registry[:docstring][:text] unless @registry[:docstring][:text].empty? |
| 75 | + end |
| 76 | + |
| 77 | + # @return [String] data type of return value |
| 78 | + def return_type |
| 79 | + @tags.select { |tag| tag[:tag_name] == 'return' }[0][:types][0] unless @tags.select { |tag| tag[:tag_name] == 'return' }[0].nil? |
| 80 | + end |
| 81 | + |
| 82 | + # @return [String] text from @since tag |
| 83 | + def since |
| 84 | + @tags.select { |tag| tag[:tag_name] == 'since' }[0][:text] unless @tags.select { |tag| tag[:tag_name] == 'since' }[0].nil? |
| 85 | + end |
| 86 | + |
| 87 | + # @return [Array] @see tag hashes |
| 88 | + def see |
| 89 | + @tags.select { |tag| tag[:tag_name] == 'see' } unless @tags.select { |tag| tag[:tag_name] == 'see' }[0].nil? |
| 90 | + end |
| 91 | + |
| 92 | + # @return [Array] parameter tag hashes |
| 93 | + def params |
| 94 | + @tags.select { |tag| tag[:tag_name] == 'param' } unless @tags.select { |tag| tag[:tag_name] == 'param' }[0].nil? |
| 95 | + end |
| 96 | + |
| 97 | + # @return [Array] example tag hashes |
| 98 | + def examples |
| 99 | + @tags.select { |tag| tag[:tag_name] == 'example' } unless @tags.select { |tag| tag[:tag_name] == 'example' }[0].nil? |
| 100 | + end |
| 101 | + |
| 102 | + # @return [Array] raise tag hashes |
| 103 | + def raises |
| 104 | + @tags.select { |tag| tag[:tag_name] == 'raise' } unless @tags.select { |tag| tag[:tag_name] == 'raise' }[0].nil? |
| 105 | + end |
| 106 | + |
| 107 | + # @return [Array] option tag hashes |
| 108 | + def options |
| 109 | + @tags.select { |tag| tag[:tag_name] == 'option' } unless @tags.select { |tag| tag[:tag_name] == 'option' }[0].nil? |
| 110 | + end |
| 111 | + |
| 112 | + # @param parameter_name |
| 113 | + # parameter name to match to option tags |
| 114 | + # @return [Array] option tag hashes that have a parent parameter_name |
| 115 | + def options_for_param(parameter_name) |
| 116 | + opts_for_p = options.select { |o| o[:parent] == parameter_name } unless options.nil? |
| 117 | + opts_for_p unless opts_for_p.nil? || opts_for_p.length.zero? |
| 118 | + end |
| 119 | + |
| 120 | + # @return [Array] any defaults found for the component |
| 121 | + def defaults |
| 122 | + @registry[:defaults] unless @registry[:defaults].nil? |
| 123 | + end |
| 124 | + |
| 125 | + # @return [Hash] information needed for the table of contents |
| 126 | + def toc_info |
| 127 | + { |
| 128 | + name: name.to_s, |
| 129 | + link: link, |
| 130 | + desc: summary || @registry[:docstring][:text].gsub("\n", ". ") |
| 131 | + } |
| 132 | + end |
| 133 | + |
| 134 | + # @return [String] makes the component name suitable for a GitHub markdown link |
| 135 | + def link |
| 136 | + name.delete('::').strip.gsub(' ','-').downcase |
| 137 | + end |
| 138 | + |
| 139 | + # Some return, default, or valid values need to be in backticks. Instead of fu in the handler or code_object, this just does the change on the front. |
| 140 | + # @param value |
| 141 | + # any string |
| 142 | + # @return [String] value or value in backticks if it is in a list |
| 143 | + def value_string(value) |
| 144 | + to_symbol = %w[undef true false] |
| 145 | + if to_symbol.include? value |
| 146 | + return "`#{value}`" |
| 147 | + else |
| 148 | + return value |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + # @return [String] full markdown rendering of a component |
| 153 | + def render(template) |
| 154 | + file = File.join(File.dirname(__FILE__),"templates/#{template}") |
| 155 | + ERB.new(File.read(file), nil, '-').result(binding) |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments