Migrate to embedded internal Haml

This commit is contained in:
Takashi Kokubun 2015-11-21 02:55:12 +09:00
parent 04b5f4c375
commit 82a240aa69
23 changed files with 150 additions and 110 deletions

View File

@ -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'

View File

@ -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)

View File

@ -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]]

View File

@ -1,4 +1,4 @@
require 'haml/util'
require 'hamlit/parser/haml_util'
module Hamlit
class Filters

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.tr!("\n", ';')
begin
extend Haml::Helpers
_hamlout = @haml_buffer = Haml::Buffer.new(haml_buffer, #{options.for_buffer.inspect})
extend ::Hamlit::HamlHelpers
_hamlout = @haml_buffer = ::Hamlit::HamlBuffer.new(haml_buffer, #{options.for_buffer.inspect})
_erbout = _hamlout.buffer
@output_buffer = output_buffer ||= ActionView::OutputBuffer.new rescue nil
END
@ -128,7 +131,7 @@ END
@node.parent.value[:dont_push_end] = true
end
# Don't restore dont_* for end because it isn't a conditional branch.
elsif Parser::MID_BLOCK_KEYWORDS.include?(keyword)
elsif ::Hamlit::HamlParser::MID_BLOCK_KEYWORDS.include?(keyword)
# Restore dont_* for this conditional branch
@dont_indent_next_line = @node.parent.value[:dont_indent_next_line]
@dont_tab_up_next_text = @node.parent.value[:dont_tab_up_next_text]
@ -268,9 +271,9 @@ END
unless filter = Filters.defined[@node.value[:name]]
name = @node.value[:name]
if ["maruku", "textile"].include?(name)
raise Error.new(Error.message(:install_haml_contrib, name), @node.line - 1)
raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:install_haml_contrib, name), @node.line - 1)
else
raise Error.new(Error.message(:filter_not_defined, name), @node.line - 1)
raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:filter_not_defined, name), @node.line - 1)
end
end
filter.internal_compile(self, @node.value[:text])
@ -355,7 +358,7 @@ END
mtabs = 0
"\#{#{val}}"
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
str = @to_merge.join
@ -443,13 +446,13 @@ END
escaped =
if escape_attrs == :once
Haml::Helpers.escape_once(value.to_s)
::Hamlit::HamlHelpers.escape_once(value.to_s)
elsif escape_attrs
Haml::Helpers.html_escape(value.to_s)
::Hamlit::HamlHelpers.html_escape(value.to_s)
else
value.to_s
end
value = Haml::Helpers.preserve(escaped)
value = ::Hamlit::HamlHelpers.preserve(escaped)
if escape_attrs
# We want to decide whether or not to escape quotes
value.gsub!(/&quot;|&#x0022;/, '"')
@ -510,7 +513,7 @@ END
end
def prerender_tag(name, self_close, attributes)
attributes_string = Compiler.build_attributes(
attributes_string = ::Hamlit::HamlCompiler.build_attributes(
@options.html?, @options.attr_wrapper, @options.escape_attrs, @options.hyphenate_data_attrs, attributes)
"<#{name}#{attributes_string}#{self_close && @options.xhtml? ? ' /' : ''}>"
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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -7,6 +7,7 @@ require 'action_view'
require 'rails'
require 'hamlit'
require 'haml'
# require 'minitest/reporters'
# Minitest::Reporters.use!