Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/herb/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
class Herb::CLI
include Herb::Colors

attr_accessor :json, :silent, :no_interactive, :no_log_file, :no_timing, :local, :escape, :no_escape, :freeze, :debug, :tool, :strict
attr_accessor :json, :silent, :no_interactive, :no_log_file, :no_timing, :local, :escape, :no_escape, :freeze, :debug, :tool, :strict,
:auto_close_tags

def initialize(args)
@args = args
Expand Down Expand Up @@ -246,6 +247,10 @@ def option_parser
self.strict = false
end

parser.on("--auto-close-tags", "Auto-insert closing tags for elements with omitted closing tags (for compile/render commands)") do
self.auto_close_tags = true
end

parser.on("--tool TOOL", "Show config for specific tool: linter, formatter (for config command)") do |t|
self.tool = t.to_sym
end
Expand Down Expand Up @@ -294,6 +299,7 @@ def compile_template
options[:escape] = no_escape ? false : true
options[:freeze] = true if freeze
options[:strict] = strict.nil? || strict
options[:auto_close_omitted_tags] = auto_close_tags

if debug
options[:debug] = true
Expand All @@ -309,6 +315,7 @@ def compile_template
filename: engine.filename,
bufvar: engine.bufvar,
strict: options[:strict],
auto_close_omitted_tags: options[:auto_close_omitted_tags],
}

puts result.to_json
Expand Down Expand Up @@ -362,6 +369,7 @@ def render_template
options[:escape] = no_escape ? false : true
options[:freeze] = true if freeze
options[:strict] = strict.nil? || strict
options[:auto_close_omitted_tags] = auto_close_tags

if debug
options[:debug] = true
Expand All @@ -379,6 +387,7 @@ def render_template
output: rendered_output,
filename: engine.filename,
strict: options[:strict],
auto_close_omitted_tags: options[:auto_close_omitted_tags],
}

puts result.to_json
Expand Down
3 changes: 2 additions & 1 deletion lib/herb/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
module Herb
class Engine
attr_reader :src, :filename, :project_path, :relative_file_path, :bufvar, :debug, :content_for_head,
:validation_error_template, :visitors
:validation_error_template, :visitors, :auto_close_omitted_tags

ESCAPE_TABLE = {
"&" => "&",
Expand Down Expand Up @@ -52,6 +52,7 @@ def initialize(input, properties = {})
@validation_error_template = nil
@validation_mode = properties.fetch(:validation_mode, :raise)
@strict = properties.fetch(:strict, true)
@auto_close_omitted_tags = properties.fetch(:auto_close_omitted_tags, false)
@visitors = properties.fetch(:visitors, default_visitors)

if @debug && @visitors.empty?
Expand Down
14 changes: 13 additions & 1 deletion lib/herb/engine/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(engine, options = {})
@attrfunc = options.fetch(:attrfunc, @escape ? "__herb.attr" : "::Herb::Engine.attr")
@jsfunc = options.fetch(:jsfunc, @escape ? "__herb.js" : "::Herb::Engine.js")
@cssfunc = options.fetch(:cssfunc, @escape ? "__herb.css" : "::Herb::Engine.css")
@auto_close_omitted_tags = options.fetch(:auto_close_omitted_tags, false)
@tokens = [] #: Array[untyped]
@element_stack = [] #: Array[String]
@context_stack = [:html_content]
Expand Down Expand Up @@ -146,7 +147,18 @@ def visit_html_close_tag_node(node)
end

def visit_html_omitted_close_tag_node(node)
# no-op
return unless @auto_close_omitted_tags

tag_name = node.tag_name&.value

if @engine.content_for_head && tag_name&.downcase == "head"
escaped_html = @engine.content_for_head.gsub("'", "\\\\'")
@tokens << [:expr, "'#{escaped_html}'.html_safe", current_context]
end

add_text("</")
add_text(tag_name)
add_text(">")
end

def visit_html_text_node(node)
Expand Down
Loading
Loading