Class: Chiridion::Engine::GithubLinker

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

Overview

Generates GitHub source links from file paths and line numbers.

Parses git remote URL to extract org/repo, then constructs blob URLs with line references for linking documentation back to source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo: nil, branch: "main", root: Dir.pwd) ⇒ GithubLinker

Returns a new instance of GithubLinker.

Parameters:

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

    Explicit GitHub repo (e.g., “org/repo”)

  • branch (String) (defaults to: "main")

    Git branch for links

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

    Project root for detecting git remote



19
20
21
22
# File 'lib/chiridion/engine/github_linker.rb', line 19

def initialize(repo: nil, branch: "main", root: Dir.pwd)
  @branch   = branch
  @base_url = repo ? "https://github.com/#{repo}" : extract_github_base_url(root)
end

Instance Attribute Details

#base_urlString? (readonly)

Returns GitHub base URL (e.g., “github.com/org/repo”).

Returns:



11
12
13
# File 'lib/chiridion/engine/github_linker.rb', line 11

def base_url
  @base_url
end

#branchString (readonly)

Returns Git branch for source links.

Returns:

  • (String)

    Git branch for source links



14
15
16
# File 'lib/chiridion/engine/github_linker.rb', line 14

def branch
  @branch
end

Instance Method Details

Generate a markdown link to a source location on GitHub.

Parameters:

  • path (String)

    Project-relative file path

  • start_line (Integer)

    Starting line number

  • end_line (Integer, nil) (defaults to: nil)

    Ending line number (optional)

Returns:

  • (String)

    Markdown link or plain text if no GitHub remote



30
31
32
33
34
35
36
# File 'lib/chiridion/engine/github_linker.rb', line 30

def link(path, start_line, end_line = nil)
  text = format_text(path, start_line, end_line)
  return "`#{text}`" unless @base_url

  url = format_url(path, start_line, end_line)
  "[#{text}](#{url})"
end

#url(path, start_line, end_line = nil) ⇒ String?

Generate just the URL (for frontmatter).

Parameters:

  • path (String)

    Project-relative file path

  • start_line (Integer)

    Starting line number

  • end_line (Integer, nil) (defaults to: nil)

    Ending line number (optional)

Returns:

  • (String, nil)

    GitHub URL or nil if no GitHub remote



44
45
46
47
48
# File 'lib/chiridion/engine/github_linker.rb', line 44

def url(path, start_line, end_line = nil)
  return nil unless @base_url

  format_url(path, start_line, end_line)
end