lib/macros4cuke/templating/engine.rb in macros4cuke-0.5.14 vs lib/macros4cuke/templating/engine.rb in macros4cuke-0.5.15

- old
+ new

@@ -7,11 +7,11 @@ require_relative 'static-text' require_relative 'eo-line' require_relative 'comment' require_relative 'template-element' require_relative 'placeholder' -require_relative 'section' # Load the Section and ConditionalSection +require_relative 'section' # Load the Section and ConditionalSection module Macros4Cuke # Module used as a namespace # Module containing all classes implementing the simple template engine # used internally in Macros4Cuke. @@ -30,11 +30,11 @@ # The regular expression that matches a space, # any punctuation sign or delimiter that is forbidden # between chevrons <...> template tags. DisallowedSigns = begin # Use concatenation (+) to work around Ruby bug! - forbidden = ' !"#' + "$%&'()*+,-./:;<=>?[\\]^`{|}~" + forbidden = ' !"#' + "$%&'()*+,-./:;<=>?[\\]^`{|}~" all_escaped = [] forbidden.each_char { |ch| all_escaped << Regexp.escape(ch) } pattern = all_escaped.join('|') Regexp.new(pattern) end @@ -52,12 +52,10 @@ def initialize(aSourceTemplate) @source = aSourceTemplate @representation = compile(aSourceTemplate) end - public - # Render the template within the given scope object and # with the locals specified. # The method mimicks the signature of the Tilt::Template#render method. # @param aContextObject [anything] context object to get actual values # (when not present in the locals Hash). @@ -110,11 +108,11 @@ # [:static, text], [:comment, text] or [:dynamic, tag text] def self.parse(aTextLine) scanner = StringScanner.new(aTextLine) result = [] - if scanner.check(/\s*#/) # Detect comment line + if scanner.check(/\s*#/) # Detect comment line result << [:comment, aTextLine] else until scanner.eos? # Scan tag at current position... tag_literal = scanner.scan(/<(?:[^\\<>]|\\.)*>/) @@ -130,12 +128,10 @@ end return result end - private - # Called when the given text line could not be parsed. # Raises an exception with the syntax issue identified. # @param aTextLine [String] A text line from the template. def self.identify_parse_error(aTextLine) # Unsuccessful scanning: we typically have improperly balanced chevrons. @@ -150,18 +146,19 @@ case ch when '<' then unbalance += 1 when '>' then unbalance -= 1 end - fail(StandardError, "Nested opening chevron '<'.") if unbalance > 1 - fail(StandardError, "Missing opening chevron '<'.") if unbalance < 0 + raise(StandardError, "Nested opening chevron '<'.") if unbalance > 1 + raise(StandardError, "Missing opening chevron '<'.") if unbalance < 0 end - fail(StandardError, "Missing closing chevron '>'.") if unbalance == 1 + raise(StandardError, "Missing closing chevron '>'.") if unbalance == 1 end + + private - # Create the internal representation of the given template. def compile(aSourceTemplate) # Split the input text into lines. input_lines = aSourceTemplate.split(/\r\n?|\n/) @@ -170,11 +167,11 @@ line_items = self.class.parse(line) line_items.each do |(kind, text)| # A tag text cannot be empty nor blank next if (kind != :dynamic) || !text.strip.empty? - fail(EmptyArgumentError.new(line.strip)) + raise(EmptyArgumentError.new(line.strip)) end line_items end @@ -234,14 +231,14 @@ # Where kind must be one of :static, :dynamic def compile_couple(aCouple) (kind, text) = aCouple result = case kind - when :static then StaticText.new(text) - when :comment then Comment.new(text) - when :dynamic then parse_tag(text) - end + when :static then StaticText.new(text) + when :comment then Comment.new(text) + when :dynamic then parse_tag(text) + end return result end @@ -253,29 +250,29 @@ matching = DisallowedSigns.match(aText[1..-1]) else # Disallow punctuation and delimiter signs in tags. matching = DisallowedSigns.match(aText) end - fail(InvalidCharError.new(aText, matching[0])) if matching + raise(InvalidCharError.new(aText, matching[0])) if matching result = case aText[0, 1] - when '?' - ConditionalSection.new(aText[1..-1], true) + when '?' + ConditionalSection.new(aText[1..-1], true) - when '/' - SectionEndMarker.new(aText[1..-1]) - else - Placeholder.new(aText) - end + when '/' + SectionEndMarker.new(aText[1..-1]) + else + Placeholder.new(aText) + end return result end # Transform a flat sequence of elements into a hierarchy of sections. # @param flat_sequence [Array] a linear list of elements (including sections) def compile_sections(flat_sequence) - open_sections = [] # The list of nested open sections + open_sections = [] # The list of nested open sections compiled = flat_sequence.each_with_object([]) do |element, subResult| case element when Section open_sections << element @@ -292,12 +289,12 @@ end end end unless open_sections.empty? - error_message = "Unterminated section #{open_sections.last}." - fail(StandardError, error_message) + error_message = "Unterminated section #{open_sections.last}." + raise(StandardError, error_message) end return compiled end @@ -306,14 +303,14 @@ def validate_section_end(marker, sections) msg_prefix = "End of section</#{marker.name}> " if sections.empty? msg = 'found while no corresponding section is open.' - fail(StandardError, msg_prefix + msg) + raise(StandardError, msg_prefix + msg) end return if marker.name == sections.last.name msg = "doesn't match current section '#{sections.last.name}'." - fail(StandardError, msg_prefix + msg) + raise(StandardError, msg_prefix + msg) end end # class end # module end # module