forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlwriter.rb
More file actions
91 lines (78 loc) · 1.73 KB
/
Copy pathxmlwriter.rb
File metadata and controls
91 lines (78 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
class XMLWriter
def initialize(io)
@io = io
end
def doctype(name, syspub, public_id, system_id)
@io.puts("<!DOCTYPE #{name} #{syspub} \"#{public_id}\" \"#{system_id}\">")
end
def element(text, attrs=nil)
start_tag(text, attrs)
yield
end_tag(text)
end
def simple_element(text, body, attrs=nil)
start_tag(text, attrs)
pcdata(body)
end_tag(text)
end
def start_tag(text, attrs=nil)
chk_name(text)
@io.print('<')
@io.print(text)
attrs(attrs)
@io.print('>')
end
def empty_tag(text, attrs=nil)
chk_name(text)
@io.print('<')
@io.print(text)
attrs(attrs)
@io.print('/>')
end
def end_tag(text)
chk_name(text)
@io.print('</')
@io.print(text)
@io.print('>')
end
def pcdata(text)
@io.print(text.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>'))
end
def cdata(text)
raise "CDATA text must not contain ']]>'" if text =~ /\]\]>/
@io.print("<![CDATA[")
@io.print(text)
@io.print("]]>")
end
def comment(text)
raise "comment must not contain '--'" if text =~ /--/
@io.print("<!--")
@io.print(text)
@io.print("-->")
end
def pi(text)
raise "processing instruction must not contain '?>'" if text =~ /\?>/
@io.print("<?")
@io.print(text)
@io.print("?>")
end
def passthrough(text)
@io.print(text)
end
private
def chk_name(name)
raise "bad character '#{$&}' in tag name #{name}" if name =~ /[<>& "']/
end
def attrs(attrs)
unless attrs.nil?
attrs.each do |key, val|
raise "#{key.inspect}=#{val.inspect}" if key.nil? || val.nil?
@io.print(' ')
@io.print(key)
@io.print('="')
@io.print(val.gsub(/"/, """))
@io.print('"')
end
end
end
end