Module: Yard::Yaml::TagRenderer

Defined in:
lib/yard/yaml/tag_renderer.rb

Overview

Renders HTML fragments for @yaml (inline block) and @yaml_file tags
found on a YARD code object. This is a small, YARD-agnostic utility
that can be called from templates. It does not depend on YARD internals
beyond the code object responding to tags(:name).

Phase 5: This provides the wiring surface needed by templates. Actual
template hook-up can call …).

Class Method Summary collapse

Class Method Details

.extract_text(tag) ⇒ Object



54
55
56
57
58
59
# File 'lib/yard/yaml/tag_renderer.rb', line 54

def extract_text(tag)
  return tag.text if tag.respond_to?(:text)
  # Some YARD tags use #name or #to_s; fall back sensibly
  return tag.name.to_s if tag.respond_to?(:name)
  tag.to_s
end

.fetch_tags(object, name) ⇒ Object

— internal helpers —



47
48
49
50
51
# File 'lib/yard/yaml/tag_renderer.rb', line 47

def fetch_tags(object, name)
  object.tags(name) || []
rescue StandardError
  []
end

.render_for(object, base_dir: Dir.pwd, config: Yard::Yaml.config) ⇒ String

Render YAML-related tags on a code object into a single HTML fragment.

Parameters:

  • object (#tags)

    a YARD code object (or duck-typed test double)

  • base_dir (String) (defaults to: Dir.pwd)

    base directory to resolve @yaml_file paths

  • config (Yard::Yaml::Config) (defaults to: Yard::Yaml.config)

Returns:

  • (String)

    HTML fragment (may be empty string)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yard/yaml/tag_renderer.rb', line 21

def render_for(object, base_dir: Dir.pwd, config: Yard::Yaml.config)
  return "" unless object && object.respond_to?(:tags)

  parts = []

  # Inline @yaml blocks (text of the tag is treated as YAML)
  fetch_tags(object, :yaml).each do |tag|
    yaml_text = extract_text(tag)
    next if yaml_text.nil? || yaml_text.strip.empty?
    html = Yard::Yaml::TemplateHelpers.render_yaml_block(yaml_text, config: config)
    parts << wrap_section(html, kind: :yaml)
  end

  # @yaml_file path
  fetch_tags(object, :yaml_file).each do |tag|
    path = extract_text(tag)
    next if path.nil? || path.strip.empty?
    html = Yard::Yaml::TemplateHelpers.render_yaml_file(path, base_dir: base_dir, config: config)
    parts << wrap_section(html, kind: :yaml_file)
  end

  parts.join("\n")
end

.wrap_section(html, kind:) ⇒ Object



62
63
64
65
66
# File 'lib/yard/yaml/tag_renderer.rb', line 62

def wrap_section(html, kind:)
  return "" if html.to_s.empty?
  css = (kind == :yaml) ? "yyaml-inline" : "yyaml-file"
  %(<div class="#{css}">#{html}</div>)
end