From eb37d75c85987b58b41fcd9db9bfae279f270bc2 Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Thu, 28 Feb 2019 21:51:20 +0100 Subject: [PATCH] Implement Blogcast liquid tag (#1891) --- app/liquid_tags/blogcast_tag.rb | 38 +++++++++++++++++++++ app/views/pages/_editor_guide_text.html.erb | 6 ++++ spec/liquid_tags/blogcast_tag_spec.rb | 21 ++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 app/liquid_tags/blogcast_tag.rb create mode 100644 spec/liquid_tags/blogcast_tag_spec.rb diff --git a/app/liquid_tags/blogcast_tag.rb b/app/liquid_tags/blogcast_tag.rb new file mode 100644 index 000000000..f77dd3c13 --- /dev/null +++ b/app/liquid_tags/blogcast_tag.rb @@ -0,0 +1,38 @@ +class BlogcastTag < LiquidTagBase + def initialize(tag_name, id, tokens) + super + @id = parse_id(id) + end + + def render(_context) + html = <<-HTML +
+ +
+ HTML + finalize_html(html) + end + + private + + def parse_id(input) + input_no_space = input.delete(" ") + if valid_id?(input_no_space) + input_no_space + else + raise StandardError, "Invalid Blogcast Id" + end + end + + def valid_id?(id) + (id =~ /\A\d{1,9}\Z/i)&.zero? + end +end + +Liquid::Template.register_tag("blogcast", BlogcastTag) diff --git a/app/views/pages/_editor_guide_text.html.erb b/app/views/pages/_editor_guide_text.html.erb index 31b75a9aa..28b7f78ca 100644 --- a/app/views/pages/_editor_guide_text.html.erb +++ b/app/views/pages/_editor_guide_text.html.erb @@ -226,6 +226,12 @@
       {% spotify spotify:episode:5V4XZWqZQJvbddd31n56mf %}
     
+ +

Blogcast Tag

+

All you need is the article id code from the embed code:

+
+      {% blogcast 1234 %}
+    

Parsing Liquid Tags as a Code Example

To parse Liquid tags as code, simply wrap it with a single backtick or triple backticks.

diff --git a/spec/liquid_tags/blogcast_tag_spec.rb b/spec/liquid_tags/blogcast_tag_spec.rb new file mode 100644 index 000000000..dc86eeed8 --- /dev/null +++ b/spec/liquid_tags/blogcast_tag_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.describe BlogcastTag, type: :liquid_template do + describe "#id" do + let(:valid_id) { "1234" } + let(:invalid_id) { "inv@lid" } + + def generate_tag(id) + Liquid::Template.register_tag("blogcast", BlogcastTag) + Liquid::Template.parse("{% blogcast #{id} %}") + end + + it "rejects invalid ids" do + expect { generate_tag(invalid_id) }.to raise_error(StandardError) + end + + it "accepts a valid id" do + expect { generate_tag(valid_id) }.not_to raise_error + end + end +end