class AdHocTemplate::Parser

Public Class Methods

new(source, tag) click to toggle source
Calls superclass method
# File lib/ad_hoc_template/parser.rb, line 302
def initialize(source, tag)
  @tag = tag
  @tokens = PseudoHiki.split_into_tokens(source, @tag.token_pat)
  super()
end
parse(str, tag_name=:default) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 234
def self.parse(str, tag_name=:default)
  str = remove_indents_and_newlines_if_necessary(str, tag_name)
  new(str, TagType[tag_name]).parse!.tree
end
register_user_defined_tag_type(config_source) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 239
def self.register_user_defined_tag_type(config_source)
  config = YAML.safe_load(config_source, [Symbol])
  check_validity_of_config(config)
  TagType.register(registered_tag_name = config['tag_name'].to_sym,
                   config['tag'],
                   config['iteration_tag'],
                   config['fallback_tag'],
                   config['remove_indent'] || false)
  registered_tag_name
end

Private Class Methods

check_validity_of_config(config) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 287
def self.check_validity_of_config(config)
  %w[tag_name tag iteration_tag fallback_tag].each do |item|
    config[item] || raise(UserDefinedTagTypeConfigError,
                          "\"#{item}\" should be defined.")
  end
end
end_tag_alone_re(tag) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 283
def self.end_tag_alone_re(tag)
  /^([ \t]+(?:#{tag})#{LINE_END_STR})/
end
regexp_escape_tag_pair(tag_type, node_class) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 271
def self.regexp_escape_tag_pair(tag_type, node_class)
  [tag_type.head_of[node_class],
   tag_type.tail_of[node_class],].map {|tag| Regexp.escape(tag) }
end
remove_indent_before_fallback_tags(template_source, tag_type) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 266
def self.remove_indent_before_fallback_tags(template_source, tag_type)
  tag_re_str = regexp_escape_tag_pair(tag_type, FallbackNode).join('|')
  template_source.gsub(end_tag_alone_re(tag_re_str), &:lstrip)
end
remove_indent_before_iteration_tags(template_source, tag_type) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 260
def self.remove_indent_before_iteration_tags(template_source, tag_type)
  start_tag, end_tag = regexp_escape_tag_pair(tag_type, IterationNode)
  template_source.gsub(/^([ \t]+#{start_tag}\S*#{LINE_END_STR})/, &:lstrip)
    .gsub(end_tag_alone_re(end_tag), &:lstrip)
end
remove_indents_and_newlines_if_necessary(str, tag_name) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 250
def self.remove_indents_and_newlines_if_necessary(str, tag_name)
  node_types = [IterationNode, FallbackNode]
  tag_type = TagType[tag_name]
  if TagType[tag_name].strip_iteration_indent
    str = remove_indent_before_iteration_tags(str, tag_type)
    str = remove_indent_before_fallback_tags(str, tag_type)
  end
  remove_trailing_newline_of_end_tags(node_types, str, tag_type)
end
remove_trailing_newline_of_end_tags(node_types, source, tag_type) click to toggle source
# File lib/ad_hoc_template/parser.rb, line 276
def self.remove_trailing_newline_of_end_tags(node_types, source, tag_type)
  node_types.inject(source) do |s, node_type|
    end_tag = tag_type.tail_of[node_type]
    s.gsub(/#{Regexp.escape(end_tag)}#{LINE_END_STR}/, end_tag)
  end
end

Public Instance Methods

parse!() click to toggle source
# File lib/ad_hoc_template/parser.rb, line 308
def parse!
  @tokens.each do |token|
    next if @tag.tail[token] == current_node.class && pop
    next if @tag.head[token] && push(@tag.head[token].new)
    push Leaf.create(token)
  end

  @tokens = nil
  self
end