class Wptemplates::Parser
Public Instance Methods
parse(text)
click to toggle source
# File lib/wptemplates/parser.rb, line 11 def parse(text) @input = StringScanner.new(text) parse_main end
Protected Instance Methods
link_new_with_normalize(text, link, anchor)
click to toggle source
# File lib/wptemplates/parser.rb, line 104 def link_new_with_normalize text, link, anchor text = normalize_linklabel(text) link = normalize_link(link) anchor = normalize_link(anchor, true) unless anchor.nil? Link.new(text, link, anchor) end
parse_anything(in_template_parameter = false)
click to toggle source
# File lib/wptemplates/parser.rb, line 38 def parse_anything in_template_parameter = false if in_template_parameter @input.scan(till_doublebrace_doubleopenbrackets_or_pipe) && Text.new(@input.matched) else @input.scan(till_doubleopenbrace_or_doubleopenbrackets) && Text.new(@input.matched) end end
parse_link()
click to toggle source
# File lib/wptemplates/parser.rb, line 80 def parse_link if @input.scan(a_link) url, label, letters = (1..3).map {|i| @input[i]} if label == "" pipe_trick url, label, letters else link_new_with_normalize (label || url)+letters, url[until_hash], url[after_hash] end end end
parse_main(in_template_parameter = false)
click to toggle source
# File lib/wptemplates/parser.rb, line 18 def parse_main in_template_parameter = false output = Soup.new while unit = parse_link || parse_template || parse_anything(in_template_parameter) output << unit end output << Text.new("") if output.empty? output end
parse_numeric_template_parameter(h,i)
click to toggle source
# File lib/wptemplates/parser.rb, line 73 def parse_numeric_template_parameter(h,i) if @input.scan(a_pipe) value = parse_main(true) h[i] = value end end
parse_template()
click to toggle source
# File lib/wptemplates/parser.rb, line 30 def parse_template if @input.scan(a_doubleopenbrace) template = Template.new parse_template_name, parse_template_parameters @input.scan(a_doubleclosingbrace) or raise "unclosed template" template end end
parse_template_name()
click to toggle source
# File lib/wptemplates/parser.rb, line 46 def parse_template_name if @input.scan(till_doubleclosebrace_or_pipe) symbolize(@input.matched) end end
parse_template_parameters()
click to toggle source
# File lib/wptemplates/parser.rb, line 52 def parse_template_parameters i = 0 h = {} while parsed_named_template_parameter(h) || parse_numeric_template_parameter(h,i) do i += 1 end h end
parsed_named_template_parameter(h)
click to toggle source
# File lib/wptemplates/parser.rb, line 61 def parsed_named_template_parameter(h) if @input.scan(from_pipe_till_equals_no_doubleclosebrace_or_pipe) key = symbolize(@input[1]) value = parse_main(true) {l:0,r:-1}.each do |d,i| value[i].text.send(:"#{d}strip!") value.delete_at(i) if value[i].text.empty? && (value.length > 1) end h[key] = value end end
pipe_trick(url, label, letters)
click to toggle source
# File lib/wptemplates/parser.rb, line 91 def pipe_trick url, label, letters if url["#"] nil elsif m = has_parens.match(url) link_new_with_normalize(m[:no_parens]+letters, url, nil) else label = fixpoint(clone: true, start: url) do |u| u[first_comma,:before][parens, :before] end link_new_with_normalize(label+letters, url, nil) end end