Class: Textus::Protocol::Links::UriRewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/protocol/links/uri_rewriter.rb

Constant Summary collapse

TEXTUS_URI =
/\[([^\]]*)\]\(textus:([^#)\s]+)(#[^)\s]*)?\)/

Instance Method Summary collapse

Constructor Details

#initialize(resolver:, from_path:, from_key: nil, edge_store: nil) ⇒ UriRewriter

Returns a new instance of UriRewriter.



7
8
9
10
11
12
# File 'lib/textus/protocol/links/uri_rewriter.rb', line 7

def initialize(resolver:, from_path:, from_key: nil, edge_store: nil)
  @resolver   = resolver
  @from_path  = from_path
  @from_key   = from_key
  @edge_store = edge_store
end

Instance Method Details

#rewrite(content) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/textus/protocol/links/uri_rewriter.rb', line 14

def rewrite(content)
  content.gsub(TEXTUS_URI) do
    text   = Regexp.last_match(1)
    key    = Regexp.last_match(2)
    anchor = Regexp.last_match(3).to_s

    resolved = @resolver.resolve(key: key, from_path: @from_path)
    @edge_store&.record(from_key: @from_key, to_key: key)
    "[#{text}](#{resolved}#{anchor})"
  rescue Resolver::UnknownKeyError
    "[#{text}](`textus get #{key}`)"
  end
end