From 82a240aa69b92b295ae5e92702e946c889e18b73 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 21 Nov 2015 02:55:12 +0900 Subject: [PATCH] Migrate to embedded internal Haml --- hamlit.gemspec | 2 +- lib/hamlit/compiler/script_compiler.rb | 4 +- lib/hamlit/compiler/tag_compiler.rb | 4 +- lib/hamlit/filters/base.rb | 2 +- lib/hamlit/filters/css.rb | 4 +- lib/hamlit/filters/escaped.rb | 6 +- lib/hamlit/filters/javascript.rb | 4 +- lib/hamlit/filters/plain.rb | 6 +- lib/hamlit/filters/preserve.rb | 6 +- lib/hamlit/filters/tilt_base.rb | 4 +- lib/hamlit/parser.rb | 12 +++- .../parser/{buffer.rb => haml_buffer.rb} | 22 +++--- .../parser/{compiler.rb => haml_compiler.rb} | 31 +++++---- lib/hamlit/parser/{error.rb => haml_error.rb} | 6 +- .../parser/{helpers.rb => haml_helpers.rb} | 30 ++++---- .../parser/{options.rb => haml_options.rb} | 14 ++-- .../parser/{parser.rb => haml_parser.rb} | 69 ++++++++++--------- lib/hamlit/parser/{util.rb => haml_util.rb} | 8 +-- sample/rails/Gemfile.lock | 3 - sample/sinatra/Gemfile.lock | 20 ++++++ test/haml/haml-spec/Rakefile | 1 + test/haml/haml-spec/ugly_test.rb | 1 + test/test_helper.rb | 1 + 23 files changed, 150 insertions(+), 110 deletions(-) rename lib/hamlit/parser/{buffer.rb => haml_buffer.rb} (94%) rename lib/hamlit/parser/{compiler.rb => haml_compiler.rb} (94%) rename lib/hamlit/parser/{error.rb => haml_error.rb} (97%) rename lib/hamlit/parser/{helpers.rb => haml_helpers.rb} (95%) rename lib/hamlit/parser/{options.rb => haml_options.rb} (96%) rename lib/hamlit/parser/{parser.rb => haml_parser.rb} (87%) rename lib/hamlit/parser/{util.rb => haml_util.rb} (97%) create mode 100644 sample/sinatra/Gemfile.lock diff --git a/hamlit.gemspec b/hamlit.gemspec index 0d455d8d..6dcfa484 100644 --- a/hamlit.gemspec +++ b/hamlit.gemspec @@ -20,13 +20,13 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_dependency 'escape_utils' - spec.add_dependency 'haml', '>= 4.0.7', '< 5.0' spec.add_dependency 'temple', '~> 0.7.6' spec.add_dependency 'tilt' spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'coffee-script' spec.add_development_dependency 'faml', '>= 0.3.3' + spec.add_development_dependency 'haml' spec.add_development_dependency 'less' spec.add_development_dependency 'minitest-reporters', '~> 1.1' spec.add_development_dependency 'rails', '>= 4.0.0' diff --git a/lib/hamlit/compiler/script_compiler.rb b/lib/hamlit/compiler/script_compiler.rb index c9b77956..5e189317 100644 --- a/lib/hamlit/compiler/script_compiler.rb +++ b/lib/hamlit/compiler/script_compiler.rb @@ -22,7 +22,7 @@ module Hamlit if node.value[:escape_html] str = Temple::Utils.escape_html(str) elsif node.value[:preserve] - str = Haml::Helpers.find_and_preserve(str, %w(textarea pre code)) + str = ::Hamlit::HamlHelpers.find_and_preserve(str, %w(textarea pre code)) end [:static, str] end @@ -60,7 +60,7 @@ module Hamlit end def find_and_preserve(code) - %Q[Haml::Helpers.find_and_preserve(#{code}, %w(textarea pre code))] + %Q[::Hamlit::HamlHelpers.find_and_preserve(#{code}, %w(textarea pre code))] end def escape_html(temple) diff --git a/lib/hamlit/compiler/tag_compiler.rb b/lib/hamlit/compiler/tag_compiler.rb index 72c04822..0d0948b6 100644 --- a/lib/hamlit/compiler/tag_compiler.rb +++ b/lib/hamlit/compiler/tag_compiler.rb @@ -1,4 +1,4 @@ -require 'haml/util' +require 'hamlit/parser/haml_util' require 'hamlit/compiler/attribute_compiler' module Hamlit @@ -25,7 +25,7 @@ module Hamlit nil when node.value[:parse] [:escape, node.value[:escape_html], [:dynamic, node.value[:value]]] - when Haml::Util.contains_interpolation?(node.value[:value]) + when ::Hamlit::HamlUtil.contains_interpolation?(node.value[:value]) [:dynamic, node.value[:value]] else [:static, node.value[:value]] diff --git a/lib/hamlit/filters/base.rb b/lib/hamlit/filters/base.rb index 51eee383..c7c47385 100644 --- a/lib/hamlit/filters/base.rb +++ b/lib/hamlit/filters/base.rb @@ -1,4 +1,4 @@ -require 'haml/util' +require 'hamlit/parser/haml_util' module Hamlit class Filters diff --git a/lib/hamlit/filters/css.rb b/lib/hamlit/filters/css.rb index 3c6c4947..5280300e 100644 --- a/lib/hamlit/filters/css.rb +++ b/lib/hamlit/filters/css.rb @@ -14,8 +14,8 @@ module Hamlit def compile_text!(temple, node, prefix) (node.value[:text].rstrip << "\n").each_line do |line| - if Haml::Util.contains_interpolation?(line) - temple << [:dynamic, Haml::Util.unescape_interpolation(prefix.dup << line)] + if ::Hamlit::HamlUtil.contains_interpolation?(line) + temple << [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(prefix.dup << line)] else temple << [:static, prefix.dup << line] end diff --git a/lib/hamlit/filters/escaped.rb b/lib/hamlit/filters/escaped.rb index 893de544..ec398e53 100644 --- a/lib/hamlit/filters/escaped.rb +++ b/lib/hamlit/filters/escaped.rb @@ -1,5 +1,3 @@ -require 'haml/util' - module Hamlit class Filters class Escaped < Base @@ -12,8 +10,8 @@ module Hamlit private def compile_text(text) - if Haml::Util.contains_interpolation?(text) - [:dynamic, Haml::Util.unescape_interpolation(text)] + if ::Hamlit::HamlUtil.contains_interpolation?(text) + [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(text)] else [:static, text] end diff --git a/lib/hamlit/filters/javascript.rb b/lib/hamlit/filters/javascript.rb index 0f858d99..75c1e5dd 100644 --- a/lib/hamlit/filters/javascript.rb +++ b/lib/hamlit/filters/javascript.rb @@ -14,8 +14,8 @@ module Hamlit def compile_text!(temple, node, prefix) (node.value[:text].rstrip << "\n").each_line do |line| - if Haml::Util.contains_interpolation?(line) - temple << [:dynamic, Haml::Util.unescape_interpolation(prefix.dup << line)] + if ::Hamlit::HamlUtil.contains_interpolation?(line) + temple << [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(prefix.dup << line)] else temple << [:static, prefix.dup << line] end diff --git a/lib/hamlit/filters/plain.rb b/lib/hamlit/filters/plain.rb index 0b9751bc..ebd15c9e 100644 --- a/lib/hamlit/filters/plain.rb +++ b/lib/hamlit/filters/plain.rb @@ -1,14 +1,12 @@ -require 'haml/util' - module Hamlit class Filters class Plain < Base def compile(node) text = node.value[:text].rstrip - if Haml::Util.contains_interpolation?(text) + if ::Hamlit::HamlUtil.contains_interpolation?(text) # FIXME: Confirm whether this is correct or not text << "\n".freeze - text = Haml::Util.unescape_interpolation(text) + text = ::Hamlit::HamlUtil.unescape_interpolation(text) [:escape, true, [:dynamic, text]] else [:static, text] diff --git a/lib/hamlit/filters/preserve.rb b/lib/hamlit/filters/preserve.rb index c97b4f70..ec322db3 100644 --- a/lib/hamlit/filters/preserve.rb +++ b/lib/hamlit/filters/preserve.rb @@ -1,5 +1,3 @@ -require 'haml/util' - module Hamlit class Filters class Preserve < Base @@ -12,8 +10,8 @@ module Hamlit private def compile_text(text) - if Haml::Util.contains_interpolation?(text) - [:dynamic, Haml::Util.unescape_interpolation(text)] + if ::Hamlit::HamlUtil.contains_interpolation?(text) + [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(text)] else [:static, text] end diff --git a/lib/hamlit/filters/tilt_base.rb b/lib/hamlit/filters/tilt_base.rb index 6a35f091..be17069f 100644 --- a/lib/hamlit/filters/tilt_base.rb +++ b/lib/hamlit/filters/tilt_base.rb @@ -12,7 +12,7 @@ module Hamlit private def compile_with_tilt(node, name, indent_width: 0) - if Haml::Util.contains_interpolation?(node.value[:text]) + if ::Hamlit::HamlUtil.contains_interpolation?(node.value[:text]) dynamic_compile(node, name, indent_width: indent_width) else static_compile(node, name, indent_width: indent_width) @@ -24,7 +24,7 @@ module Hamlit end def dynamic_compile(node, name, indent_width: 0) - source = Haml::Util.unescape_interpolation(node.value[:text]) + source = ::Hamlit::HamlUtil.unescape_interpolation(node.value[:text]) [:dynamic, "::Hamlit::Filters::TiltBase.render('#{name}', #{source}, indent_width: #{indent_width})"] end end diff --git a/lib/hamlit/parser.rb b/lib/hamlit/parser.rb index 489688d8..e06d2ff5 100644 --- a/lib/hamlit/parser.rb +++ b/lib/hamlit/parser.rb @@ -1,4 +1,10 @@ -require 'haml' +require 'hamlit/parser/haml_error' +require 'hamlit/parser/haml_util' +require 'hamlit/parser/haml_buffer' +require 'hamlit/parser/haml_compiler' +require 'hamlit/parser/haml_parser' +require 'hamlit/parser/haml_helpers' +require 'hamlit/parser/haml_options' module Hamlit class Parser @@ -9,14 +15,14 @@ module Hamlit ].freeze def initialize(options = {}) - @options = Haml::Options.defaults.dup + @options = HamlOptions.defaults.dup AVAILABLE_OPTIONS.each do |key| @options[key] = options[key] end end def call(template) - Haml::Parser.new(template, Haml::Options.new(@options)).parse + HamlParser.new(template, HamlOptions.new(@options)).parse end end end diff --git a/lib/hamlit/parser/buffer.rb b/lib/hamlit/parser/haml_buffer.rb similarity index 94% rename from lib/hamlit/parser/buffer.rb rename to lib/hamlit/parser/haml_buffer.rb index 05c03177..542e95ce 100644 --- a/lib/hamlit/parser/buffer.rb +++ b/lib/hamlit/parser/haml_buffer.rb @@ -1,15 +1,19 @@ -module Haml +require 'hamlit/parser/haml_helpers' +require 'hamlit/parser/haml_util' +require 'hamlit/parser/haml_compiler' + +module Hamlit # This class is used only internally. It holds the buffer of HTML that # is eventually output as the resulting document. # It's called from within the precompiled code, # and helps reduce the amount of processing done within `instance_eval`ed code. - class Buffer + class HamlBuffer ID_KEY = 'id'.freeze CLASS_KEY = 'class'.freeze DATA_KEY = 'data'.freeze - include Haml::Helpers - include Haml::Util + include ::Hamlit::HamlHelpers + include ::Hamlit::HamlUtil # The string that holds the compiled HTML. This is aliased as # `_erbout` for compatibility with ERB-specific code. @@ -194,7 +198,7 @@ module Haml self.class.merge_attrs(attributes, Hash[old.map {|k, v| [k.to_s, v]}]) end self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref - Compiler.build_attributes( + ::Hamlit::HamlCompiler.build_attributes( html?, @options[:attr_wrapper], @options[:escape_attrs], @options[:hyphenate_data_attrs], attributes) end @@ -223,14 +227,14 @@ module Haml # @param from [{String => #to_s}] The attribute hash to merge from # @return [{String => String}] `to`, after being merged def self.merge_attrs(to, from) - from[ID_KEY] = Compiler.filter_and_join(from[ID_KEY], '_') if from[ID_KEY] + from[ID_KEY] = ::Hamlit::HamlCompiler.filter_and_join(from[ID_KEY], '_') if from[ID_KEY] if to[ID_KEY] && from[ID_KEY] to[ID_KEY] << "_#{from.delete(ID_KEY)}" elsif to[ID_KEY] || from[ID_KEY] from[ID_KEY] ||= to[ID_KEY] end - from[CLASS_KEY] = Compiler.filter_and_join(from[CLASS_KEY], ' ') if from[CLASS_KEY] + from[CLASS_KEY] = ::Hamlit::HamlCompiler.filter_and_join(from[CLASS_KEY], ' ') if from[CLASS_KEY] if to[CLASS_KEY] && from[CLASS_KEY] # Make sure we don't duplicate class names from[CLASS_KEY] = (from[CLASS_KEY].to_s.split(' ') | to[CLASS_KEY].split(' ')).sort.join(' ') @@ -259,8 +263,8 @@ module Haml private def preserve(result, preserve_script, preserve_tag) - return Haml::Helpers.preserve(result) if preserve_tag - return Haml::Helpers.find_and_preserve(result, options[:preserve]) if preserve_script + return ::Hamlit::HamlHelpers.preserve(result) if preserve_tag + return ::Hamlit::HamlHelpers.find_and_preserve(result, options[:preserve]) if preserve_script result end diff --git a/lib/hamlit/parser/compiler.rb b/lib/hamlit/parser/haml_compiler.rb similarity index 94% rename from lib/hamlit/parser/compiler.rb rename to lib/hamlit/parser/haml_compiler.rb index 89e4d3d2..fbf02eeb 100644 --- a/lib/hamlit/parser/compiler.rb +++ b/lib/hamlit/parser/haml_compiler.rb @@ -1,6 +1,9 @@ -module Haml - class Compiler - include Haml::Util +require 'hamlit/parser/haml_util' +require 'hamlit/parser/haml_parser' + +module Hamlit + class HamlCompiler + include ::Hamlit::HamlUtil attr_accessor :options @@ -47,8 +50,8 @@ module Haml def precompiled_with_ambles(local_names) preamble = <" end @@ -543,7 +546,7 @@ END last[1].gsub!(/\(haml_temp, (.*?)\);$/, '(haml_temp.rstrip, \1);') rstrip_buffer! index - 1 else - raise SyntaxError.new("[HAML BUG] Undefined entry in Haml::Compiler@to_merge.") + raise ::Hamlit::HamlSyntaxError.new("[HAML BUG] Undefined entry in ::Hamlit::HamlCompiler@to_merge.") end end end diff --git a/lib/hamlit/parser/error.rb b/lib/hamlit/parser/haml_error.rb similarity index 97% rename from lib/hamlit/parser/error.rb rename to lib/hamlit/parser/haml_error.rb index 6a8b33a4..73f507e2 100644 --- a/lib/hamlit/parser/error.rb +++ b/lib/hamlit/parser/haml_error.rb @@ -1,6 +1,6 @@ -module Haml +module Hamlit # An exception raised by Haml code. - class Error < StandardError + class HamlError < StandardError MESSAGES = { :bad_script_indent => '"%s" is indented at wrong level: expected %d, but was at %d.', @@ -57,5 +57,5 @@ END # ill-formatted document. # It's not particularly interesting, # except in that it's a subclass of {Haml::Error}. - class SyntaxError < Error; end + class HamlSyntaxError < HamlError; end end diff --git a/lib/hamlit/parser/helpers.rb b/lib/hamlit/parser/haml_helpers.rb similarity index 95% rename from lib/hamlit/parser/helpers.rb rename to lib/hamlit/parser/haml_helpers.rb index 72773b8f..f1a57382 100644 --- a/lib/hamlit/parser/helpers.rb +++ b/lib/hamlit/parser/haml_helpers.rb @@ -1,9 +1,15 @@ -module Haml +require 'hamlit/parser/haml_error' +require 'hamlit/parser/haml_buffer' +require 'hamlit/parser/haml_options' +require 'hamlit/parser/haml_compiler' +require 'hamlit/parser/haml_parser' + +module Hamlit # This module contains various helpful methods to make it easier to do various tasks. # {Haml::Helpers} is automatically included in the context # that a Haml template is parsed in, so all these methods are at your # disposal from within the template. - module Helpers + module HamlHelpers # An object that raises an error when \{#to\_s} is called. # It's used to raise an error when the return value of a helper is used # when it shouldn't be. @@ -20,8 +26,8 @@ MESSAGE # # @raise [Haml::Error] The error def to_s - raise Haml::Error.new(@message) - rescue Haml::Error => e + raise ::Hamlit::HamlError.new(@message) + rescue ::Hamlit::HamlError => e e.backtrace.shift # If the ErrorReturn is used directly in the template, @@ -40,7 +46,7 @@ MESSAGE # @return [String] A human-readable string representation def inspect - "Haml::Helpers::ErrorReturn(#{@message.inspect})" + "::Hamlit::HamlHelpers::ErrorReturn(#{@message.inspect})" end end @@ -70,7 +76,7 @@ MESSAGE # context.haml_tag :p, "Stuff" # def init_haml_helpers - @haml_buffer = Haml::Buffer.new(haml_buffer, Options.new.for_buffer) + @haml_buffer = ::Hamlit::HamlBuffer.new(haml_buffer, ::Hamlit::HamlOptions.new.for_buffer) nil end @@ -496,7 +502,7 @@ MESSAGE attrs.keys.each {|key| attrs[key.to_s] = attrs.delete(key)} unless attrs.empty? name, attrs = merge_name_and_attributes(name.to_s, attrs) - attributes = Haml::Compiler.build_attributes(haml_buffer.html?, + attributes = ::Hamlit::HamlCompiler.build_attributes(haml_buffer.html?, haml_buffer.options[:attr_wrapper], haml_buffer.options[:escape_attrs], haml_buffer.options[:hyphenate_data_attrs], @@ -508,8 +514,8 @@ MESSAGE end if flags.include?(:/) - raise Error.new(Error.message(:self_closing_content)) if text - raise Error.new(Error.message(:illegal_nesting_self_closing)) if block + raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:self_closing_content)) if text + raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:illegal_nesting_self_closing)) if block end tag = "<#{name}#{attributes}>" @@ -531,7 +537,7 @@ MESSAGE end if text - raise Error.new(Error.message(:illegal_nesting_line, name)) + raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:illegal_nesting_line, name)) end if flags.include?(:<) @@ -651,8 +657,8 @@ MESSAGE # skip merging if no ids or classes found in name return name, attributes_hash unless name =~ /^(.+?)?([\.#].*)$/ - return $1 || "div", Buffer.merge_attrs( - Haml::Parser.parse_class_and_id($2), attributes_hash) + return $1 || "div", ::Hamlit::HamlBuffer.merge_attrs( + ::Hamlit::HamlParser.parse_class_and_id($2), attributes_hash) end # Runs a block of code with the given buffer as the currently active buffer. diff --git a/lib/hamlit/parser/options.rb b/lib/hamlit/parser/haml_options.rb similarity index 96% rename from lib/hamlit/parser/options.rb rename to lib/hamlit/parser/haml_options.rb index c444e5a5..dbf447d4 100644 --- a/lib/hamlit/parser/options.rb +++ b/lib/hamlit/parser/haml_options.rb @@ -1,8 +1,12 @@ -module Haml +require 'hamlit/parser/haml_parser' +require 'hamlit/parser/haml_compiler' +require 'hamlit/parser/haml_error' + +module Hamlit # This class encapsulates all of the configuration options that Haml # understands. Please see the {file:REFERENCE.md#options Haml Reference} to # learn how to set the options. - class Options + class HamlOptions @defaults = { :attr_wrapper => "'", @@ -22,8 +26,8 @@ module Haml :suppress_eval => false, :ugly => false, :cdata => false, - :parser_class => ::Haml::Parser, - :compiler_class => ::Haml::Compiler, + :parser_class => ::Hamlit::HamlParser, + :compiler_class => ::Hamlit::HamlCompiler, :trace => false } @@ -238,7 +242,7 @@ module Haml def format=(value) unless self.class.valid_formats.include?(value) - raise Haml::Error, "Invalid output format #{value.inspect}" + raise ::Hamlit::HamlError, "Invalid output format #{value.inspect}" end @format = value end diff --git a/lib/hamlit/parser/parser.rb b/lib/hamlit/parser/haml_parser.rb similarity index 87% rename from lib/hamlit/parser/parser.rb rename to lib/hamlit/parser/haml_parser.rb index c6dfdcc9..c8cf0755 100644 --- a/lib/hamlit/parser/parser.rb +++ b/lib/hamlit/parser/haml_parser.rb @@ -1,8 +1,11 @@ require 'strscan' +require 'hamlit/parser/haml_util' +require 'hamlit/parser/haml_buffer' +require 'hamlit/parser/haml_error' -module Haml - class Parser - include Haml::Util +module Hamlit + class HamlParser + include ::Hamlit::HamlUtil attr_reader :root @@ -113,7 +116,7 @@ module Haml @indentation = nil @line = next_line - raise SyntaxError.new(Error.message(:indenting_at_start), @line.index) if @line.tabs != 0 + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:indenting_at_start), @line.index) if @line.tabs != 0 loop do next_line @@ -136,7 +139,7 @@ module Haml end if !flat? && @next_line.tabs - @line.tabs > 1 - raise SyntaxError.new(Error.message(:deeper_indenting, @next_line.tabs - @line.tabs), @next_line.index) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:deeper_indenting, @next_line.tabs - @line.tabs), @next_line.index) end @line = @next_line @@ -144,7 +147,7 @@ module Haml # Close all the open tags close until @parent.type == :root @root - rescue Haml::Error => e + rescue ::Hamlit::HamlError => e e.backtrace.unshift "#{@options.filename}:#{(e.line ? e.line + 1 : @line.index + 1) + @options.line - 1}" raise end @@ -156,7 +159,7 @@ module Haml @indentation = line.whitespace if @indentation.include?(?\s) && @indentation.include?(?\t) - raise SyntaxError.new(Error.message(:cant_use_tabs_and_spaces), line.index) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:cant_use_tabs_and_spaces), line.index) end @flat_spaces = @indentation * (@template_tabs+1) if flat? @@ -167,11 +170,11 @@ module Haml return tabs if line.whitespace == @indentation * tabs return @template_tabs + 1 if flat? && line.whitespace =~ /^#{@flat_spaces}/ - message = Error.message(:inconsistent_indentation, + message = ::Hamlit::HamlError.message(:inconsistent_indentation, human_indentation(line.whitespace), human_indentation(@indentation) ) - raise SyntaxError.new(message, line.index) + raise ::Hamlit::HamlSyntaxError.new(message, line.index) end private @@ -273,7 +276,7 @@ module Haml def plain(line, escape_html = nil) if block_opened? - raise SyntaxError.new(Error.message(:illegal_nesting_plain), @next_line.index) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_nesting_plain), @next_line.index) end unless contains_interpolation?(line.text) @@ -286,7 +289,7 @@ module Haml end def script(line, escape_html = nil, preserve = false) - raise SyntaxError.new(Error.message(:no_ruby_code, '=')) if line.text.empty? + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:no_ruby_code, '=')) if line.text.empty? line = handle_ruby_multiline(line) escape_html = @options.escape_html if escape_html.nil? @@ -298,12 +301,12 @@ module Haml end def flat_script(line, escape_html = nil) - raise SyntaxError.new(Error.message(:no_ruby_code, '~')) if line.text.empty? + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:no_ruby_code, '~')) if line.text.empty? script(line, escape_html, :preserve) end def silent_script(line) - raise SyntaxError.new(Error.message(:no_end), line.index) if line.text[1..-1].strip == 'end' + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:no_end), line.index) if line.text[1..-1].strip == 'end' line = handle_ruby_multiline(line) keyword = block_keyword(line.text) @@ -312,7 +315,7 @@ module Haml if ["else", "elsif", "when"].include?(keyword) if @script_level_stack.empty? - raise Haml::SyntaxError.new(Error.message(:missing_if, keyword), @line.index) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:missing_if, keyword), @line.index) end if keyword == 'when' and !@script_level_stack.last[2] @@ -323,8 +326,8 @@ module Haml end if @script_level_stack.last[1] != @line.tabs - message = Error.message(:bad_script_indent, keyword, @script_level_stack.last[1], @line.tabs) - raise Haml::SyntaxError.new(message, @line.index) + message = ::Hamlit::HamlError.message(:bad_script_indent, keyword, @script_level_stack.last[1], @line.tabs) + raise ::Hamlit::HamlSyntaxError.new(message, @line.index) end end @@ -396,29 +399,29 @@ module Haml end end - attributes = Parser.parse_class_and_id(attributes) + attributes = ::Hamlit::HamlParser.parse_class_and_id(attributes) attributes_list = [] if attributes_hashes[:new] static_attributes, attributes_hash = attributes_hashes[:new] - Buffer.merge_attrs(attributes, static_attributes) if static_attributes + ::Hamlit::HamlBuffer.merge_attrs(attributes, static_attributes) if static_attributes attributes_list << attributes_hash end if attributes_hashes[:old] static_attributes = parse_static_hash(attributes_hashes[:old]) - Buffer.merge_attrs(attributes, static_attributes) if static_attributes + ::Hamlit::HamlBuffer.merge_attrs(attributes, static_attributes) if static_attributes attributes_list << attributes_hashes[:old] unless static_attributes || @options.suppress_eval end attributes_list.compact! - raise SyntaxError.new(Error.message(:illegal_nesting_self_closing), @next_line.index) if block_opened? && self_closing - raise SyntaxError.new(Error.message(:no_ruby_code, action), last_line - 1) if parse && value.empty? - raise SyntaxError.new(Error.message(:self_closing_content), last_line - 1) if self_closing && !value.empty? + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_nesting_self_closing), @next_line.index) if block_opened? && self_closing + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:no_ruby_code, action), last_line - 1) if parse && value.empty? + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:self_closing_content), last_line - 1) if self_closing && !value.empty? if block_opened? && !value.empty? && !is_ruby_multiline?(value) - raise SyntaxError.new(Error.message(:illegal_nesting_line, tag_name), @next_line.index) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_nesting_line, tag_name), @next_line.index) end self_closing ||= !!(!block_opened? && value.empty? && @options.autoclose.any? {|t| t === tag_name}) @@ -461,7 +464,7 @@ module Haml end if block_opened? && !text.empty? - raise SyntaxError.new(Haml::Error.message(:illegal_nesting_content), @next_line.index) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_nesting_content), @next_line.index) end ParseNode.new(:comment, @line.index + 1, :conditional => conditional, :text => text, :revealed => revealed, :parse => parse) @@ -469,13 +472,13 @@ module Haml # Renders an XHTML doctype or XML shebang. def doctype(text) - raise SyntaxError.new(Error.message(:illegal_nesting_header), @next_line.index) if block_opened? + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_nesting_header), @next_line.index) if block_opened? version, type, encoding = text[3..-1].strip.downcase.scan(DOCTYPE_REGEX)[0] ParseNode.new(:doctype, @line.index + 1, :version => version, :type => type, :encoding => encoding) end def filter(name) - raise Error.new(Error.message(:invalid_filter_name, name)) unless name =~ /^\w+$/ + raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:invalid_filter_name, name)) unless name =~ /^\w+$/ if filter_opened? @flat = true @@ -567,12 +570,12 @@ module Haml # Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value def parse_tag(text) match = text.scan(/%([-:\w]+)([-:\w.#]*)(.+)?/)[0] - raise SyntaxError.new(Error.message(:invalid_tag, text)) unless match + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:invalid_tag, text)) unless match tag_name, attributes, rest = match if !attributes.empty? && (attributes =~ /[.#](\.|#|\z)/) - raise SyntaxError.new(Error.message(:illegal_element)) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_element)) end new_attributes_hash = old_attributes_hash = last_line = nil @@ -623,8 +626,8 @@ module Haml begin attributes_hash, rest = balance(text, ?{, ?}) - rescue SyntaxError => e - if text.strip[-1] == ?, && e.message == Error.message(:unbalanced_brackets) + rescue ::Hamlit::HamlSyntaxError => e + if text.strip[-1] == ?, && e.message == ::Hamlit::HamlError.message(:unbalanced_brackets) text << "\n#{@next_line.text}" last_line += 1 next_line @@ -649,9 +652,9 @@ module Haml break if name.nil? if name == false - scanned = Haml::Util.balance(text, ?(, ?)) + scanned = ::Hamlit::HamlUtil.balance(text, ?(, ?)) text = scanned ? scanned.first : text - raise Haml::SyntaxError.new(Error.message(:invalid_attribute_list, text.inspect), last_line - 1) + raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:invalid_attribute_list, text.inspect), last_line - 1) end attributes[name] = value scanner.scan(/\s*/) @@ -773,7 +776,7 @@ module Haml end def balance(*args) - Haml::Util.balance(*args) or raise(SyntaxError.new(Error.message(:unbalanced_brackets))) + ::Hamlit::HamlUtil.balance(*args) or raise(::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:unbalanced_brackets))) end def block_opened? diff --git a/lib/hamlit/parser/util.rb b/lib/hamlit/parser/haml_util.rb similarity index 97% rename from lib/hamlit/parser/util.rb rename to lib/hamlit/parser/haml_util.rb index a1b2d100..70ad8d3e 100644 --- a/lib/hamlit/parser/util.rb +++ b/lib/hamlit/parser/haml_util.rb @@ -9,9 +9,9 @@ require 'set' require 'stringio' require 'strscan' -module Haml +module Hamlit # A module containing various useful functions. - module Util + module HamlUtil extend self # Silence all output to STDERR within a block. @@ -200,7 +200,7 @@ MSG def unescape_interpolation(str, escape_html = nil) res = '' - rest = Haml::Util.handle_interpolation str.dump do |scan| + rest = ::Hamlit::HamlUtil.handle_interpolation str.dump do |scan| escapes = (scan[2].size - 1) / 2 char = scan[3] # '{', '@' or '$' res << scan.matched[0...-3 - escapes] @@ -214,7 +214,7 @@ MSG end content = eval('"' + interpolated + '"') content.prepend(char) if char == '@' || char == '$' - content = "Haml::Helpers.html_escape((#{content}))" if escape_html + content = "::Hamlit::HamlHelpers.html_escape((#{content}))" if escape_html res << "\#{#{content}}" end diff --git a/sample/rails/Gemfile.lock b/sample/rails/Gemfile.lock index e23458a0..ef6d2965 100644 --- a/sample/rails/Gemfile.lock +++ b/sample/rails/Gemfile.lock @@ -3,7 +3,6 @@ PATH specs: hamlit (0.1.0) escape_utils - haml (>= 4.0.7, < 5.0) temple (~> 0.7.6) tilt @@ -63,8 +62,6 @@ GEM execjs (2.6.0) globalid (0.3.6) activesupport (>= 4.1.0) - haml (4.0.7) - tilt i18n (0.7.0) jbuilder (2.3.2) activesupport (>= 3.0.0, < 5) diff --git a/sample/sinatra/Gemfile.lock b/sample/sinatra/Gemfile.lock new file mode 100644 index 00000000..c64bc475 --- /dev/null +++ b/sample/sinatra/Gemfile.lock @@ -0,0 +1,20 @@ +GEM + remote: https://rubygems.org/ + specs: + rack (1.6.4) + rack-protection (1.5.3) + rack + sinatra (1.4.6) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) + tilt (2.0.1) + +PLATFORMS + ruby + +DEPENDENCIES + sinatra + +BUNDLED WITH + 1.10.6 diff --git a/test/haml/haml-spec/Rakefile b/test/haml/haml-spec/Rakefile index 7f5286b6..601bc737 100644 --- a/test/haml/haml-spec/Rakefile +++ b/test/haml/haml-spec/Rakefile @@ -12,6 +12,7 @@ def generate_spec(mode) spec = <<-SPEC.unindent require "minitest/autorun" require "hamlit" + require "haml" # This is a spec converted by haml-spec. # See: https://github.com/haml/haml-spec diff --git a/test/haml/haml-spec/ugly_test.rb b/test/haml/haml-spec/ugly_test.rb index c7fc22aa..a55ab572 100644 --- a/test/haml/haml-spec/ugly_test.rb +++ b/test/haml/haml-spec/ugly_test.rb @@ -1,6 +1,7 @@ $:.unshift File.expand_path('../../test', __dir__) require 'test_helper' +require 'haml' require 'minitest/autorun' # This is a spec converted by haml-spec. diff --git a/test/test_helper.rb b/test/test_helper.rb index 018c2fe1..2c41f462 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,6 +7,7 @@ require 'action_view' require 'rails' require 'hamlit' +require 'haml' # require 'minitest/reporters' # Minitest::Reporters.use!