nokogiri 1.11.0.rc4-arm64-darwin
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/LICENSE-DEPENDENCIES.md +1682 -0
- data/LICENSE.md +9 -0
- data/README.md +200 -0
- data/bin/nokogiri +118 -0
- data/dependencies.yml +74 -0
- data/ext/nokogiri/depend +477 -0
- data/ext/nokogiri/extconf.rb +819 -0
- data/ext/nokogiri/html_document.c +171 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/include/libexslt/exslt.h +102 -0
- data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
- data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
- data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
- data/ext/nokogiri/include/libxml2/libxml/c14n.h +126 -0
- data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
- data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
- data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
- data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
- data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
- data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
- data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
- data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
- data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
- data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
- data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
- data/ext/nokogiri/include/libxml2/libxml/parser.h +1241 -0
- data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
- data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
- data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
- data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
- data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
- data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
- data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
- data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
- data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
- data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
- data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +366 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +945 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +153 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
- data/ext/nokogiri/include/libxml2/libxml/xpath.h +566 -0
- data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
- data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
- data/ext/nokogiri/include/libxslt/attributes.h +38 -0
- data/ext/nokogiri/include/libxslt/documents.h +93 -0
- data/ext/nokogiri/include/libxslt/extensions.h +262 -0
- data/ext/nokogiri/include/libxslt/extra.h +72 -0
- data/ext/nokogiri/include/libxslt/functions.h +78 -0
- data/ext/nokogiri/include/libxslt/imports.h +75 -0
- data/ext/nokogiri/include/libxslt/keys.h +53 -0
- data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
- data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
- data/ext/nokogiri/include/libxslt/pattern.h +84 -0
- data/ext/nokogiri/include/libxslt/preproc.h +43 -0
- data/ext/nokogiri/include/libxslt/security.h +104 -0
- data/ext/nokogiri/include/libxslt/templates.h +77 -0
- data/ext/nokogiri/include/libxslt/transform.h +207 -0
- data/ext/nokogiri/include/libxslt/variables.h +118 -0
- data/ext/nokogiri/include/libxslt/xslt.h +110 -0
- data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
- data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
- data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
- data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
- data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
- data/ext/nokogiri/nokogiri.c +135 -0
- data/ext/nokogiri/nokogiri.h +130 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +62 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +69 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +622 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +63 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +111 -0
- data/ext/nokogiri/xml_namespace.h +14 -0
- data/ext/nokogiri/xml_node.c +1773 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +486 -0
- data/ext/nokogiri/xml_node_set.h +12 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +657 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +179 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +305 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +159 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +276 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +64 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +374 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +263 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/nokogiri.rb +127 -0
- data/lib/nokogiri/2.5/nokogiri.bundle +0 -0
- data/lib/nokogiri/2.6/nokogiri.bundle +0 -0
- data/lib/nokogiri/2.7/nokogiri.bundle +0 -0
- data/lib/nokogiri/3.0/nokogiri.bundle +0 -0
- data/lib/nokogiri/css.rb +28 -0
- data/lib/nokogiri/css/node.rb +53 -0
- data/lib/nokogiri/css/parser.rb +751 -0
- data/lib/nokogiri/css/parser.y +272 -0
- data/lib/nokogiri/css/parser_extras.rb +94 -0
- data/lib/nokogiri/css/syntax_error.rb +8 -0
- data/lib/nokogiri/css/tokenizer.rb +154 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +260 -0
- data/lib/nokogiri/decorators/slop.rb +43 -0
- data/lib/nokogiri/html.rb +38 -0
- data/lib/nokogiri/html/builder.rb +36 -0
- data/lib/nokogiri/html/document.rb +322 -0
- data/lib/nokogiri/html/document_fragment.rb +50 -0
- data/lib/nokogiri/html/element_description.rb +24 -0
- data/lib/nokogiri/html/element_description_defaults.rb +672 -0
- data/lib/nokogiri/html/entity_lookup.rb +14 -0
- data/lib/nokogiri/html/sax/parser.rb +63 -0
- data/lib/nokogiri/html/sax/parser_context.rb +17 -0
- data/lib/nokogiri/html/sax/push_parser.rb +37 -0
- data/lib/nokogiri/jruby/dependencies.rb +20 -0
- data/lib/nokogiri/syntax_error.rb +5 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +182 -0
- data/lib/nokogiri/xml.rb +76 -0
- data/lib/nokogiri/xml/attr.rb +15 -0
- data/lib/nokogiri/xml/attribute_decl.rb +19 -0
- data/lib/nokogiri/xml/builder.rb +447 -0
- data/lib/nokogiri/xml/cdata.rb +12 -0
- data/lib/nokogiri/xml/character_data.rb +8 -0
- data/lib/nokogiri/xml/document.rb +290 -0
- data/lib/nokogiri/xml/document_fragment.rb +159 -0
- data/lib/nokogiri/xml/dtd.rb +33 -0
- data/lib/nokogiri/xml/element_content.rb +37 -0
- data/lib/nokogiri/xml/element_decl.rb +14 -0
- data/lib/nokogiri/xml/entity_decl.rb +20 -0
- data/lib/nokogiri/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +14 -0
- data/lib/nokogiri/xml/node.rb +1240 -0
- data/lib/nokogiri/xml/node/save_options.rb +62 -0
- data/lib/nokogiri/xml/node_set.rb +372 -0
- data/lib/nokogiri/xml/notation.rb +7 -0
- data/lib/nokogiri/xml/parse_options.rb +127 -0
- data/lib/nokogiri/xml/pp.rb +3 -0
- data/lib/nokogiri/xml/pp/character_data.rb +19 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/processing_instruction.rb +9 -0
- data/lib/nokogiri/xml/reader.rb +116 -0
- data/lib/nokogiri/xml/relax_ng.rb +37 -0
- data/lib/nokogiri/xml/sax.rb +5 -0
- data/lib/nokogiri/xml/sax/document.rb +172 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/schema.rb +72 -0
- data/lib/nokogiri/xml/searchable.rb +239 -0
- data/lib/nokogiri/xml/syntax_error.rb +71 -0
- data/lib/nokogiri/xml/text.rb +10 -0
- data/lib/nokogiri/xml/xpath.rb +11 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
- data/lib/nokogiri/xml/xpath_context.rb +17 -0
- data/lib/nokogiri/xslt.rb +57 -0
- data/lib/nokogiri/xslt/stylesheet.rb +26 -0
- data/lib/xsd/xmlparser/nokogiri.rb +103 -0
- metadata +565 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
###
|
5
|
+
# Represents an attribute declaration in a DTD
|
6
|
+
class AttributeDecl < Nokogiri::XML::Node
|
7
|
+
undef_method :attribute_nodes
|
8
|
+
undef_method :attributes
|
9
|
+
undef_method :content
|
10
|
+
undef_method :namespace
|
11
|
+
undef_method :namespace_definitions
|
12
|
+
undef_method :line if method_defined?(:line)
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"#<#{self.class.name}:#{sprintf("0x%x", object_id)} #{to_s.inspect}>"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,447 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
###
|
5
|
+
# Nokogiri builder can be used for building XML and HTML documents.
|
6
|
+
#
|
7
|
+
# == Synopsis:
|
8
|
+
#
|
9
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
10
|
+
# xml.root {
|
11
|
+
# xml.products {
|
12
|
+
# xml.widget {
|
13
|
+
# xml.id_ "10"
|
14
|
+
# xml.name "Awesome widget"
|
15
|
+
# }
|
16
|
+
# }
|
17
|
+
# }
|
18
|
+
# end
|
19
|
+
# puts builder.to_xml
|
20
|
+
#
|
21
|
+
# Will output:
|
22
|
+
#
|
23
|
+
# <?xml version="1.0"?>
|
24
|
+
# <root>
|
25
|
+
# <products>
|
26
|
+
# <widget>
|
27
|
+
# <id>10</id>
|
28
|
+
# <name>Awesome widget</name>
|
29
|
+
# </widget>
|
30
|
+
# </products>
|
31
|
+
# </root>
|
32
|
+
#
|
33
|
+
#
|
34
|
+
# === Builder scope
|
35
|
+
#
|
36
|
+
# The builder allows two forms. When the builder is supplied with a block
|
37
|
+
# that has a parameter, the outside scope is maintained. This means you
|
38
|
+
# can access variables that are outside your builder. If you don't need
|
39
|
+
# outside scope, you can use the builder without the "xml" prefix like
|
40
|
+
# this:
|
41
|
+
#
|
42
|
+
# builder = Nokogiri::XML::Builder.new do
|
43
|
+
# root {
|
44
|
+
# products {
|
45
|
+
# widget {
|
46
|
+
# id_ "10"
|
47
|
+
# name "Awesome widget"
|
48
|
+
# }
|
49
|
+
# }
|
50
|
+
# }
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# == Special Tags
|
54
|
+
#
|
55
|
+
# The builder works by taking advantage of method_missing. Unfortunately
|
56
|
+
# some methods are defined in ruby that are difficult or dangerous to
|
57
|
+
# remove. You may want to create tags with the name "type", "class", and
|
58
|
+
# "id" for example. In that case, you can use an underscore to
|
59
|
+
# disambiguate your tag name from the method call.
|
60
|
+
#
|
61
|
+
# Here is an example of using the underscore to disambiguate tag names from
|
62
|
+
# ruby methods:
|
63
|
+
#
|
64
|
+
# @objects = [Object.new, Object.new, Object.new]
|
65
|
+
#
|
66
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
67
|
+
# xml.root {
|
68
|
+
# xml.objects {
|
69
|
+
# @objects.each do |o|
|
70
|
+
# xml.object {
|
71
|
+
# xml.type_ o.type
|
72
|
+
# xml.class_ o.class.name
|
73
|
+
# xml.id_ o.id
|
74
|
+
# }
|
75
|
+
# end
|
76
|
+
# }
|
77
|
+
# }
|
78
|
+
# end
|
79
|
+
# puts builder.to_xml
|
80
|
+
#
|
81
|
+
# The underscore may be used with any tag name, and the last underscore
|
82
|
+
# will just be removed. This code will output the following XML:
|
83
|
+
#
|
84
|
+
# <?xml version="1.0"?>
|
85
|
+
# <root>
|
86
|
+
# <objects>
|
87
|
+
# <object>
|
88
|
+
# <type>Object</type>
|
89
|
+
# <class>Object</class>
|
90
|
+
# <id>48390</id>
|
91
|
+
# </object>
|
92
|
+
# <object>
|
93
|
+
# <type>Object</type>
|
94
|
+
# <class>Object</class>
|
95
|
+
# <id>48380</id>
|
96
|
+
# </object>
|
97
|
+
# <object>
|
98
|
+
# <type>Object</type>
|
99
|
+
# <class>Object</class>
|
100
|
+
# <id>48370</id>
|
101
|
+
# </object>
|
102
|
+
# </objects>
|
103
|
+
# </root>
|
104
|
+
#
|
105
|
+
# == Tag Attributes
|
106
|
+
#
|
107
|
+
# Tag attributes may be supplied as method arguments. Here is our
|
108
|
+
# previous example, but using attributes rather than tags:
|
109
|
+
#
|
110
|
+
# @objects = [Object.new, Object.new, Object.new]
|
111
|
+
#
|
112
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
113
|
+
# xml.root {
|
114
|
+
# xml.objects {
|
115
|
+
# @objects.each do |o|
|
116
|
+
# xml.object(:type => o.type, :class => o.class, :id => o.id)
|
117
|
+
# end
|
118
|
+
# }
|
119
|
+
# }
|
120
|
+
# end
|
121
|
+
# puts builder.to_xml
|
122
|
+
#
|
123
|
+
# === Tag Attribute Short Cuts
|
124
|
+
#
|
125
|
+
# A couple attribute short cuts are available when building tags. The
|
126
|
+
# short cuts are available by special method calls when building a tag.
|
127
|
+
#
|
128
|
+
# This example builds an "object" tag with the class attribute "classy"
|
129
|
+
# and the id of "thing":
|
130
|
+
#
|
131
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
132
|
+
# xml.root {
|
133
|
+
# xml.objects {
|
134
|
+
# xml.object.classy.thing!
|
135
|
+
# }
|
136
|
+
# }
|
137
|
+
# end
|
138
|
+
# puts builder.to_xml
|
139
|
+
#
|
140
|
+
# Which will output:
|
141
|
+
#
|
142
|
+
# <?xml version="1.0"?>
|
143
|
+
# <root>
|
144
|
+
# <objects>
|
145
|
+
# <object class="classy" id="thing"/>
|
146
|
+
# </objects>
|
147
|
+
# </root>
|
148
|
+
#
|
149
|
+
# All other options are still supported with this syntax, including
|
150
|
+
# blocks and extra tag attributes.
|
151
|
+
#
|
152
|
+
# == Namespaces
|
153
|
+
#
|
154
|
+
# Namespaces are added similarly to attributes. Nokogiri::XML::Builder
|
155
|
+
# assumes that when an attribute starts with "xmlns", it is meant to be
|
156
|
+
# a namespace:
|
157
|
+
#
|
158
|
+
# builder = Nokogiri::XML::Builder.new { |xml|
|
159
|
+
# xml.root('xmlns' => 'default', 'xmlns:foo' => 'bar') do
|
160
|
+
# xml.tenderlove
|
161
|
+
# end
|
162
|
+
# }
|
163
|
+
# puts builder.to_xml
|
164
|
+
#
|
165
|
+
# Will output XML like this:
|
166
|
+
#
|
167
|
+
# <?xml version="1.0"?>
|
168
|
+
# <root xmlns:foo="bar" xmlns="default">
|
169
|
+
# <tenderlove/>
|
170
|
+
# </root>
|
171
|
+
#
|
172
|
+
# === Referencing declared namespaces
|
173
|
+
#
|
174
|
+
# Tags that reference non-default namespaces (i.e. a tag "foo:bar") can be
|
175
|
+
# built by using the Nokogiri::XML::Builder#[] method.
|
176
|
+
#
|
177
|
+
# For example:
|
178
|
+
#
|
179
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
180
|
+
# xml.root('xmlns:foo' => 'bar') {
|
181
|
+
# xml.objects {
|
182
|
+
# xml['foo'].object.classy.thing!
|
183
|
+
# }
|
184
|
+
# }
|
185
|
+
# end
|
186
|
+
# puts builder.to_xml
|
187
|
+
#
|
188
|
+
# Will output this XML:
|
189
|
+
#
|
190
|
+
# <?xml version="1.0"?>
|
191
|
+
# <root xmlns:foo="bar">
|
192
|
+
# <objects>
|
193
|
+
# <foo:object class="classy" id="thing"/>
|
194
|
+
# </objects>
|
195
|
+
# </root>
|
196
|
+
#
|
197
|
+
# Note the "foo:object" tag.
|
198
|
+
#
|
199
|
+
# == Document Types
|
200
|
+
#
|
201
|
+
# To create a document type (DTD), access use the Builder#doc method to get
|
202
|
+
# the current context document. Then call Node#create_internal_subset to
|
203
|
+
# create the DTD node.
|
204
|
+
#
|
205
|
+
# For example, this Ruby:
|
206
|
+
#
|
207
|
+
# builder = Nokogiri::XML::Builder.new do |xml|
|
208
|
+
# xml.doc.create_internal_subset(
|
209
|
+
# 'html',
|
210
|
+
# "-//W3C//DTD HTML 4.01 Transitional//EN",
|
211
|
+
# "http://www.w3.org/TR/html4/loose.dtd"
|
212
|
+
# )
|
213
|
+
# xml.root do
|
214
|
+
# xml.foo
|
215
|
+
# end
|
216
|
+
# end
|
217
|
+
#
|
218
|
+
# puts builder.to_xml
|
219
|
+
#
|
220
|
+
# Will output this xml:
|
221
|
+
#
|
222
|
+
# <?xml version="1.0"?>
|
223
|
+
# <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
224
|
+
# <root>
|
225
|
+
# <foo/>
|
226
|
+
# </root>
|
227
|
+
#
|
228
|
+
class Builder
|
229
|
+
# The current Document object being built
|
230
|
+
attr_accessor :doc
|
231
|
+
|
232
|
+
# The parent of the current node being built
|
233
|
+
attr_accessor :parent
|
234
|
+
|
235
|
+
# A context object for use when the block has no arguments
|
236
|
+
attr_accessor :context
|
237
|
+
|
238
|
+
attr_accessor :arity # :nodoc:
|
239
|
+
|
240
|
+
###
|
241
|
+
# Create a builder with an existing root object. This is for use when
|
242
|
+
# you have an existing document that you would like to augment with
|
243
|
+
# builder methods. The builder context created will start with the
|
244
|
+
# given +root+ node.
|
245
|
+
#
|
246
|
+
# For example:
|
247
|
+
#
|
248
|
+
# doc = Nokogiri::XML(File.read('somedoc.xml'))
|
249
|
+
# Nokogiri::XML::Builder.with(doc.at_css('some_tag')) do |xml|
|
250
|
+
# # ... Use normal builder methods here ...
|
251
|
+
# xml.awesome # add the "awesome" tag below "some_tag"
|
252
|
+
# end
|
253
|
+
#
|
254
|
+
def self.with(root, &block)
|
255
|
+
new({}, root, &block)
|
256
|
+
end
|
257
|
+
|
258
|
+
###
|
259
|
+
# Create a new Builder object. +options+ are sent to the top level
|
260
|
+
# Document that is being built.
|
261
|
+
#
|
262
|
+
# Building a document with a particular encoding for example:
|
263
|
+
#
|
264
|
+
# Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
265
|
+
# ...
|
266
|
+
# end
|
267
|
+
def initialize(options = {}, root = nil, &block)
|
268
|
+
if root
|
269
|
+
@doc = root.document
|
270
|
+
@parent = root
|
271
|
+
else
|
272
|
+
klassname = "::" + (self.class.name.split("::")[0..-2] + ["Document"]).join("::")
|
273
|
+
klass = begin
|
274
|
+
Object.const_get(klassname)
|
275
|
+
rescue NameError
|
276
|
+
Nokogiri::XML::Document
|
277
|
+
end
|
278
|
+
@parent = @doc = klass.new
|
279
|
+
end
|
280
|
+
|
281
|
+
@context = nil
|
282
|
+
@arity = nil
|
283
|
+
@ns = nil
|
284
|
+
|
285
|
+
options.each do |k, v|
|
286
|
+
@doc.send(:"#{k}=", v)
|
287
|
+
end
|
288
|
+
|
289
|
+
return unless block_given?
|
290
|
+
|
291
|
+
@arity = block.arity
|
292
|
+
if @arity <= 0
|
293
|
+
@context = eval("self", block.binding)
|
294
|
+
instance_eval(&block)
|
295
|
+
else
|
296
|
+
yield self
|
297
|
+
end
|
298
|
+
|
299
|
+
@parent = @doc
|
300
|
+
end
|
301
|
+
|
302
|
+
###
|
303
|
+
# Create a Text Node with content of +string+
|
304
|
+
def text(string)
|
305
|
+
insert @doc.create_text_node(string)
|
306
|
+
end
|
307
|
+
|
308
|
+
###
|
309
|
+
# Create a CDATA Node with content of +string+
|
310
|
+
def cdata(string)
|
311
|
+
insert doc.create_cdata(string)
|
312
|
+
end
|
313
|
+
|
314
|
+
###
|
315
|
+
# Create a Comment Node with content of +string+
|
316
|
+
def comment(string)
|
317
|
+
insert doc.create_comment(string)
|
318
|
+
end
|
319
|
+
|
320
|
+
###
|
321
|
+
# Build a tag that is associated with namespace +ns+. Raises an
|
322
|
+
# ArgumentError if +ns+ has not been defined higher in the tree.
|
323
|
+
def [](ns)
|
324
|
+
if @parent != @doc
|
325
|
+
@ns = @parent.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
326
|
+
end
|
327
|
+
return self if @ns
|
328
|
+
|
329
|
+
@parent.ancestors.each do |a|
|
330
|
+
next if a == doc
|
331
|
+
@ns = a.namespace_definitions.find { |x| x.prefix == ns.to_s }
|
332
|
+
return self if @ns
|
333
|
+
end
|
334
|
+
|
335
|
+
@ns = { :pending => ns.to_s }
|
336
|
+
return self
|
337
|
+
end
|
338
|
+
|
339
|
+
###
|
340
|
+
# Convert this Builder object to XML
|
341
|
+
def to_xml(*args)
|
342
|
+
if Nokogiri.jruby?
|
343
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
344
|
+
if !options[:save_with]
|
345
|
+
options[:save_with] = Node::SaveOptions::AS_BUILDER
|
346
|
+
end
|
347
|
+
args.insert(0, options)
|
348
|
+
end
|
349
|
+
@doc.to_xml(*args)
|
350
|
+
end
|
351
|
+
|
352
|
+
###
|
353
|
+
# Append the given raw XML +string+ to the document
|
354
|
+
def <<(string)
|
355
|
+
@doc.fragment(string).children.each { |x| insert(x) }
|
356
|
+
end
|
357
|
+
|
358
|
+
def method_missing(method, *args, &block) # :nodoc:
|
359
|
+
if @context && @context.respond_to?(method)
|
360
|
+
@context.send(method, *args, &block)
|
361
|
+
else
|
362
|
+
node = @doc.create_element(method.to_s.sub(/[_!]$/, ""), *args) { |n|
|
363
|
+
# Set up the namespace
|
364
|
+
if @ns.is_a? Nokogiri::XML::Namespace
|
365
|
+
n.namespace = @ns
|
366
|
+
@ns = nil
|
367
|
+
end
|
368
|
+
}
|
369
|
+
|
370
|
+
if @ns.is_a? Hash
|
371
|
+
node.namespace = node.namespace_definitions.find { |x| x.prefix == @ns[:pending] }
|
372
|
+
if node.namespace.nil?
|
373
|
+
raise ArgumentError, "Namespace #{@ns[:pending]} has not been defined"
|
374
|
+
end
|
375
|
+
@ns = nil
|
376
|
+
end
|
377
|
+
|
378
|
+
insert(node, &block)
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
private
|
383
|
+
|
384
|
+
###
|
385
|
+
# Insert +node+ as a child of the current Node
|
386
|
+
def insert(node, &block)
|
387
|
+
node = @parent.add_child(node)
|
388
|
+
if block_given?
|
389
|
+
old_parent = @parent
|
390
|
+
@parent = node
|
391
|
+
@arity ||= block.arity
|
392
|
+
if @arity <= 0
|
393
|
+
instance_eval(&block)
|
394
|
+
else
|
395
|
+
block.call(self)
|
396
|
+
end
|
397
|
+
@parent = old_parent
|
398
|
+
end
|
399
|
+
NodeBuilder.new(node, self)
|
400
|
+
end
|
401
|
+
|
402
|
+
class NodeBuilder # :nodoc:
|
403
|
+
def initialize(node, doc_builder)
|
404
|
+
@node = node
|
405
|
+
@doc_builder = doc_builder
|
406
|
+
end
|
407
|
+
|
408
|
+
def []=(k, v)
|
409
|
+
@node[k] = v
|
410
|
+
end
|
411
|
+
|
412
|
+
def [](k)
|
413
|
+
@node[k]
|
414
|
+
end
|
415
|
+
|
416
|
+
def method_missing(method, *args, &block)
|
417
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
418
|
+
case method.to_s
|
419
|
+
when /^(.*)!$/
|
420
|
+
@node["id"] = $1
|
421
|
+
@node.content = args.first if args.first
|
422
|
+
when /^(.*)=/
|
423
|
+
@node[$1] = args.first
|
424
|
+
else
|
425
|
+
@node["class"] =
|
426
|
+
((@node["class"] || "").split(/\s/) + [method.to_s]).join(" ")
|
427
|
+
@node.content = args.first if args.first
|
428
|
+
end
|
429
|
+
|
430
|
+
# Assign any extra options
|
431
|
+
opts.each do |k, v|
|
432
|
+
@node[k.to_s] = ((@node[k.to_s] || "").split(/\s/) + [v]).join(" ")
|
433
|
+
end
|
434
|
+
|
435
|
+
if block_given?
|
436
|
+
old_parent = @doc_builder.parent
|
437
|
+
@doc_builder.parent = @node
|
438
|
+
value = @doc_builder.instance_eval(&block)
|
439
|
+
@doc_builder.parent = old_parent
|
440
|
+
return value
|
441
|
+
end
|
442
|
+
self
|
443
|
+
end
|
444
|
+
end
|
445
|
+
end
|
446
|
+
end
|
447
|
+
end
|