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.require_paths = ['lib']
spec.add_dependency 'escape_utils' spec.add_dependency 'escape_utils'
spec.add_dependency 'haml', '>= 4.0.7', '< 5.0'
spec.add_dependency 'temple', '~> 0.7.6' spec.add_dependency 'temple', '~> 0.7.6'
spec.add_dependency 'tilt' spec.add_dependency 'tilt'
spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'coffee-script' spec.add_development_dependency 'coffee-script'
spec.add_development_dependency 'faml', '>= 0.3.3' spec.add_development_dependency 'faml', '>= 0.3.3'
spec.add_development_dependency 'haml'
spec.add_development_dependency 'less' spec.add_development_dependency 'less'
spec.add_development_dependency 'minitest-reporters', '~> 1.1' spec.add_development_dependency 'minitest-reporters', '~> 1.1'
spec.add_development_dependency 'rails', '>= 4.0.0' spec.add_development_dependency 'rails', '>= 4.0.0'

View File

@ -22,7 +22,7 @@ module Hamlit
if node.value[:escape_html] if node.value[:escape_html]
str = Temple::Utils.escape_html(str) str = Temple::Utils.escape_html(str)
elsif node.value[:preserve] 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 end
[:static, str] [:static, str]
end end
@ -60,7 +60,7 @@ module Hamlit
end end
def find_and_preserve(code) 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 end
def escape_html(temple) def escape_html(temple)

View File

@ -1,4 +1,4 @@
require 'haml/util' require 'hamlit/parser/haml_util'
require 'hamlit/compiler/attribute_compiler' require 'hamlit/compiler/attribute_compiler'
module Hamlit module Hamlit
@ -25,7 +25,7 @@ module Hamlit
nil nil
when node.value[:parse] when node.value[:parse]
[:escape, node.value[:escape_html], [:dynamic, node.value[:value]]] [: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]] [:dynamic, node.value[:value]]
else else
[:static, node.value[:value]] [:static, node.value[:value]]

View File

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

View File

@ -14,8 +14,8 @@ module Hamlit
def compile_text!(temple, node, prefix) def compile_text!(temple, node, prefix)
(node.value[:text].rstrip << "\n").each_line do |line| (node.value[:text].rstrip << "\n").each_line do |line|
if Haml::Util.contains_interpolation?(line) if ::Hamlit::HamlUtil.contains_interpolation?(line)
temple << [:dynamic, Haml::Util.unescape_interpolation(prefix.dup << line)] temple << [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(prefix.dup << line)]
else else
temple << [:static, prefix.dup << line] temple << [:static, prefix.dup << line]
end end

View File

@ -1,5 +1,3 @@
require 'haml/util'
module Hamlit module Hamlit
class Filters class Filters
class Escaped < Base class Escaped < Base
@ -12,8 +10,8 @@ module Hamlit
private private
def compile_text(text) def compile_text(text)
if Haml::Util.contains_interpolation?(text) if ::Hamlit::HamlUtil.contains_interpolation?(text)
[:dynamic, Haml::Util.unescape_interpolation(text)] [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(text)]
else else
[:static, text] [:static, text]
end end

View File

@ -14,8 +14,8 @@ module Hamlit
def compile_text!(temple, node, prefix) def compile_text!(temple, node, prefix)
(node.value[:text].rstrip << "\n").each_line do |line| (node.value[:text].rstrip << "\n").each_line do |line|
if Haml::Util.contains_interpolation?(line) if ::Hamlit::HamlUtil.contains_interpolation?(line)
temple << [:dynamic, Haml::Util.unescape_interpolation(prefix.dup << line)] temple << [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(prefix.dup << line)]
else else
temple << [:static, prefix.dup << line] temple << [:static, prefix.dup << line]
end end

View File

