NXML 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/nxml.rb +192 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f08655347f6689ca26e003711ff1286beeaf22f22177902bc642a535bc2daa38
|
4
|
+
data.tar.gz: 2230e2aa5df7ef3612391a18a57bba46f330c285a9eed07513203e9c65b0f729
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 29b85a1c6b9e32b6acbe70533eff4ba7a52e1f740666f7c6dce536c7eaa2c36731c7d2746a53b4963130c5b22b1f3bd3189f91d481ea974b5eab52ae8b652d64
|
7
|
+
data.tar.gz: 7343fdbc8d299b4d745bf817286b57c4fa3a973783f554cc6fd37a2981a64d8309892a1abb4282484dceed91d24814fba994800c0696c38bbc5588614fd035b5
|
data/lib/nxml.rb
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
# Andrej Gelenberg <andrej.gelenberg@udo.edu> (c) 2019
|
2
|
+
|
3
|
+
class NXML
|
4
|
+
|
5
|
+
NAME_START = ":A-Z_a-z\u{C0}-\u{D6}\u{D8}-\u{F6}\u{F8}-\u{2FF}\u{370}-\u{37D}\u{37F}-\u{1FFF}\u{200C}-\u{200D}\u{2070}-\u{218F}\u{2C00}-\u{2FEF}\u{3001}-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFFD}\u{10000}-\u{EFFFF}"
|
6
|
+
NAME_CHAR = "\\-.0-9\u{B7}\u{0300}-\u{036F}\u{203F}-\u{2040}"
|
7
|
+
|
8
|
+
NAME_RE = Regexp.new "^[#{NAME_START}][#{NAME_START}#{NAME_CHAR}]*$"
|
9
|
+
|
10
|
+
ATTR_ESCAPE_HASH = {
|
11
|
+
'&' => '&',
|
12
|
+
'"' => '"',
|
13
|
+
'<' => '<'
|
14
|
+
}
|
15
|
+
ATTR_ESCAPE = Regexp.new "[#{ATTR_ESCAPE_HASH.keys.join}]"
|
16
|
+
|
17
|
+
TEXT_ESCAPE_HASH = {
|
18
|
+
'&' => '&',
|
19
|
+
'<' => '>',
|
20
|
+
'>' => '<'
|
21
|
+
}
|
22
|
+
TEXT_ESCAPE = Regexp.new "[#{TEXT_ESCAPE_HASH.keys.join}]"
|
23
|
+
|
24
|
+
def self.check_name(name)
|
25
|
+
if not name.match? NAME_RE
|
26
|
+
raise ArgumentError.new "invalid XML name \"#{name}\""
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.escape_attr_value(value)
|
31
|
+
value.gsub ATTR_ESCAPE, ATTR_ESCAPE_HASH
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.escape_attr_raw(value)
|
35
|
+
value.gsub /["]/, '"'
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.escape_text(value)
|
39
|
+
value.gsub TEXT_ESCAPE, TEXT_ESCAPE_HASH
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.escape_text_raw(value)
|
43
|
+
value.gsub /[<]/, '<'
|
44
|
+
end
|
45
|
+
|
46
|
+
class Text
|
47
|
+
def initialize(text, raw = false)
|
48
|
+
if raw
|
49
|
+
@text = NXML::escape_text_raw text
|
50
|
+
else
|
51
|
+
@text = NXML::escape_text text
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_str
|
56
|
+
@text
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Element
|
61
|
+
def initialize(name, &block)
|
62
|
+
NXML::check_name name
|
63
|
+
|
64
|
+
@name = name
|
65
|
+
@attributes = {}
|
66
|
+
@children = []
|
67
|
+
|
68
|
+
instance_eval &block if block_given?
|
69
|
+
end
|
70
|
+
|
71
|
+
def method_missing(name, value = nil, *attrs)
|
72
|
+
if name[-1] == '='
|
73
|
+
name = name[0..-2]
|
74
|
+
if value.nil?
|
75
|
+
@attributes[name]
|
76
|
+
else
|
77
|
+
attribute name, value
|
78
|
+
end
|
79
|
+
else
|
80
|
+
super name, value, *attrs
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def tag(name, &block)
|
85
|
+
r = Element.new(name, &block)
|
86
|
+
@children << r
|
87
|
+
r
|
88
|
+
end
|
89
|
+
|
90
|
+
def text(text)
|
91
|
+
r = Text.new(text)
|
92
|
+
@children << r
|
93
|
+
r
|
94
|
+
end
|
95
|
+
|
96
|
+
def cdata(text)
|
97
|
+
r = CDATA.new(text)
|
98
|
+
@children << r
|
99
|
+
r
|
100
|
+
end
|
101
|
+
|
102
|
+
def attribute(name, value)
|
103
|
+
NXML::check_name name
|
104
|
+
r = NXML::escape_attr_value value
|
105
|
+
@attributes[name] = r
|
106
|
+
r
|
107
|
+
end
|
108
|
+
|
109
|
+
def attribute_raw(name, value)
|
110
|
+
XML::check_name name
|
111
|
+
|
112
|
+
r = NXML::escape_attr_raw value
|
113
|
+
@attributes[name] = r
|
114
|
+
r
|
115
|
+
end
|
116
|
+
|
117
|
+
def a(name, value)
|
118
|
+
attribute name, value
|
119
|
+
end
|
120
|
+
|
121
|
+
def to_str
|
122
|
+
ret = "<#{@name}"
|
123
|
+
|
124
|
+
if not @attributes.empty?
|
125
|
+
for k,v in @attributes
|
126
|
+
ret += " #{k}=\"#{v}\""
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
if @children.empty?
|
131
|
+
ret += "/>"
|
132
|
+
else
|
133
|
+
ret += ">#{@children.join}</#{@name}>"
|
134
|
+
end
|
135
|
+
|
136
|
+
ret
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
class CDATA
|
142
|
+
def initialize(data)
|
143
|
+
if not data.index(']]>').nil?
|
144
|
+
raise ArgumentError.new "CDATA should not contain CDATA closing sequence ']]>'"
|
145
|
+
end
|
146
|
+
|
147
|
+
@data = data
|
148
|
+
end
|
149
|
+
|
150
|
+
def to_str
|
151
|
+
"<![CDATA[#{@data}]]>"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
attr_accessor :version, :encoding, :standalone, :root
|
156
|
+
|
157
|
+
def initialize(&block)
|
158
|
+
version = "1.0"
|
159
|
+
@encoding = Encoding::UTF_8
|
160
|
+
|
161
|
+
instance_eval &block
|
162
|
+
end
|
163
|
+
|
164
|
+
def root(tagname, &block)
|
165
|
+
@root = Element.new tagname, &block
|
166
|
+
end
|
167
|
+
|
168
|
+
def to_str
|
169
|
+
ret = "<?xml"
|
170
|
+
|
171
|
+
if @root.nil?
|
172
|
+
end
|
173
|
+
|
174
|
+
if not @version.nil?
|
175
|
+
ret += " version=\"#{@version}\""
|
176
|
+
end
|
177
|
+
|
178
|
+
if not @encoding.nil?
|
179
|
+
ret += " encoding=\"#{@encoding.name}\""
|
180
|
+
end
|
181
|
+
|
182
|
+
if not @standalone.nil?
|
183
|
+
ret += " standalone=\"#{@standalone ? "yes" : "no"}\""
|
184
|
+
end
|
185
|
+
|
186
|
+
ret += "?>\n"
|
187
|
+
|
188
|
+
ret += @root
|
189
|
+
|
190
|
+
ret.encode @encoding
|
191
|
+
end
|
192
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: NXML
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrej Gelenberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: library for generating XML 1.0 documents
|
14
|
+
email: andrej.gelenberg@udo.edu
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/nxml.rb
|
20
|
+
homepage: https://gitlab.com/andrej.gelenberg/nxml
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.7.6.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: NXML
|
44
|
+
test_files: []
|