diff --git a/lib/hamlit/cli.rb b/lib/hamlit/cli.rb index 53c0aeba..3c049e50 100644 --- a/lib/hamlit/cli.rb +++ b/lib/hamlit/cli.rb @@ -3,8 +3,6 @@ require 'thor' module Hamlit class CLI < Thor - class_option :style, type: :string, aliases: ['-t'], default: 'ugly' - desc 'render HAML', 'Render haml template' def render(file) code = generate_code(file) @@ -25,7 +23,7 @@ module Hamlit def generate_code(file) template = File.read(file) - Hamlit::HamlEngine.new(template, haml_options).precompiled + Hamlit::Engine.new.call(template) end def generate_ast(file) @@ -33,10 +31,6 @@ module Hamlit Hamlit::Parser.new.call(template) end - def haml_options - { ugly: options['style'] == 'ugly' } - end - # Flexible default_task, compatible with haml's CLI def method_missing(*args) return super(*args) if args.length > 1 diff --git a/lib/hamlit/engine.rb b/lib/hamlit/engine.rb index cd833c21..ca766873 100644 --- a/lib/hamlit/engine.rb +++ b/lib/hamlit/engine.rb @@ -2,7 +2,6 @@ require 'temple' require 'hamlit/parser' require 'hamlit/compiler' require 'hamlit/html' -require 'pretty_hamlit/engine' module Hamlit class Engine < Temple::Engine diff --git a/lib/hamlit/parser.rb b/lib/hamlit/parser.rb index 7fbe54d2..489688d8 100644 --- a/lib/hamlit/parser.rb +++ b/lib/hamlit/parser.rb @@ -5,6 +5,7 @@ module Hamlit AVAILABLE_OPTIONS = %i[ autoclose escape_html + escape_attrs ].freeze def initialize(options = {}) diff --git a/lib/pretty_hamlit/compiler.rb b/lib/pretty_hamlit/compiler.rb deleted file mode 100644 index 69a3503c..00000000 --- a/lib/pretty_hamlit/compiler.rb +++ /dev/null @@ -1,41 +0,0 @@ -require 'pretty_hamlit/compiler/children_compiler' -require 'pretty_hamlit/compiler/tag_compiler' -require 'pretty_hamlit/filters' - -module PrettyHamlit - class Compiler < Hamlit::Compiler - def initialize(options = {}) - super - @indent_level = 0 - - @children_compiler = ChildrenCompiler.new - @tag_compiler = TagCompiler.new(options) - - @filter_compiler = Filters.new(options) - end - - private - - def compile_children(node) - @children_compiler.compile(node, @indent_level) { |n| compile(n) } - end - - def compile_comment(node) - @indent_level += 1 - super - ensure - @indent_level -= 1 - end - - def compile_filter(node) - @filter_compiler.compile(node, @indent_level) - end - - def compile_tag(node) - @indent_level += 1 - @tag_compiler.compile(node, @indent_level) { |n| compile_children(n) } - ensure - @indent_level -= 1 - end - end -end diff --git a/lib/pretty_hamlit/compiler/children_compiler.rb b/lib/pretty_hamlit/compiler/children_compiler.rb deleted file mode 100644 index d5b6c79a..00000000 --- a/lib/pretty_hamlit/compiler/children_compiler.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'hamlit/compiler/children_compiler' - -module PrettyHamlit - class Compiler < Hamlit::Compiler - class ChildrenCompiler < Hamlit::Compiler::ChildrenCompiler - def compile(node, indent_level, &block) - temple = [:multi] - return temple if node.children.empty? - - temple << :whitespace if prepend_whitespace?(node) - node.children.each do |n| - rstrip_whitespace!(temple) if nuke_outer_whitespace?(n) - insert_newlines!(temple, n) - temple << yield(n) - if insert_whitespace?(n) - if nuke_inner_whitespace?(node) - temple << :weak_whitespace - else - temple << :whitespace - end - end - end - rstrip_whitespace!(temple) if nuke_inner_whitespace?(node) - weaken_last_whitespace!(temple) - confirm_whitespace(temple, indent_level) - end - - private - - def rstrip_whitespace!(temple) - if %i[whitespace weak_whitespace].include?(temple[-1]) - temple.delete_at(-1) - end - end - - def weaken_last_whitespace!(temple) - if temple[-1] == :whitespace - temple.delete_at(-1) - temple << :weak_whitespace - end - end - - def confirm_whitespace(temple, indent_level) - temple.map do |exp| - case exp - when :whitespace - [:static, "\n" + (' ' * indent_level)] - when :weak_whitespace - level = [0, indent_level - 1].max - [:static, "\n" + (' ' * level)] - else - exp - end - end - end - end - end -end diff --git a/lib/pretty_hamlit/compiler/tag_compiler.rb b/lib/pretty_hamlit/compiler/tag_compiler.rb deleted file mode 100644 index 6740af61..00000000 --- a/lib/pretty_hamlit/compiler/tag_compiler.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'hamlit/compiler/tag_compiler' - -module PrettyHamlit - class Compiler < Hamlit::Compiler - class TagCompiler < Hamlit::Compiler::TagCompiler - def initialize(options = {}) - super - @buffer = options[:buffer] - end - - def compile(node, indent_level, &block) - attrs = @attribute_compiler.compile(node) - contents = compile_contents(node, indent_level, &block) - [:html, :tag, node.value[:name], attrs, contents] - end - - private - - def compile_contents(node, indent_level, &block) - case - when !node.children.empty? - yield(node) - when node.value[:value].nil? && self_closing?(node) - nil - when node.value[:parse] - [:multi, - [:code, "#{@buffer} << ::PrettyHamlit::DynamicIndentation.inline_or_expand(#{indent_level}) do |#{@buffer}|"], - [:dynamic, node.value[:value]], - [:code, 'end'], - ] - when Haml::Util.contains_interpolation?(node.value[:value]) - [:dynamic, node.value[:value]] - else - [:static, node.value[:value]] - end - end - end - end -end diff --git a/lib/pretty_hamlit/dynamic_indentation.rb b/lib/pretty_hamlit/dynamic_indentation.rb deleted file mode 100644 index 7a275d02..00000000 --- a/lib/pretty_hamlit/dynamic_indentation.rb +++ /dev/null @@ -1,33 +0,0 @@ -module PrettyHamlit - class DynamicIndentation - class << self - def indent_with(indent_level, &block) - text = capture(&block) - insert_indent(text, indent_level) - end - - def inline_or_expand(indent_level, &block) - text = capture(&block) - return text unless text.include?("\n") - - indent_space(indent_level) << - insert_indent(text, indent_level) << - indent_space(indent_level - 1) - end - - private - - def indent_space(indent_level) - "\n" << ' ' * [indent_level, 0].max - end - - def insert_indent(text, indent_level) - text.gsub("\n", indent_space(indent_level)) - end - - def capture(&block) - block.call([]).compact.join.rstrip - end - end - end -end diff --git a/lib/pretty_hamlit/engine.rb b/lib/pretty_hamlit/engine.rb deleted file mode 100644 index 76db1819..00000000 --- a/lib/pretty_hamlit/engine.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'temple' -require 'hamlit/parser' -require 'hamlit/html' -require 'pretty_hamlit/compiler' -require 'pretty_hamlit/dynamic_indentation' - -module PrettyHamlit - class Engine < Temple::Engine - define_options( - generator: Temple::Generators::ArrayBuffer, - format: :html, - html_type: nil, - attr_quote: "'", - escape_html: true, - escape_attrs: true, - buffer: '_buf', - autoclose: %w(area base basefont br col command embed frame - hr img input isindex keygen link menuitem meta - param source track wbr), - filename: "", - ) - - use Hamlit::Parser - use Compiler - use Hamlit::HTML - filter :Escapable - filter :ControlFlow - filter :MultiFlattener - filter :StaticMerger - use :Generator, -> { options[:generator] } - end -end diff --git a/lib/pretty_hamlit/filters.rb b/lib/pretty_hamlit/filters.rb deleted file mode 100644 index 47d51d42..00000000 --- a/lib/pretty_hamlit/filters.rb +++ /dev/null @@ -1,51 +0,0 @@ -require 'hamlit/filters' -require 'pretty_hamlit/filters/plain' - -module PrettyHamlit - class Filters - @registered = Hamlit::Filters.registered.dup - - class << self - attr_reader :registered - - private - - def register(name, compiler) - registered[name] = compiler - end - end - - register :plain, Plain - - def initialize(options = {}) - @options = options - end - - def compile(node, indent_level) - node.value[:text] ||= '' - content = find_compiler(node.value[:name]).compile(node) - [:multi, - [:code, "#{@options[:buffer]} << ::PrettyHamlit::DynamicIndentation.indent_with(#{indent_level}) do |#{@options[:buffer]}|"], - content, - [:code, 'end'], - ] - end - - private - - def find_compiler(name) - name = name.to_sym - compiler = Filters.registered[name] - raise NotFound.new("FilterCompiler for '#{name}' was not found") unless compiler - - compilers[name] ||= compiler.new(@options) - end - - def compilers - @compilers ||= {} - end - - class NotFound < RuntimeError - end - end -end diff --git a/lib/pretty_hamlit/filters/plain.rb b/lib/pretty_hamlit/filters/plain.rb deleted file mode 100644 index ff61ad91..00000000 --- a/lib/pretty_hamlit/filters/plain.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'hamlit/filters/plain' - -module PrettyHamlit - class Filters - class Plain < Hamlit::Filters::Plain - def compile(node) - text = node.value[:text].rstrip - if Haml::Util.contains_interpolation?(text) - text = Haml::Util.unescape_interpolation(text) - [:escape, true, [:dynamic, text]] - else - [:static, text] - end - end - end - end -end