@ -1,14 +1,12 @@
require 'haml/util'
module Hamlit module Hamlit
class Filters class Filters
class Plain < Base class Plain < Base
def compile(node) def compile(node)
text = node.value[:text].rstrip 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 # FIXME: Confirm whether this is correct or not
text << "\n".freeze text << "\n".freeze
text = Haml::Util.unescape_interpolation(text) text = ::Hamlit::HamlUtil.unescape_interpolation(text)
[:escape, true, [:dynamic, text]] [:escape, true, [:dynamic, text]]
else else
[:static, text] [:static, text]

View File

@ -1,5 +1,3 @@
require 'haml/util'
module Hamlit module Hamlit
class Filters class Filters
class Preserve < Base class Preserve < Base
@ -12,8 +10,8 @@ module Hamlit
private private
def compile_text(text) def compile_text(text)
if Haml::Util.contains_interpolation?(text) if ::Hamlit::HamlUtil.contains_interpolation?(text)
[:dynamic, Haml::Util.unescape_interpolation(text)] [:dynamic, ::Hamlit::HamlUtil.unescape_interpolation(text)]
else else
[:static, text] [:static, text]
end end

View File

@ -12,7 +12,7 @@ module Hamlit
private private
def compile_with_tilt(node, name, indent_width: 0) 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) dynamic_compile(node, name, indent_width: indent_width)
else else
static_compile(node, name, indent_width: indent_width) static_compile(node, name, indent_width: indent_width)
@ -24,7 +24,7 @@ module Hamlit
end end
def dynamic_compile(node, name, indent_width: 0) 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})"] [:dynamic, "::Hamlit::Filters::TiltBase.render('#{name}', #{source}, indent_width: #{indent_width})"]
end end
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 module Hamlit
class Parser class Parser
@ -9,14 +15,14 @@ module Hamlit
].freeze ].freeze
def initialize(options = {}) def initialize(options = {})
@options = Haml::Options.defaults.dup @options = HamlOptions.defaults.dup
AVAILABLE_OPTIONS.each do |key| AVAILABLE_OPTIONS.each do |key|
@options[key] = options[key] @options[key] = options[key]
end end
end end
def call(template) def call(template)
Haml::Parser.new(template, Haml::Options.new(@options)).parse HamlParser.new(template, HamlOptions.new(@options)).parse
end end
end 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 # This class is used only internally. It holds the buffer of HTML that
# is eventually output as the resulting document. # is eventually output as the resulting document.
# It's called from within the precompiled code, # It's called from within the precompiled code,
# and helps reduce the amount of processing done within `instance_eval`ed code. # and helps reduce the amount of processing done within `instance_eval`ed code.
class Buffer class HamlBuffer
ID_KEY = 'id'.freeze ID_KEY = 'id'.freeze
CLASS_KEY = 'class'.freeze CLASS_KEY = 'class'.freeze
DATA_KEY = 'data'.freeze DATA_KEY = 'data'.freeze
include Haml::Helpers include ::Hamlit::HamlHelpers
include Haml::Util include ::Hamlit::HamlUtil
# The string that holds the compiled HTML. This is aliased as # The string that holds the compiled HTML. This is aliased as
# `_erbout` for compatibility with ERB-specific code. # `_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]}]) self.class.merge_attrs(attributes, Hash[old.map {|k, v| [k.to_s, v]}])
end end
self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref 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) html?, @options[:attr_wrapper], @options[:escape_attrs], @options[:hyphenate_data_attrs], attributes)
end end
@ -223,14 +227,14 @@ module Haml
# @param from [{String => #to_s}] The attribute hash to merge from # @param from [{String => #to_s}] The attribute hash to merge from
# @return [{String => String}] `to`, after being merged # @return [{String => String}] `to`, after being merged
def self.merge_attrs(to, from) 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] if to[ID_KEY] && from[ID_KEY]
to[ID_KEY] << "_#{from.delete(ID_KEY)}" to[ID_KEY] << "_#{from.delete(ID_KEY)}"
elsif to[ID_KEY] || from[ID_KEY] elsif to[ID_KEY] || from[ID_KEY]
from[ID_KEY] ||= to[ID_KEY] from[ID_KEY] ||= to[ID_KEY]
end 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] if to[CLASS_KEY] && from[CLASS_KEY]
# Make sure we don't duplicate class names # Make sure we don't duplicate class names
from[CLASS_KEY] = (from[CLASS_KEY].to_s.split(' ') | to[CLASS_KEY].split(' ')).sort.join(' ') from[CLASS_KEY] = (from[CLASS_KEY].to_s.split(' ') | to[CLASS_KEY].split(' ')).sort.join(' ')
@ -259,8 +263,8 @@ module Haml
private private
def preserve(result, preserve_script, preserve_tag) def preserve(result, preserve_script, preserve_tag)
return Haml::Helpers.preserve(result) if preserve_tag return ::Hamlit::HamlHelpers.preserve(result) if preserve_tag
return Haml::Helpers.find_and_preserve(result, options[:preserve]) if preserve_script return ::Hamlit::HamlHelpers.find_and_preserve(result, options[:preserve]) if preserve_script
result result
end end

