Class: Utopia::Content::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/processor.rb

Defined Under Namespace

Classes: UnbalancedTagError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate) ⇒ Processor

Returns a new instance of Processor.



79
80
81
82
83
84
# File 'lib/utopia/content/processor.rb', line 79

def initialize(delegate)
  @delegate = delegate
  @stack = []

  @parser = Trenni::Parser.new(self)
end

Class Method Details

.parse_markup(markup, delegate) ⇒ Object



52
53
54
55
56
# File 'lib/utopia/content/processor.rb', line 52

def self.parse_markup(markup, delegate)
  processor = self.new(delegate)
  
  processor.parse(markup)
end

Instance Method Details

#attribute(name, value) ⇒ Object



145
146
147
# File 'lib/utopia/content/processor.rb', line 145

def attribute(name, value)
  @stack.last[0].attributes[name.to_sym] = value
end

#begin_parse(scanner) ⇒ Object



96
97
98
# File 'lib/utopia/content/processor.rb', line 96

def begin_parse(scanner)
  @scanner = scanner
end

#begin_tag(tag_name, begin_tag_type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/utopia/content/processor.rb', line 116

def begin_tag(tag_name, begin_tag_type)
  if begin_tag_type == :opened
    @stack << [Tag.new(tag_name, SymbolicHash.new), @scanner.pos]
  else
    current_tag, current_position = @stack.pop

    if tag_name != current_tag.name
      raise UnbalancedTagError.new(@scanner, current_position, current_tag.name, tag_name)
    end

    @delegate.tag_end(current_tag)
  end
end

#cdata(text) ⇒ Object



108
109
110
# File 'lib/utopia/content/processor.rb', line 108

def cdata(text)
  @delegate.cdata("<![CDATA[#{text}]]>")
end

#comment(text) ⇒ Object



112
113
114
# File 'lib/utopia/content/processor.rb', line 112

def comment(text)
  @delegate.cdata("<!--#{text}-->")
end

#doctype(attributes) ⇒ Object



100
101
102
# File 'lib/utopia/content/processor.rb', line 100

def doctype(attributes)
  @delegate.cdata("<!DOCTYPE#{attributes}>")
end

#finish_tag(begin_tag_type, end_tag_type) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/utopia/content/processor.rb', line 130

def finish_tag(begin_tag_type, end_tag_type)
  if begin_tag_type == :opened # <...
    if end_tag_type == :closed # <.../>
      cur, pos = @stack.pop
      cur.closed = true

      @delegate.tag_complete(cur)
    elsif end_tag_type == :opened # <...>
      cur, pos = @stack.last

      @delegate.tag_begin(cur)
    end
  end
end

#instruction(content) ⇒ Object



149
150
151
# File 'lib/utopia/content/processor.rb', line 149

def instruction(content)
  cdata("<?#{content}?>")
end

#parse(input) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/utopia/content/processor.rb', line 86

def parse(input)
  @parser.parse(input)
  
  unless @stack.empty?
    current_tag, current_position = @stack.pop
    
    raise UnbalancedTagError.new(@scanner, current_position, current_tag.name, 'EOF')
  end
end

#text(text) ⇒ Object



104
105
106
# File 'lib/utopia/content/processor.rb', line 104

def text(text)
  @delegate.cdata(text)
end