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.



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

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



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

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

#begin_parse(scanner) ⇒ Object



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

def begin_parse(scanner)
	@scanner = scanner
end

#begin_tag(tag_name, begin_tag_type) ⇒ Object



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

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



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

def cdata(text)
	@delegate.cdata(Trenni::Strings::to_html(text))
end

#comment(text) ⇒ Object



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

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

#finish_tag(begin_tag_type, end_tag_type) ⇒ Object



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

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



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

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

#parse(input) ⇒ Object



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

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



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

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