View File

@ -1,6 +1,9 @@
module Haml require 'hamlit/parser/haml_util'
class Compiler require 'hamlit/parser/haml_parser'
include Haml::Util
module Hamlit
class HamlCompiler
include ::Hamlit::HamlUtil
attr_accessor :options attr_accessor :options
@ -47,8 +50,8 @@ module Haml
def precompiled_with_ambles(local_names) def precompiled_with_ambles(local_names)
preamble = <<END.tr!("\n", ';') preamble = <<END.tr!("\n", ';')
begin begin
extend Haml::Helpers extend ::Hamlit::HamlHelpers
_hamlout = @haml_buffer = Haml::Buffer.new(haml_buffer, #{options.for_buffer.inspect}) _hamlout = @haml_buffer = ::Hamlit::HamlBuffer.new(haml_buffer, #{options.for_buffer.inspect})
_erbout = _hamlout.buffer _erbout = _hamlout.buffer
@output_buffer = output_buffer ||= ActionView::OutputBuffer.new rescue nil @output_buffer = output_buffer ||= ActionView::OutputBuffer.new rescue nil
END END
@ -128,7 +131,7 @@ END
@node.parent.value[:dont_push_end] = true @node.parent.value[:dont_push_end] = true
end end
# Don't restore dont_* for end because it isn't a conditional branch. # 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 # Restore dont_* for this conditional branch
@dont_indent_next_line = @node.parent.value[:dont_indent_next_line] @dont_indent_next_line = @node.parent.value[:dont_indent_next_line]
@dont_tab_up_next_text = @node.parent.value[:dont_tab_up_next_text] @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]] unless filter = Filters.defined[@node.value[:name]]
name = @node.value[:name] name = @node.value[:name]
if ["maruku", "textile"].include?(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 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
end end
filter.internal_compile(self, @node.value[:text]) filter.internal_compile(self, @node.value[:text])
@ -355,7 +358,7 @@ END
mtabs = 0 mtabs = 0
"\#{#{val}}" "\#{#{val}}"
else 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 end
str = @to_merge.join str = @to_merge.join
@ -443,13 +446,13 @@ END
escaped = escaped =
if escape_attrs == :once if escape_attrs == :once
Haml::Helpers.escape_once(value.to_s) ::Hamlit::HamlHelpers.escape_once(value.to_s)
elsif escape_attrs elsif escape_attrs
Haml::Helpers.html_escape(value.to_s) ::Hamlit::HamlHelpers.html_escape(value.to_s)
else else
value.to_s value.to_s
end end
value = Haml::Helpers.preserve(escaped) value = ::Hamlit::HamlHelpers.preserve(escaped)
if escape_attrs if escape_attrs
# We want to decide whether or not to escape quotes # We want to decide whether or not to escape quotes
value.gsub!(/&quot;|&#x0022;/, '"') value.gsub!(/&quot;|&#x0022;/, '"')
@ -510,7 +513,7 @@ END
end end
def prerender_tag(name, self_close, attributes) 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) @options.html?, @options.attr_wrapper, @options.escape_attrs, @options.hyphenate_data_attrs, attributes)
"<#{name}#{attributes_string}#{self_close && @options.xhtml? ? ' /' : ''}>" "<#{name}#{attributes_string}#{self_close && @options.xhtml? ? ' /' : ''}>"
end end
@ -543,7 +546,7 @@ END
last[1].gsub!(/\(haml_temp, (.*?)\);$/, '(haml_temp.rstrip, \1);') last[1].gsub!(/\(haml_temp, (.*?)\);$/, '(haml_temp.rstrip, \1);')
rstrip_buffer! index - 1 rstrip_buffer! index - 1
else 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 end
end end

