From 1f0dd6d84a5f1ff6c6343d92c78c1973ea77a21d Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Sun, 4 Oct 2020 16:30:04 +0530 Subject: [PATCH 1/3] Memoize feed tag markup string --- lib/jekyll-feed/meta-tag.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/jekyll-feed/meta-tag.rb b/lib/jekyll-feed/meta-tag.rb index 54759297..9b5059bf 100644 --- a/lib/jekyll-feed/meta-tag.rb +++ b/lib/jekyll-feed/meta-tag.rb @@ -6,16 +6,19 @@ class MetaTag < Liquid::Tag include Jekyll::Filters::URLFilters def render(context) - @context = context - attrs = attributes.map do |k, v| - v = v.to_s unless v.respond_to?(:encode) - %(#{k}=#{v.encode(:xml => :attr)}) - end - "" + @context ||= context + memoized_result end private + def memoized_result + @memoized_result ||= begin + attrs = attributes.map { |k, v| "#{k}=#{v.to_s.encode(:xml => :attr)}" } + "" + end + end + def config @config ||= @context.registers[:site].config end From 6862469212f74cf179e90ba55092e1fd4a215e80 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Mon, 5 Oct 2020 20:04:57 +0530 Subject: [PATCH 2/3] Replace private methods with local variables --- lib/jekyll-feed/meta-tag.rb | 38 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/jekyll-feed/meta-tag.rb b/lib/jekyll-feed/meta-tag.rb index 9b5059bf..9780b563 100644 --- a/lib/jekyll-feed/meta-tag.rb +++ b/lib/jekyll-feed/meta-tag.rb @@ -6,38 +6,32 @@ class MetaTag < Liquid::Tag include Jekyll::Filters::URLFilters def render(context) - @context ||= context - memoized_result + # Jekyll::Filters::URLFilters requires `@context` to be set in the environment. + @context = context + xml_encoded_link_tag end private - def memoized_result - @memoized_result ||= begin - attrs = attributes.map { |k, v| "#{k}=#{v.to_s.encode(:xml => :attr)}" } - "" - end - end - def config @config ||= @context.registers[:site].config end - def attributes - { - :type => "application/atom+xml", - :rel => "alternate", - :href => absolute_url(path), - :title => title, - }.keep_if { |_, v| v } - end + def xml_encoded_link_tag + @xml_encoded_link_tag ||= begin + path = config.dig("feed", "path") || "feed.xml" + title = config["title"] || config["name"] - def path - config.dig("feed", "path") || "feed.xml" - end + attributes = { + :type => "application/atom+xml", + :rel => "alternate", + :href => absolute_url(path), + } + attributes[:title] = title if title - def title - config["title"] || config["name"] + attrs = attributes.map { |k, v| "#{k}=#{v.to_s.encode(:xml => :attr)}" }.join(" ") + "" + end end end end From 9ffd1827406d137e5861191921e605ac16a5d96f Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Mon, 5 Oct 2020 20:19:20 +0530 Subject: [PATCH 3/3] Fold all private methods into method `:render` --- lib/jekyll-feed/meta-tag.rb | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/jekyll-feed/meta-tag.rb b/lib/jekyll-feed/meta-tag.rb index 9780b563..d143c728 100644 --- a/lib/jekyll-feed/meta-tag.rb +++ b/lib/jekyll-feed/meta-tag.rb @@ -8,30 +8,20 @@ class MetaTag < Liquid::Tag def render(context) # Jekyll::Filters::URLFilters requires `@context` to be set in the environment. @context = context - xml_encoded_link_tag - end - - private - - def config - @config ||= @context.registers[:site].config - end - def xml_encoded_link_tag - @xml_encoded_link_tag ||= begin - path = config.dig("feed", "path") || "feed.xml" - title = config["title"] || config["name"] + config = context.registers[:site].config + path = config.dig("feed", "path") || "feed.xml" + title = config["title"] || config["name"] - attributes = { - :type => "application/atom+xml", - :rel => "alternate", - :href => absolute_url(path), - } - attributes[:title] = title if title + attributes = { + :type => "application/atom+xml", + :rel => "alternate", + :href => absolute_url(path), + } + attributes[:title] = title if title - attrs = attributes.map { |k, v| "#{k}=#{v.to_s.encode(:xml => :attr)}" }.join(" ") - "" - end + attrs = attributes.map { |k, v| "#{k}=#{v.to_s.encode(:xml => :attr)}" }.join(" ") + "" end end end