Module: YARD::Markdown::ArefHelper

Included in:
HeadingHelper
Defined in:
lib/yard/markdown/aref_helper.rb

Overview

Computes anchor ids that match the generated Markdown headings.

Instance Method Summary collapse

Instance Method Details

#anchor_component(value) ⇒ String

Encodes a value so it can be embedded safely in an HTML anchor id.

Parameters:

  • value (Object)

    Raw anchor fragment to encode.

Returns:

  • (String)

    Anchor-safe identifier fragment.



13
14
15
16
17
# File 'lib/yard/markdown/aref_helper.rb', line 13

def anchor_component(value)
  value.to_s.each_char.map do |char|
    char.match?(/[A-Za-z0-9_-]/) ? char : format("-%X", char.ord)
  end.join
end

#aref(object) ⇒ String

Returns the primary anchor id for a documented object.

Parameters:

  • object (YARD::CodeObjects::Base)

    Object being rendered.

Returns:

  • (String)

    Anchor id for the object's heading.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yard/markdown/aref_helper.rb', line 23

def aref(object)
  type = object.type

  return "class-#{object.path.gsub("::", "-")}" if type == :class
  return "module-#{object.path.gsub("::", "-")}" if type == :module
  return "constant-#{object.name}" if type == :constant
  return "classvariable-#{anchor_component(object.name)}" if type == :classvariable

  scope = (object.scope == :class) ? "c" : "i"

  if !object.attr_info.nil?
    "attribute-#{scope}-#{object.name}"
  else
    "method-#{scope}-#{anchor_component(object.name)}"
  end
end