lib/faml/compiler.rb in faml-0.2.6 vs lib/faml/compiler.rb in faml-0.2.7
- old
+ new
@@ -1,17 +1,18 @@
require 'parser/current'
require 'temple'
require 'faml/ast'
+require 'faml/error'
require 'faml/filter_compilers'
require 'faml/helpers'
require 'faml/rails_helpers'
require 'faml/static_hash_parser'
require 'faml/text_compiler'
module Faml
class Compiler < Temple::Parser
- class UnparsableRubyCode < StandardError
+ class UnparsableRubyCode < Error
end
DEFAULT_AUTO_CLOSE_TAGS = %w[
area base basefont br col command embed frame hr img input isindex keygen
link menuitem meta param source track wbr
@@ -21,19 +22,26 @@
define_options(
autoclose: DEFAULT_AUTO_CLOSE_TAGS,
format: :html,
preserve: DEFAULT_PRESERVE_TAGS,
use_html_safe: false,
+ filename: nil,
)
def initialize(*)
super
@text_compiler = TextCompiler.new
+ @filename = options[:filename]
end
def call(ast)
compile(ast)
+ rescue Error => e
+ if @filename && e.lineno
+ e.backtrace.unshift "#{@filename}:#{e.lineno}"
+ end
+ raise e
end
def self.find_and_preserve(input)
# Taken from the original haml code
re = /<(#{options[:preserve].map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im
@@ -120,11 +128,11 @@
(ast.is_a?(Ast::Element) && !ast.children.empty?) ||
(ast.is_a?(Ast::HtmlComment) && !ast.conditional.empty?)
end
def compile_text(ast)
- @text_compiler.compile(ast.text, escape_html: ast.escape_html)
+ @text_compiler.compile(ast.text, ast.lineno, escape_html: ast.escape_html)
end
# html5 and html4 is deprecated in temple.
DEFAULT_DOCTYPE = {
html: 'html',
@@ -184,10 +192,15 @@
if ast.nuke_outer_whitespace
[:multi, [:rmnl], temple]
else
temple
end
+ rescue UnparsableRubyCode => e
+ unless e.lineno
+ e.lineno = ast.lineno
+ end
+ raise e
end
def self_closing?(ast)
ast.self_closing || options[:autoclose].include?(ast.tag_name)
end
@@ -280,11 +293,11 @@
buffer = ::Parser::Source::Buffer.new('(faml)')
buffer.source = "call(#{text})"
parser.parse(buffer)
true
rescue ::Parser::SyntaxError
- raise UnparsableRubyCode.new("Unparsable Ruby code is given to attributes: #{text}")
+ raise UnparsableRubyCode.new("Unparsable Ruby code is given to attributes: #{text}", nil)
end
def build_optimized_attributes(parser, static_id, static_class)
static_attributes = build_optimized_static_attributes(parser, static_id, static_class)
dynamic_attributes = build_optimized_dynamic_attributes(parser, static_attributes)
@@ -372,9 +385,14 @@
end
temple
end
def compile_filter(ast)
- FilterCompilers.find(ast.name).compile(ast.texts)
+ FilterCompilers.find(ast.name).compile(ast)
+ rescue FilterCompilers::NotFound => e
+ unless e.lineno
+ e.lineno = ast.lineno
+ end
+ raise e
end
end
end