Class: Bridgetown::Tags::PostUrl

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

Instance Method Summary collapse

Methods included from Filters::URLFilters

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

Constructor Details

#initialize(tag_name, post, tokens) ⇒ PostUrl

Returns a new instance of PostUrl.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'bridgetown-core/lib/bridgetown-core/tags/post_url.rb', line 57

def initialize(tag_name, post, tokens)
  super
  @orig_post = post.strip
  begin
    @post = PostComparer.new(@orig_post)
  rescue StandardError => e
    raise Bridgetown::Errors::PostURLError, <<~MSG
      Could not parse name of post "#{@orig_post}" in tag 'post_url'.
       Make sure the post exists and the name is correct.
       #{e.class}: #{e.message}
    MSG
  end
end

Instance Method Details

#render(context) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'bridgetown-core/lib/bridgetown-core/tags/post_url.rb', line 71

def render(context)
  @context = context
  site = context.registers[:site]

  site.collections.posts.resources.each do |document|
    return relative_url(document) if @post == document

    # New matching method did not match, fall back to old method
    # with deprecation warning if this matches
    next unless @post.deprecated_equality document

    Bridgetown::Deprecator.deprecation_message(
      "A call to " \
      "'{% post_url #{@post.name} %}' did not match " \
      "a post using the new matching method of checking name " \
      "(path-date-slug) equality. Please make sure that you " \
      "change this tag to match the post's name exactly."
    )

    return relative_url(document)
  end

  raise Bridgetown::Errors::PostURLError, <<~MSG
    Could not find post "#{@orig_post}" in tag 'post_url'.
    Make sure the post exists and the name is correct.
  MSG
end