Class: Condenser::SVGTransformer::Template
- Inherits:
-
Object
- Object
- Condenser::SVGTransformer::Template
- Includes:
- ParseHelpers
- Defined in:
- lib/condenser/transformers/svg_transformer/template.rb
Constant Summary collapse
- START_TAGS =
['<']
- CLOSE_TAGS =
['/>', '>']
- VOID_ELEMENTS =
['!DOCTYPE', '?xml']
Instance Attribute Summary collapse
-
#source ⇒ Object
Returns the value of attribute source.
Attributes included from ParseHelpers
Instance Method Summary collapse
-
#initialize(source) ⇒ Template
constructor
A new instance of Template.
- #process ⇒ Object
- #to_module ⇒ Object
Methods included from ParseHelpers
#current_line, #cursor, #eos?, #forward, #gobble, #next_word, #pre_match, #rewind, #scan_until, #seek
Constructor Details
#initialize(source) ⇒ Template
Returns a new instance of Template.
11 12 13 14 |
# File 'lib/condenser/transformers/svg_transformer/template.rb', line 11 def initialize(source) @source = source.strip process end |
Instance Attribute Details
#source ⇒ Object
Returns the value of attribute source.
5 6 7 |
# File 'lib/condenser/transformers/svg_transformer/template.rb', line 5 def source @source end |
Instance Method Details
#process ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/condenser/transformers/svg_transformer/template.rb', line 16 def process seek(0) @tree = [Condenser::SVGTransformer::Base.new] @stack = [:str] while !eos? case @stack.last when :str scan_until(Regexp.new("(#{START_TAGS.map{|s| Regexp.escape(s) }.join('|')}|\\z)")) if !matched.nil? && START_TAGS.include?(matched) @stack << :tag end when :tag scan_until(Regexp.new("(\\/|[^\\s>]+)")) if matched == '/' @stack.pop @stack << :close_tag else @tree << Condenser::SVGTransformer::Tag.new(matched) @stack << :tag_attr_key end when :close_tag scan_until(Regexp.new("([^\\s>]+)")) el = @tree.pop if el.tag_name != matched raise Condenser::SVGTransformer::TemplateError.new("Expected to close #{el.tag_name.inspect} tag, instead closed #{matched.inspect}\n#{cursor}") end if !['!DOCTYPE', '?xml'].include?(el.tag_name) @tree.last.children << el scan_until(Regexp.new("(#{CLOSE_TAGS.map{|s| Regexp.escape(s) }.join('|')})")) @stack.pop end when :tag_attr_key scan_until(Regexp.new("(#{CLOSE_TAGS.map{|s| Regexp.escape(s) }.join('|')}|[^\\s=>]+)")) if CLOSE_TAGS.include?(matched) if matched == '/>' || VOID_ELEMENTS.include?(@tree.last.tag_name) el = @tree.pop @tree.last.children << el @stack.pop @stack.pop else @stack << :str end else key = if matched.start_with?('"') && matched.end_with?('"') matched[1..-2] elsif matched.start_with?('"') && matched.end_with?('"') matched[1..-2] else matched end @tree.last.attrs << key @stack << :tag_attr_value_tx end when :tag_attr_value_tx scan_until(Regexp.new("(#{(CLOSE_TAGS).map{|s| Regexp.escape(s) }.join('|')}|=|\\S)")) tag_key = @tree.last.attrs.pop if CLOSE_TAGS.include?(matched) el = @tree.last el.attrs << tag_key if VOID_ELEMENTS.include?(el.tag_name) @tree.pop @tree.last.children << el end @stack.pop @stack.pop @stack.pop elsif matched == '=' @stack.pop @tree.last.attrs << tag_key @stack << :tag_attr_value else @stack.pop @tree.last.attrs << tag_key rewind(1) end when :tag_attr_value scan_until(Regexp.new("(#{CLOSE_TAGS.map{|s| Regexp.escape(s) }.join('|')}|'|\"|\\S+)")) if matched == '"' @stack.pop @stack << :tag_attr_value_double_quoted elsif matched == "'" @stack.pop @stack << :tag_attr_value_single_quoted else @stack.pop key = @tree.last.attrs.pop @tree.last.namespace = matched if key == 'xmlns' @tree.last.attrs << { key => matched } end when :tag_attr_value_double_quoted quoted_value = '' scan_until(/"/) quoted_value << pre_match if !pre_match.strip.empty? rewind(1) quoted_value = Condenser::SVGTransformer::Value.new(quoted_value) key = @tree.last.attrs.pop @tree.last.namespace = quoted_value if key == 'xmlns' if @tree.last.attrs.last.is_a?(Hash) && !@tree.last.attrs.last.has_key?(key) @tree.last.attrs.last[key] = quoted_value else @tree.last.attrs << { key => quoted_value } end scan_until(/\"/) @stack.pop when :tag_attr_value_single_quoted quoted_value = '' scan_until(/(')/) quoted_value << pre_match if !pre_match.strip.empty? rewind(1) quoted_value = Condenser::SVGTransformer::Value.new(quoted_value) key = @tree.last.attrs.pop @tree.last.namespace = quoted_value if key == 'xmlns' if @tree.last.attrs.last.is_a?(Hash) && !@tree.last.attrs.last.has_key?(key) @tree.last.attrs.last[key] = quoted_value else @tree.last.attrs << { key => quoted_value } end scan_until(/\'/) @stack.pop end end end |
#to_module ⇒ Object
147 148 149 |
# File 'lib/condenser/transformers/svg_transformer/template.rb', line 147 def to_module @tree.first.to_module end |