Class: Bridgetown::Tags::Link

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Filters::URLFilters
Defined in:
bridgetown-core/lib/bridgetown-core/tags/link.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filters::URLFilters

#absolute_url, #in_locale, #relative_url, #strip_extname, #strip_index

Constructor Details

#initialize(tag_name, relative_path, tokens) ⇒ Link

Returns a new instance of Link.



14
15
16
17
18
# File 'bridgetown-core/lib/bridgetown-core/tags/link.rb', line 14

def initialize(tag_name, relative_path, tokens)
  super

  @relative_path = relative_path.strip
end

Class Method Details

.tag_nameObject



9
10
11
# File 'bridgetown-core/lib/bridgetown-core/tags/link.rb', line 9

def tag_name
  name.split("::").last.downcase
end

Instance Method Details

#render(context) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'bridgetown-core/lib/bridgetown-core/tags/link.rb', line 20

def render(context)
  @context = context
  site = context.registers[:site]
  relative_path = Liquid::Template.parse(@relative_path).render(context)

  site.each_site_file do |item|
    # Resource engine:
    if item.respond_to?(:relative_url) && item.relative_path.to_s == relative_path
      return item.relative_url
    end
    return relative_url(item) if item.relative_path == relative_path
    # This takes care of the case for static files that have a leading /
    return relative_url(item) if item.relative_path == "/#{relative_path}"
  end

  raise ArgumentError, <<~MSG
    Could not find document '#{relative_path}' in tag '#{self.class.tag_name}'.

    Make sure the document exists and the path is correct.
  MSG
end