Class: Chiridion::Engine::ClassLinker

Inherits:
Object
  • Object
show all
Defined in:
lib/chiridion/engine/class_linker.rb

Overview

Converts class/module references to Obsidian wikilinks.

Handles various reference formats:

  • Full paths: ‘Autopax::Foo::Bar` → `[[foo/bar|Bar]]`

  • YARD curly braces: ‘Extractor` → `[[extractor|Extractor]]`

  • Relative names: ‘Writer` → `[[writer|Writer]]` (within same namespace)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace_strip: nil) ⇒ ClassLinker

Returns a new instance of ClassLinker.



18
19
20
21
# File 'lib/chiridion/engine/class_linker.rb', line 18

def initialize(namespace_strip: nil)
  @namespace_strip = namespace_strip
  @known_classes   = {}
end

Instance Attribute Details

#known_classesHash<String, String> (readonly)

Returns Known classes mapped to doc paths.

Returns:

  • (Hash<String, String>)

    Known classes mapped to doc paths



13
14
15
# File 'lib/chiridion/engine/class_linker.rb', line 13

def known_classes
  @known_classes
end

#namespace_stripString? (readonly)

Returns Namespace prefix to strip from paths.

Returns:

  • (String, nil)

    Namespace prefix to strip from paths



16
17
18
# File 'lib/chiridion/engine/class_linker.rb', line 16

def namespace_strip
  @namespace_strip
end

Instance Method Details

#known?(class_name) ⇒ Boolean

Check if a class is a known documentable class.

Parameters:

  • class_name (String)

    Class name to check

Returns:

  • (Boolean)


92
# File 'lib/chiridion/engine/class_linker.rb', line 92

def known?(class_name) = @known_classes.key?(class_name)

Convert a class path to a wikilink.

Parameters:

  • class_path (String)

    Full or relative class path

  • context (String, nil) (defaults to: nil)

    Current class context for relative resolution

Returns:

  • (String)

    Wikilink like ‘[[path|Name]]` or original if not found



41
42
43
44
45
46
47
# File 'lib/chiridion/engine/class_linker.rb', line 41

def link(class_path, context: nil)
  display_name = class_path.split("::").last
  resolved     = resolve(class_path, context: context)
  return display_name unless resolved

  "[[#{resolved}|#{display_name}]]"
end

#linkify_docstring(text, context: nil) ⇒ String

Process a docstring, converting Class references to wikilinks.

Parameters:

  • text (String)

    Docstring text

  • context (String, nil) (defaults to: nil)

    Current class context

Returns:

  • (String)

    Text with Class converted to wikilinks



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chiridion/engine/class_linker.rb', line 54

def linkify_docstring(text, context: nil)
  return text if text.nil? || text.empty?

  result = text.dup

  # Convert YARD headings (= Title) to markdown headings
  # Direct 1:1 conversion; normalize_headers filter will adjust levels for context
  result.gsub!(/^(=+)\s+(.+)$/) do
    level = Regexp.last_match(1).length
    "#" * level + " " + Regexp.last_match(2)
  end

  # Convert {Class} references to wikilinks
  result.gsub(/\{([A-Z][\w:]*)\}/) do |_match|
    class_ref = Regexp.last_match(1)
    link(class_ref, context: context)
  end
end

#linkify_type(type_str, context: nil) ⇒ String

Convert a type annotation to include wikilinks where possible.

Returns formatted string with backticks around non-link parts. Wikilinks must be outside backticks to render properly.

Parameters:

  • type_str (String)

    Type like ‘Array<Autopax::Foo>` or `Hash=> Bar`

  • context (String, nil) (defaults to: nil)

    Current class context

Returns:

  • (String)

    Formatted type with proper backtick placement



81
82
83
84
85
86
# File 'lib/chiridion/engine/class_linker.rb', line 81

def linkify_type(type_str, context: nil)
  return "`Object`" if type_str.nil? || type_str.empty?

  segments = build_type_segments(type_str, context: context)
  format_type_segments(segments)
end

#register_classes(structure) ⇒ Object

Register known classes from the documentation structure.

Parameters:

  • structure (Hash)

    Documentation structure from Extractor



26
27
28
29
30
31
32
33
34
# File 'lib/chiridion/engine/class_linker.rb', line 26

def register_classes(structure)
  (structure[:classes] + structure[:modules]).each do |obj|
    path                         = obj[:path]
    @known_classes[path]         = doc_path(path)
    # Also register short name for relative lookups
    short_name                   = path.split("::").last
    @known_classes[short_name] ||= doc_path(path)
  end
end

#skip_type?(class_ref) ⇒ Boolean

Returns:

  • (Boolean)


98
# File 'lib/chiridion/engine/class_linker.rb', line 98

def skip_type?(class_ref) = SKIP_TYPES.include?(class_ref)