View File

@ -1,6 +1,6 @@
module Haml module Hamlit
# An exception raised by Haml code. # An exception raised by Haml code.
class Error < StandardError class HamlError < StandardError
MESSAGES = { MESSAGES = {
:bad_script_indent => '"%s" is indented at wrong level: expected %d, but was at %d.', :bad_script_indent => '"%s" is indented at wrong level: expected %d, but was at %d.',
@ -57,5 +57,5 @@ END
# ill-formatted document. # ill-formatted document.
# It's not particularly interesting, # It's not particularly interesting,
# except in that it's a subclass of {Haml::Error}. # except in that it's a subclass of {Haml::Error}.
class SyntaxError < Error; end class HamlSyntaxError < HamlError; end
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. # This module contains various helpful methods to make it easier to do various tasks.
# {Haml::Helpers} is automatically included in the context # {Haml::Helpers} is automatically included in the context
# that a Haml template is parsed in, so all these methods are at your # that a Haml template is parsed in, so all these methods are at your
# disposal from within the template. # disposal from within the template.
module Helpers module HamlHelpers
# An object that raises an error when \{#to\_s} is called. # 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 # It's used to raise an error when the return value of a helper is used
# when it shouldn't be. # when it shouldn't be.
@ -20,8 +26,8 @@ MESSAGE
# #
# @raise [Haml::Error] The error # @raise [Haml::Error] The error
def to_s def to_s
raise Haml::Error.new(@message) raise ::Hamlit::HamlError.new(@message)
rescue Haml::Error => e rescue ::Hamlit::HamlError => e
e.backtrace.shift e.backtrace.shift
# If the ErrorReturn is used directly in the template, # If the ErrorReturn is used directly in the template,
@ -40,7 +46,7 @@ MESSAGE
# @return [String] A human-readable string representation # @return [String] A human-readable string representation
def inspect def inspect
"Haml::Helpers::ErrorReturn(#{@message.inspect})" "::Hamlit::HamlHelpers::ErrorReturn(#{@message.inspect})"
end end
end end
@ -70,7 +76,7 @@ MESSAGE
# context.haml_tag :p, "Stuff" # context.haml_tag :p, "Stuff"
# #
def init_haml_helpers 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 nil
end end
@ -496,7 +502,7 @@ MESSAGE
attrs.keys.each {|key| attrs[key.to_s] = attrs.delete(key)} unless attrs.empty? attrs.keys.each {|key| attrs[key.to_s] = attrs.delete(key)} unless attrs.empty?
name, attrs = merge_name_and_attributes(name.to_s, attrs) 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[:attr_wrapper],
haml_buffer.options[:escape_attrs], haml_buffer.options[:escape_attrs],
haml_buffer.options[:hyphenate_data_attrs], haml_buffer.options[:hyphenate_data_attrs],
@ -508,8 +514,8 @@ MESSAGE
end end
if flags.include?(:/) if flags.include?(:/)
raise Error.new(Error.message(:self_closing_content)) if text raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:self_closing_content)) if text
raise Error.new(Error.message(:illegal_nesting_self_closing)) if block raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:illegal_nesting_self_closing)) if block
end end
tag = "<#{name}#{attributes}>" tag = "<#{name}#{attributes}>"
@ -531,7 +537,7 @@ MESSAGE
end end
if text if text
raise Error.new(Error.message(:illegal_nesting_line, name)) raise ::Hamlit::HamlError.new(::Hamlit::HamlError.message(:illegal_nesting_line, name))
end end
if flags.include?(:<) if flags.include?(:<)
@ -651,8 +657,8 @@ MESSAGE
# skip merging if no ids or classes found in name # skip merging if no ids or classes found in name
return name, attributes_hash unless name =~ /^(.+?)?([\.#].*)$/ return name, attributes_hash unless name =~ /^(.+?)?([\.#].*)$/
return $1 || "div", Buffer.merge_attrs( return $1 || "div", ::Hamlit::HamlBuffer.merge_attrs(
Haml::Parser.parse_class_and_id($2), attributes_hash) ::Hamlit::HamlParser.parse_class_and_id($2), attributes_hash)
end end
# Runs a block of code with the given buffer as the currently active buffer. # 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 # This class encapsulates all of the configuration options that Haml
# understands. Please see the {file:REFERENCE.md#options Haml Reference} to # understands. Please see the {file:REFERENCE.md#options Haml Reference} to
# learn how to set the options. # learn how to set the options.
class Options class HamlOptions
@defaults = { @defaults = {
:attr_wrapper => "'", :attr_wrapper => "'",
@ -22,8 +26,8 @@ module Haml
:suppress_eval => false, :suppress_eval => false,
:ugly => false, :ugly => false,
:cdata => false, :cdata => false,
:parser_class => ::Haml::Parser, :parser_class => ::Hamlit::HamlParser,
:compiler_class => ::Haml::Compiler, :compiler_class => ::Hamlit::HamlCompiler,
:trace => false :trace => false
} }
@ -238,7 +242,7 @@ module Haml
def format=(value) def format=(value)
unless self.class.valid_formats.include?(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 end
@format = value @format = value
end end

View File

@ -1,8 +1,11 @@
require 'strscan' require 'strscan'
require 'hamlit/parser/haml_util'
require 'hamlit/parser/haml_buffer'
require 'hamlit/parser/haml_error'
module Haml module Hamlit
class Parser class HamlParser
include Haml::Util include ::Hamlit::HamlUtil
attr_reader :root attr_reader :root
@ -113,7 +116,7 @@ module Haml
@indentation = nil @indentation = nil
@line = next_line @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 loop do
next_line next_line
@ -136,7 +139,7 @@ module Haml
end end
if !flat? && @next_line.tabs - @line.tabs > 1 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 end
@line = @next_line @line = @next_line
@ -144,7 +147,7 @@ module Haml
# Close all the open tags # Close all the open tags
close until @parent.type == :root close until @parent.type == :root
@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}" e.backtrace.unshift "#{@options.filename}:#{(e.line ? e.line + 1 : @line.index + 1) + @options.line - 1}"
raise raise
end end
@ -156,7 +159,7 @@ module Haml
@indentation = line.whitespace @indentation = line.whitespace
if @indentation.include?(?\s) && @indentation.include?(?\t) 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 end
@flat_spaces = @indentation * (@template_tabs+1) if flat? @flat_spaces = @indentation * (@template_tabs+1) if flat?
@ -167,11 +170,11 @@ module Haml
return tabs if line.whitespace == @indentation * tabs return tabs if line.whitespace == @indentation * tabs
return @template_tabs + 1 if flat? && line.whitespace =~ /^#{@flat_spaces}/ 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(line.whitespace),
human_indentation(@indentation) human_indentation(@indentation)
) )
raise SyntaxError.new(message, line.index) raise ::Hamlit::HamlSyntaxError.new(message, line.index)
end end
private private
@ -273,7 +276,7 @@ module Haml
def plain(line, escape_html = nil) def plain(line, escape_html = nil)
if block_opened? 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 end
unless contains_interpolation?(line.text) unless contains_interpolation?(line.text)
@ -286,7 +289,7 @@ module Haml
end end
def script(line, escape_html = nil, preserve = false) 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) line = handle_ruby_multiline(line)
escape_html = @options.escape_html if escape_html.nil? escape_html = @options.escape_html if escape_html.nil?
@ -298,12 +301,12 @@ module Haml
end end
def flat_script(line, escape_html = nil) 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) script(line, escape_html, :preserve)
end end
def silent_script(line) 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) line = handle_ruby_multiline(line)
keyword = block_keyword(line.text) keyword = block_keyword(line.text)
@ -312,7 +315,7 @@ module Haml
if ["else", "elsif", "when"].include?(keyword) if ["else", "elsif", "when"].include?(keyword)
if @script_level_stack.empty? 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 end
if keyword == 'when' and !@script_level_stack.last[2] if keyword == 'when' and !@script_level_stack.last[2]
@ -323,8 +326,8 @@ module Haml
end end
if @script_level_stack.last[1] != @line.tabs if @script_level_stack.last[1] != @line.tabs
message = Error.message(:bad_script_indent, keyword, @script_level_stack.last[1], @line.tabs) message = ::Hamlit::HamlError.message(:bad_script_indent, keyword, @script_level_stack.last[1], @line.tabs)
raise Haml::SyntaxError.new(message, @line.index) raise ::Hamlit::HamlSyntaxError.new(message, @line.index)
end end
end end
@ -396,29 +399,29 @@ module Haml
end end
end end
attributes = Parser.parse_class_and_id(attributes) attributes = ::Hamlit::HamlParser.parse_class_and_id(attributes)
attributes_list = [] attributes_list = []
if attributes_hashes[:new] if attributes_hashes[:new]
static_attributes, attributes_hash = 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 attributes_list << attributes_hash
end end
if attributes_hashes[:old] if attributes_hashes[:old]
static_attributes = parse_static_hash(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 attributes_list << attributes_hashes[:old] unless static_attributes || @options.suppress_eval
end end
attributes_list.compact! attributes_list.compact!
raise SyntaxError.new(Error.message(:illegal_nesting_self_closing), @next_line.index) if block_opened? && self_closing raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.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 ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.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(:self_closing_content), last_line - 1) if self_closing && !value.empty?
if block_opened? && !value.empty? && !is_ruby_multiline?(value) 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 end
self_closing ||= !!(!block_opened? && value.empty? && @options.autoclose.any? {|t| t === tag_name}) self_closing ||= !!(!block_opened? && value.empty? && @options.autoclose.any? {|t| t === tag_name})
@ -461,7 +464,7 @@ module Haml
end end
if block_opened? && !text.empty? 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 end
ParseNode.new(:comment, @line.index + 1, :conditional => conditional, :text => text, :revealed => revealed, :parse => parse) 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. # Renders an XHTML doctype or XML shebang.
def doctype(text) 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] version, type, encoding = text[3..-1].strip.downcase.scan(DOCTYPE_REGEX)[0]
ParseNode.new(:doctype, @line.index + 1, :version => version, :type => type, :encoding => encoding) ParseNode.new(:doctype, @line.index + 1, :version => version, :type => type, :encoding => encoding)
end end
def filter(name) 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? if filter_opened?
@flat = true @flat = true
@ -567,12 +570,12 @@ module Haml
# Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value # Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value
def parse_tag(text) def parse_tag(text)
match = text.scan(/%([-:\w]+)([-:\w.#]*)(.+)?/)[0] 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 tag_name, attributes, rest = match
if !attributes.empty? && (attributes =~ /[.#](\.|#|\z)/) if !attributes.empty? && (attributes =~ /[.#](\.|#|\z)/)
raise SyntaxError.new(Error.message(:illegal_element)) raise ::Hamlit::HamlSyntaxError.new(::Hamlit::HamlError.message(:illegal_element))
end end
new_attributes_hash = old_attributes_hash = last_line = nil new_attributes_hash = old_attributes_hash = last_line = nil
@ -623,8 +626,8 @@ module Haml
begin begin
attributes_hash, rest = balance(text, ?{, ?}) attributes_hash, rest = balance(text, ?{, ?})
rescue SyntaxError => e rescue ::Hamlit::HamlSyntaxError => e
if text.strip[-1] == ?, && e.message == Error.message(:unbalanced_brackets) if text.strip[-1] == ?, && e.message == ::Hamlit::HamlError.message(:unbalanced_brackets)
text << "\n#{@next_line.text}" text << "\n#{@next_line.text}"
last_line += 1 last_line += 1
next_line next_line
@ -649,9 +652,9 @@ module Haml
break if name.nil? break if name.nil?
if name == false if name == false
scanned = Haml::Util.balance(text, ?(, ?)) scanned = ::Hamlit::HamlUtil.balance(text, ?(, ?))
text = scanned ? scanned.first : 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 end
attributes[name] = value attributes[name] = value
scanner.scan(/\s*/) scanner.scan(/\s*/)
@ -773,7 +776,7 @@ module Haml
end end
def balance(*args) 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 end
def block_opened? def block_opened?

View File

@ -9,9 +9,9 @@ require 'set'
require 'stringio' require 'stringio'
require 'strscan' require 'strscan'
module Haml module Hamlit
# A module containing various useful functions. # A module containing various useful functions.
module Util module HamlUtil
extend self extend self
# Silence all output to STDERR within a block. # Silence all output to STDERR within a block.
@ -200,7 +200,7 @@ MSG
def unescape_interpolation(str, escape_html = nil) def unescape_interpolation(str, escape_html = nil)
res = '' 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 escapes = (scan[2].size - 1) / 2
char = scan[3] # '{', '@' or '$' char = scan[3] # '{', '@' or '$'
res << scan.matched[0...-3 - escapes] res << scan.matched[0...-3 - escapes]
@ -214,7 +214,7 @@ MSG
end end
content = eval('"' + interpolated + '"') content = eval('"' + interpolated + '"')
content.prepend(char) if char == '@' || char == '$' 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}}" res << "\#{#{content}}"
end end

View File

@ -3,7 +3,6 @@ PATH
specs: specs:
hamlit (0.1.0) hamlit (0.1.0)
escape_utils escape_utils
haml (>= 4.0.7, < 5.0)
temple (~> 0.7.6) temple (~> 0.7.6)
tilt tilt
@ -63,8 +62,6 @@ GEM
execjs (2.6.0) execjs (2.6.0)
globalid (0.3.6) globalid (0.3.6)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
haml (4.0.7)
tilt
i18n (0.7.0) i18n (0.7.0)
jbuilder (2.3.2) jbuilder (2.3.2)
activesupport (>= 3.0.0, < 5) 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 spec = <<-SPEC.unindent
require "minitest/autorun" require "minitest/autorun"
require "hamlit" require "hamlit"
require "haml"
# This is a spec converted by haml-spec. # This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec # See: https://github.com/haml/haml-spec

View File

@ -1,6 +1,7 @@
$:.unshift File.expand_path('../../test', __dir__) $:.unshift File.expand_path('../../test', __dir__)
require 'test_helper' require 'test_helper'
require 'haml'
require 'minitest/autorun' require 'minitest/autorun'
# This is a spec converted by haml-spec. # This is a spec converted by haml-spec.

View File

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