From 3ca802efe4c6d1c4aa2416709456f5857d59a7e9 Mon Sep 17 00:00:00 2001 From: Steve Shreeve Date: Mon, 16 Sep 2024 18:19:05 -0600 Subject: [PATCH 1/2] Add a :compact option that will strip whitespace on terminal nodes --- lib/temple/html/pretty.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/temple/html/pretty.rb b/lib/temple/html/pretty.rb index 6143573..a152864 100644 --- a/lib/temple/html/pretty.rb +++ b/lib/temple/html/pretty.rb @@ -10,7 +10,8 @@ class Pretty < Fast header hgroup hr html li link meta nav ol option p rp rt ruby section script style table tbody td tfoot th thead tr ul video doctype).freeze, - pre_tags: %w(code pre textarea).freeze + pre_tags: %w(code pre textarea).freeze, + compact: false def initialize(opts = {}) super @@ -62,6 +63,18 @@ def on_html_tag(name, attrs, content = nil) result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile(attrs)] result << [:static, (closed && @format != :html ? ' /' : '') + '>'] + # strip newlines around terminal nodes + @pretty = true + case content + in [:multi, [:newline]] + return (result << [:static, ""]) + in [:multi, [:multi, [:static, str]]] + return (result << [:static, "#{str}"]) + in [:multi, [:escape, true, [:dynamic, code]], [:multi, [:newline]]] + return (result << [:multi, [:escape, true, [:dynamic, code]], [:static, ""]]) + else nil + end + @pretty = !@pre_tags || !options[:pre_tags].include?(name) if content @indent += 1 From d9b20e58436bb1484ce040c248a0dd07c3ade868 Mon Sep 17 00:00:00 2001 From: Steve Shreeve Date: Mon, 16 Sep 2024 18:21:57 -0600 Subject: [PATCH 2/2] Enable the :compact option --- lib/temple/html/pretty.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/temple/html/pretty.rb b/lib/temple/html/pretty.rb index a152864..29ad071 100644 --- a/lib/temple/html/pretty.rb +++ b/lib/temple/html/pretty.rb @@ -73,7 +73,7 @@ def on_html_tag(name, attrs, content = nil) in [:multi, [:escape, true, [:dynamic, code]], [:multi, [:newline]]] return (result << [:multi, [:escape, true, [:dynamic, code]], [:static, ""]]) else nil - end + end if options[:compact] @pretty = !@pre_tags || !options[:pre_tags].include?(name) if content