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

Instance Method Details

#format_tag(tag) ⇒ String

Formats a non-example YARD tag as a Markdown list item body.

Parameters:

  • tag (YARD::Tags::Tag)

    Non-example tag being converted into list item text.

Returns:

  • (String)

    Markdown representation of the tag.



31
32
33
34
35
36
37
38
39
40
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 31

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.

Parameters:

  • types (Array<Object>, nil)

    Raw tag types from YARD.

Returns:

  • (Array<String>)

    Cleaned type strings.



46
47
48
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 46

def normalized_tag_types(types)
  Array(types).map(&:to_s).map(&:strip).reject(&:empty?)
end

#render_tags(object) ⇒ String

Renders all tags for an object as Markdown.

Parameters:

  • object (YARD::CodeObjects::Base)

    Object whose tags are being rendered.

Returns:

  • (String)

    Markdown representation of the object's tags.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yard/markdown/tag_formatting_helper.rb', line 11

def render_tags(object)
  example_tags, regular_tags = object.tags.partition { |tag| tag.tag_name == "example" }
  lines = regular_tags.map { |tag| "- #{format_tag(tag)}" }

  example_tags.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