Skip to content
Draft
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
24 changes: 23 additions & 1 deletion lib/herb/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Herb::CLI
include Herb::Colors

attr_accessor :json, :silent, :log_file, :no_timing, :local, :escape, :no_escape, :freeze, :debug, :tool, :strict, :analyze, :track_whitespace, :verbose, :isolate, :arena_stats, :leak_check, :action_view_helpers, :trim, :optimize, :file_timeout
attr_accessor :json, :silent, :log_file, :no_timing, :local, :escape, :no_escape, :freeze, :debug, :tool, :strict, :analyze, :track_whitespace, :verbose, :isolate, :arena_stats, :leak_check, :action_view_helpers, :trim, :optimize, :slots, :file_timeout

def initialize(args)
@args = args
Expand Down Expand Up @@ -334,6 +334,10 @@ def option_parser
self.optimize = true
end

parser.on("--slots", "Emit slot markers for reactive rendering (for compile/render commands) (default: false)") do
self.slots = 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 @@ -688,9 +692,12 @@ def compile_template

options[:optimize] = true if optimize
options[:trim] = true if trim
options[:slots] = true if slots
options[:validate_ruby] = true
engine = Herb::Engine.new(file_content, options)

print_warnings(engine.slot_visitor&.warnings || []) unless json

if json
result = {
success: true,
Expand Down Expand Up @@ -760,6 +767,21 @@ def compile_template
end
end

def print_warnings(warnings)
return if warnings.empty?

$stderr.puts

warnings.each do |warning|
location = [@file, warning.location&.start&.line, warning.location&.start&.column].compact.join(":")

warn "#{bold(yellow("warning"))}: #{warning.message}"
warn " #{dimmed(location)}"
warn " #{dimmed(warning.expression.to_s)}" if warning.respond_to?(:expression) && warning.expression
$stderr.puts
end
end

def render_template
require_relative "engine"

Expand Down
16 changes: 14 additions & 2 deletions lib/herb/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "pathname"

require_relative "engine/debug_visitor"
require_relative "engine/slot_visitor"
require_relative "engine/compiler"
require_relative "engine/error_formatter"
require_relative "engine/validation_errors"
Expand All @@ -18,8 +19,7 @@

module Herb
class Engine
attr_reader :src, :filename, :project_path, :relative_file_path, :bufvar, :debug, :content_for_head,
:validation_error_template, :visitors, :enabled_validators
attr_reader :src, :filename, :project_path, :relative_file_path, :bufvar, :debug, :content_for_head, :validation_error_template, :visitors, :enabled_validators, :slot_visitor

# @rbs!
# def self.optimize_warning_issued: () -> bool
Expand Down Expand Up @@ -76,6 +76,7 @@ def initialize(input, properties = {})
@chain_appends = properties[:chain_appends]
@buffer_on_stack = false
@debug = properties.fetch(:debug, Herb.configuration.engine_option("debug", false))
@slots = properties.fetch(:slots, Herb.configuration.engine_option("slots", false))
@content_for_head = properties[:content_for_head]
@validation_error_template = nil
@validation_mode = properties.fetch(:validation_mode, :raise)
Expand All @@ -100,6 +101,17 @@ def initialize(input, properties = {})
@visitors << debug_visitor
end

if @slots
@parser_options[:iteration_nodes] = true unless @parser_options.key?(:iteration_nodes)

@slot_visitor = SlotVisitor.new(
file_path: @filename,
project_path: @project_path
)

@visitors = [@slot_visitor, *@visitors]
end

unless [:raise, :overlay, :none].include?(@validation_mode)
raise ArgumentError,
"validation_mode must be one of :raise, :overlay, or :none, got #{@validation_mode.inspect}"
Expand Down
48 changes: 48 additions & 0 deletions lib/herb/engine/slot_markers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true
# typed: true

module Herb
class Engine
class SlotMarkers
#: (Integer, Symbol) -> String
def slot_open(index, _type)
"<!--herb-slot:#{index}-->"
end

#: (Integer) -> String
def slot_close(index)
"<!--/herb-slot:#{index}-->"
end

#: (Integer, Integer) -> String
def branch(slot_index, branch_index)
"<!--herb-branch:#{slot_index}:#{branch_index}-->"
end

#: (Integer) -> String
def row_open_prefix(slot_index)
"<!--herb-row:#{slot_index}:"
end

#: () -> String
def row_open_suffix
"-->"
end

#: (Integer) -> String
def row_close(slot_index)
"<!--/herb-row:#{slot_index}-->"
end

#: (String, String) -> String
def region_open(file, version)
"<!--herb-region:#{file}:#{version}-->"
end

#: (String) -> String
def region_close(file)
"<!--/herb-region:#{file}-->"
end
end
end
end
Loading
Loading