Module: YARD::Markdown::TagFormattingHelper
- Defined in:
- lib/yard/markdown/tag_formatting_helper.rb
Overview
Formats YARD tags into Markdown list items and fenced examples.
Instance Method Summary collapse
-
#format_hash_tag_type(name, value) ⇒ String?
Formats a hash-style tag type entry.
-
#format_tag(tag) ⇒ String
Formats a non-example YARD tag as a Markdown list item body.
-
#normalized_tag_types(types) ⇒ Array<String>
Normalizes tag type declarations into printable strings.
-
#render_tags(object) ⇒ String
Renders all tags for an object as Markdown.
Instance Method Details
#format_hash_tag_type(name, value) ⇒ String?
Formats a hash-style tag type entry.
66 67 68 69 70 71 72 |
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 66 def format_hash_tag_type(name, value) key = name.rstrip return nil if key.empty? return key if value.nil? || value == true || (value.respond_to?(:empty?) && value.empty?) "#{key}: #{value}" end |
#format_tag(tag) ⇒ String
Formats a non-example YARD tag as a Markdown list item body.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 36 def format_tag(tag) parts = ["**@#{tag.tag_name}**"] parts << "`#{tag.name}`" unless tag.name.to_s.lstrip.empty? cleaned_types = normalized_tag_types(tag.types) parts << "[#{cleaned_types.join(', ')}]" unless cleaned_types.empty? parts << tag.text.strip unless tag.text.to_s.lstrip.empty? parts.join(' ') end |
#normalized_tag_types(types) ⇒ Array<String>
Normalizes tag type declarations into printable strings.
51 52 53 54 55 56 57 58 59 |
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 51 def normalized_tag_types(types) values = if types.instance_of?(Hash) types.map { |name, value| format_hash_tag_type(name, value) } else Array(types) end values.map(&:to_s).map(&:strip).reject(&:empty?) end |
#render_tags(object) ⇒ String
Renders all tags for an object as Markdown.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 11 def (object) lines = [] = object..reject { |tag| tag.tag_name == 'example' } = object..select { |tag| tag.tag_name == 'example' } .each do |tag| lines << "- #{format_tag(tag)}" end .each do |tag| lines << nil unless lines.empty? title = tag.name.to_s.rstrip.empty? ? '**@example**' : "**@example #{tag.name}**" lines << title lines << '```ruby' lines << tag.text.to_s.rstrip lines << '```' end lines.join("\n") end |