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.



56
57
58
59
60
61
# File 'lib/utopia/content/processor.rb', line 56

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

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

Class Method Details

.parse_xml(xml_data, delegate) ⇒ Object



29
30
31
32
33
# File 'lib/utopia/content/processor.rb', line 29

def self.parse_xml(xml_data, delegate)
	processor = self.new(delegate)
	
	processor.parse(xml_data)
end

Instance Method Details

#attribute(name, value) ⇒ Object



118
119
120
# File 'lib/utopia/content/processor.rb', line 118

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

#begin_parse(scanner) ⇒ Object



73
74
75
# File 'lib/utopia/content/processor.rb', line 73

def begin_parse(scanner)
	@scanner = scanner
end

#begin_tag(tag_name, begin_tag_type) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/utopia/content/processor.rb', line 89

def begin_tag(tag_name, begin_tag_type)
	if begin_tag_type == :opened
		@stack << [Tag.new(tag_name, {}), @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



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

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

#comment(text) ⇒ Object



85
86
87
# File 'lib/utopia/content/processor.rb', line 85

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

#finish_tag(begin_tag_type, end_tag_type) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/utopia/content/processor.rb', line 103

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



122
123
124
# File 'lib/utopia/content/processor.rb', line 122

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

#parse(input) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/utopia/content/processor.rb', line 63

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



77
78
79
# File 'lib/utopia/content/processor.rb', line 77

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