lib/macros4cuke/templating/engine.rb in macros4cuke-0.2.18 vs lib/macros4cuke/templating/engine.rb in macros4cuke-0.2.19

- old
+ new

@@ -99,10 +99,19 @@ # The reasons were the following: # - Be closer to the usual Gherkin syntax (parameters of scenario outlines use chevrons <...>, # while Mustache use !{{...}} delimiters), # - Feature files are meant to be simple, so should the template engine be. class Engine + # The regular expression that matches any punctuation sign or delimiter that is forbidden between chevrons <...> template tags. + DisallowedSigns = begin + forbidden = '!"#' + "$%&'()*+,-./:;<=>?[\\]^`{|}~" # Used concatenation (+) to work around Ruby bug! + all_escaped = [] + forbidden.each_char() { |ch| all_escaped << Regexp.escape(ch) } + pattern = all_escaped.join("|") + Regexp.new(pattern) + end + # The original text of the template is kept here. attr_reader(:source) # Builds an Engine and compiles the given template text into an internal representation. # @param aSourceTemplate [String] The template source text. It may contain zero or tags enclosed between chevrons <...>. @@ -138,11 +147,10 @@ end return @variables end - # Class method. Parse the given line text into a raw representation. # @return [Array] Couples of the form: # [:static, text] or [:dynamic, tag text] def self.parse(aTextLine) scanner = StringScanner.new(aTextLine) @@ -209,27 +217,37 @@ line_rep = aRawLine.map { |couple| compile_couple(couple) } line_rep << EOLine.new end - # [aCouple] a two-element array of the form: [kind, text] + # @param aCouple [Array] a two-element array of the form: [kind, text] # Where kind must be one of :static, :dynamic def compile_couple(aCouple) (kind, text) = aCouple result = case kind when :static StaticText.new(text) when :dynamic - Placeholder.new(text) + parse_tag(text) else raise StandardError, "Internal error: Don't know template element of kind #{kind}" end - + return result end + # Parse the contents of a tag entry. + # @param aText [String] The text that is enclosed between chevrons. + def parse_tag(aText) + # Disallow punctuation and delimiter signs in tags. + matching = DisallowedSigns.match(aText) + raise InvalidCharError.new(aText, matching[0]) if matching + + return Placeholder.new(aText) + end + end # class end # module end # module \ No newline at end of file