From 0384e3fa69fc6858a5a57aba16eed545c1680f44 Mon Sep 17 00:00:00 2001 From: Thyrum Date: Wed, 24 Jul 2024 09:41:08 +0000 Subject: [PATCH] Allow for directives with all whitespace value A directive with an empty (all whitespace) value like ``` {subtitle: } ``` will now result in a subtitle with value "". This is done in order to make the parsing more robust. --- lib/chordpro/html.rb | 4 +++- lib/chordpro/transform.rb | 5 +++++ spec/chordpro/html_spec.rb | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/chordpro/html.rb b/lib/chordpro/html.rb index b06bf67..5fe515d 100644 --- a/lib/chordpro/html.rb +++ b/lib/chordpro/html.rb @@ -18,7 +18,9 @@ def title(title) alias_method :t, :title def subtitle(subtitle) - @html.h2(class: "subtitle") { |h2| h2.text! subtitle } + unless subtitle.match(/^\s*$/) + @html.h2(class: "subtitle") { |h2| h2.text! subtitle } + end end alias_method :st, :subtitle alias_method :su, :subtitle diff --git a/lib/chordpro/transform.rb b/lib/chordpro/transform.rb index 4803519..edf9e4c 100644 --- a/lib/chordpro/transform.rb +++ b/lib/chordpro/transform.rb @@ -5,6 +5,11 @@ class Transform < Parslet::Transform Chordpro::Directive.new(directive_name, value.to_s) end + rule(directive: {name: simple(:name), value: []}) do + directive_name = Directive.find(name) || Directive::Name.new(name.to_s) + Chordpro::Directive.new(directive_name, "") + end + rule(directive: {name: simple(:name)}) do directive_name = Directive.find(name) || Directive::Name.new(name.to_s) Chordpro::Directive.new(directive_name) diff --git a/spec/chordpro/html_spec.rb b/spec/chordpro/html_spec.rb index 5b44922..b0998c6 100644 --- a/spec/chordpro/html_spec.rb +++ b/spec/chordpro/html_spec.rb @@ -19,6 +19,9 @@ def html(string) it "renders h2 for #{name}" do expect(html("{#{name}:The Beatles}").to_s).to eq('

The Beatles

') end + it "allows empty #{name}" do + expect(html("{#{name}: }").to_s).to eq('') + end end it "guards against xss in the subtitle" do