nokogiri 1.0.0-x86-mswin32-60
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.
- data/History.txt +6 -0
- data/Manifest.txt +120 -0
- data/README.ja.txt +86 -0
- data/README.txt +87 -0
- data/Rakefile +264 -0
- data/ext/nokogiri/extconf.rb +59 -0
- data/ext/nokogiri/html_document.c +83 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_sax_parser.c +32 -0
- data/ext/nokogiri/html_sax_parser.h +11 -0
- data/ext/nokogiri/iconv.dll +0 -0
- data/ext/nokogiri/libexslt.dll +0 -0
- data/ext/nokogiri/libxml2.dll +0 -0
- data/ext/nokogiri/libxslt.dll +0 -0
- data/ext/nokogiri/native.c +40 -0
- data/ext/nokogiri/native.h +51 -0
- data/ext/nokogiri/native.so +0 -0
- data/ext/nokogiri/xml_cdata.c +52 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_document.c +159 -0
- data/ext/nokogiri/xml_document.h +10 -0
- data/ext/nokogiri/xml_dtd.c +117 -0
- data/ext/nokogiri/xml_dtd.h +8 -0
- data/ext/nokogiri/xml_node.c +709 -0
- data/ext/nokogiri/xml_node.h +15 -0
- data/ext/nokogiri/xml_node_set.c +124 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_reader.c +429 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_sax_parser.c +174 -0
- data/ext/nokogiri/xml_sax_parser.h +10 -0
- data/ext/nokogiri/xml_syntax_error.c +194 -0
- data/ext/nokogiri/xml_syntax_error.h +11 -0
- data/ext/nokogiri/xml_text.c +29 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath.c +46 -0
- data/ext/nokogiri/xml_xpath.h +11 -0
- data/ext/nokogiri/xml_xpath_context.c +81 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +108 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/ext/nokogiri/zlib1.dll +0 -0
- data/lib/nokogiri.rb +51 -0
- data/lib/nokogiri/css.rb +6 -0
- data/lib/nokogiri/css/generated_parser.rb +653 -0
- data/lib/nokogiri/css/generated_tokenizer.rb +159 -0
- data/lib/nokogiri/css/node.rb +95 -0
- data/lib/nokogiri/css/parser.rb +24 -0
- data/lib/nokogiri/css/parser.y +198 -0
- data/lib/nokogiri/css/tokenizer.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rex +63 -0
- data/lib/nokogiri/css/xpath_visitor.rb +165 -0
- data/lib/nokogiri/decorators.rb +1 -0
- data/lib/nokogiri/decorators/hpricot.rb +3 -0
- data/lib/nokogiri/decorators/hpricot/node.rb +58 -0
- data/lib/nokogiri/decorators/hpricot/node_set.rb +14 -0
- data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +17 -0
- data/lib/nokogiri/hpricot.rb +47 -0
- data/lib/nokogiri/html.rb +95 -0
- data/lib/nokogiri/html/builder.rb +9 -0
- data/lib/nokogiri/html/document.rb +9 -0
- data/lib/nokogiri/html/sax/parser.rb +21 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/xml.rb +67 -0
- data/lib/nokogiri/xml/after_handler.rb +18 -0
- data/lib/nokogiri/xml/before_handler.rb +32 -0
- data/lib/nokogiri/xml/builder.rb +79 -0
- data/lib/nokogiri/xml/cdata.rb +9 -0
- data/lib/nokogiri/xml/document.rb +30 -0
- data/lib/nokogiri/xml/dtd.rb +6 -0
- data/lib/nokogiri/xml/element.rb +6 -0
- data/lib/nokogiri/xml/entity_declaration.rb +9 -0
- data/lib/nokogiri/xml/node.rb +195 -0
- data/lib/nokogiri/xml/node_set.rb +183 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/reader.rb +14 -0
- data/lib/nokogiri/xml/sax.rb +9 -0
- data/lib/nokogiri/xml/sax/document.rb +59 -0
- data/lib/nokogiri/xml/sax/parser.rb +33 -0
- data/lib/nokogiri/xml/syntax_error.rb +21 -0
- data/lib/nokogiri/xml/text.rb +6 -0
- data/lib/nokogiri/xml/xpath.rb +6 -0
- data/lib/nokogiri/xml/xpath_context.rb +14 -0
- data/lib/nokogiri/xslt.rb +11 -0
- data/lib/nokogiri/xslt/stylesheet.rb +6 -0
- data/nokogiri.gemspec +34 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +224 -0
- data/test/css/test_tokenizer.rb +162 -0
- data/test/css/test_xpath_visitor.rb +54 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/helper.rb +70 -0
- data/test/hpricot/files/basic.xhtml +17 -0
- data/test/hpricot/files/boingboing.html +2266 -0
- data/test/hpricot/files/cy0.html +3653 -0
- data/test/hpricot/files/immob.html +400 -0
- data/test/hpricot/files/pace_application.html +1320 -0
- data/test/hpricot/files/tenderlove.html +16 -0
- data/test/hpricot/files/uswebgen.html +220 -0
- data/test/hpricot/files/utf8.html +1054 -0
- data/test/hpricot/files/week9.html +1723 -0
- data/test/hpricot/files/why.xml +19 -0
- data/test/hpricot/load_files.rb +7 -0
- data/test/hpricot/test_alter.rb +67 -0
- data/test/hpricot/test_builder.rb +27 -0
- data/test/hpricot/test_parser.rb +423 -0
- data/test/hpricot/test_paths.rb +15 -0
- data/test/hpricot/test_preserved.rb +78 -0
- data/test/hpricot/test_xml.rb +30 -0
- data/test/html/sax/test_parser.rb +27 -0
- data/test/html/test_builder.rb +78 -0
- data/test/html/test_document.rb +86 -0
- data/test/test_convert_xpath.rb +180 -0
- data/test/test_nokogiri.rb +36 -0
- data/test/test_reader.rb +222 -0
- data/test/test_xslt_transforms.rb +29 -0
- data/test/xml/sax/test_parser.rb +93 -0
- data/test/xml/test_builder.rb +16 -0
- data/test/xml/test_cdata.rb +18 -0
- data/test/xml/test_document.rb +171 -0
- data/test/xml/test_dtd.rb +43 -0
- data/test/xml/test_node.rb +223 -0
- data/test/xml/test_node_set.rb +116 -0
- data/test/xml/test_text.rb +13 -0
- metadata +217 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
ENV["ARCHFLAGS"] = "-arch #{`uname -p` =~ /powerpc/ ? 'ppc' : 'i386'}"
|
2
|
+
|
3
|
+
require 'mkmf'
|
4
|
+
|
5
|
+
ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
|
6
|
+
|
7
|
+
$CFLAGS << " #{ENV["CFLAGS"]}"
|
8
|
+
if Config::CONFIG['target_os'] == 'mingw32'
|
9
|
+
$CFLAGS << " -DXP_WIN -DXP_WIN32"
|
10
|
+
else
|
11
|
+
$CFLAGS << " -g -DXP_UNIX"
|
12
|
+
end
|
13
|
+
|
14
|
+
$CFLAGS << " -O3 -Wall -Wextra -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline"
|
15
|
+
|
16
|
+
if Config::CONFIG['target_os'] == 'mingw32'
|
17
|
+
find_library('xml2', 'xmlParseDoc',
|
18
|
+
File.join(ROOT, 'cross', 'libxml2-2.7.2.win32', 'bin'))
|
19
|
+
find_library('xslt', 'xsltParseStylesheetDoc',
|
20
|
+
File.join(ROOT, 'cross', 'libxslt-1.1.24.win32', 'bin'))
|
21
|
+
else
|
22
|
+
find_library('xml2', 'xmlParseDoc')
|
23
|
+
find_library('xslt', 'xsltParseStylesheetDoc')
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
if Config::CONFIG['target_os'] == 'mingw32'
|
28
|
+
header = File.join(ROOT, 'cross', 'libxml2-2.7.2.win32', 'include')
|
29
|
+
unless find_header('libxml/xmlversion.h', header)
|
30
|
+
abort "need libxml"
|
31
|
+
end
|
32
|
+
|
33
|
+
header = File.join(ROOT, 'cross', 'libxslt-1.1.24.win32', 'include')
|
34
|
+
unless find_header('libxslt/libxslt.h', header)
|
35
|
+
abort "need libxslt"
|
36
|
+
end
|
37
|
+
|
38
|
+
header = File.join(ROOT, 'cross', 'iconv-1.9.2.win32', 'include')
|
39
|
+
unless find_header('iconv.h', header)
|
40
|
+
abort "need iconv"
|
41
|
+
end
|
42
|
+
else
|
43
|
+
unless find_header('libxml/xmlversion.h', '/usr/include/libxml2')
|
44
|
+
abort "need libxml"
|
45
|
+
end
|
46
|
+
unless find_header('libxslt/xslt.h', '/usr/include')
|
47
|
+
abort "need libxslt"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
unless find_executable("racc")
|
52
|
+
abort "need racc, get the tarball from http://i.loveruby.net/archive/racc/racc-1.4.5-all.tar.gz"
|
53
|
+
end
|
54
|
+
|
55
|
+
unless find_executable("frex")
|
56
|
+
abort "need frex, sudo gem install aaronp-frex -s http://gems.github.com"
|
57
|
+
end
|
58
|
+
|
59
|
+
create_makefile('nokogiri/native')
|
@@ -0,0 +1,83 @@
|
|
1
|
+
#include <html_document.h>
|
2
|
+
|
3
|
+
/*
|
4
|
+
* call-seq:
|
5
|
+
* serialize
|
6
|
+
*
|
7
|
+
* Serialize this document
|
8
|
+
*/
|
9
|
+
static VALUE serialize(VALUE self)
|
10
|
+
{
|
11
|
+
xmlDocPtr doc;
|
12
|
+
xmlChar *buf;
|
13
|
+
int size;
|
14
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
15
|
+
|
16
|
+
htmlDocDumpMemory(doc, &buf, &size);
|
17
|
+
VALUE rb_str = rb_str_new((char *)buf, (long)size);
|
18
|
+
free(buf);
|
19
|
+
return rb_str;
|
20
|
+
}
|
21
|
+
|
22
|
+
/*
|
23
|
+
* call-seq:
|
24
|
+
* read_memory(string, url, encoding, options)
|
25
|
+
*
|
26
|
+
* Read the HTML document contained in +string+ with given +url+, +encoding+,
|
27
|
+
* and +options+. See Nokogiri::HTML.parse
|
28
|
+
*/
|
29
|
+
static VALUE read_memory( VALUE klass,
|
30
|
+
VALUE string,
|
31
|
+
VALUE url,
|
32
|
+
VALUE encoding,
|
33
|
+
VALUE options )
|
34
|
+
{
|
35
|
+
const char * c_buffer = StringValuePtr(string);
|
36
|
+
const char * c_url = (url == Qnil) ? NULL : StringValuePtr(url);
|
37
|
+
const char * c_enc = (encoding == Qnil) ? NULL : StringValuePtr(encoding);
|
38
|
+
int len = NUM2INT(rb_funcall(string, rb_intern("length"), 0));
|
39
|
+
|
40
|
+
htmlDocPtr doc = htmlReadMemory(c_buffer, len, c_url, c_enc, NUM2INT(options));
|
41
|
+
|
42
|
+
if(doc == NULL)
|
43
|
+
doc = htmlNewDoc((const xmlChar *)c_url, NULL);
|
44
|
+
|
45
|
+
return Nokogiri_wrap_xml_document(klass, doc);
|
46
|
+
}
|
47
|
+
|
48
|
+
/*
|
49
|
+
* call-seq:
|
50
|
+
* type
|
51
|
+
*
|
52
|
+
* The type for this document
|
53
|
+
*/
|
54
|
+
static VALUE type(VALUE self)
|
55
|
+
{
|
56
|
+
htmlDocPtr doc;
|
57
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
58
|
+
return INT2NUM((int)doc->type);
|
59
|
+
}
|
60
|
+
|
61
|
+
VALUE cNokogiriHtmlDocument ;
|
62
|
+
void init_html_document()
|
63
|
+
{
|
64
|
+
/*
|
65
|
+
* HACK. This is so that rdoc will work with this C file.
|
66
|
+
*/
|
67
|
+
/*
|
68
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
69
|
+
VALUE html = rb_define_module_under(nokogiri, "HTML");
|
70
|
+
VALUE xml = rb_define_module_under(nokogiri, "XML");
|
71
|
+
VALUE node = rb_define_class_under(xml, "Node", rb_cObject);
|
72
|
+
VALUE xml_doc = rb_define_class_under(xml, "Document", node);
|
73
|
+
VALUE klass = rb_define_class_under(html, "Document", xml_doc);
|
74
|
+
*/
|
75
|
+
|
76
|
+
VALUE klass ;
|
77
|
+
klass = cNokogiriHtmlDocument = rb_const_get(mNokogiriHtml, rb_intern("Document"));
|
78
|
+
|
79
|
+
rb_define_singleton_method(klass, "read_memory", read_memory, 4);
|
80
|
+
|
81
|
+
rb_define_method(klass, "type", type, 0);
|
82
|
+
rb_define_method(klass, "serialize", serialize, 0);
|
83
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#include <html_sax_parser.h>
|
2
|
+
|
3
|
+
static VALUE native_parse_file(VALUE self, VALUE data, VALUE encoding)
|
4
|
+
{
|
5
|
+
xmlSAXHandlerPtr handler;
|
6
|
+
Data_Get_Struct(self, xmlSAXHandler, handler);
|
7
|
+
htmlSAXParseFile( StringValuePtr(data),
|
8
|
+
(const char *)StringValuePtr(encoding),
|
9
|
+
(htmlSAXHandlerPtr)handler,
|
10
|
+
(void *)self );
|
11
|
+
return data;
|
12
|
+
}
|
13
|
+
|
14
|
+
static VALUE native_parse_memory(VALUE self, VALUE data, VALUE encoding)
|
15
|
+
{
|
16
|
+
xmlSAXHandlerPtr handler;
|
17
|
+
Data_Get_Struct(self, xmlSAXHandler, handler);
|
18
|
+
htmlSAXParseDoc( (xmlChar *)StringValuePtr(data),
|
19
|
+
(const char *)StringValuePtr(encoding),
|
20
|
+
(htmlSAXHandlerPtr)handler,
|
21
|
+
(void *)self );
|
22
|
+
return data;
|
23
|
+
}
|
24
|
+
|
25
|
+
VALUE cNokogiriHtmlSaxParser ;
|
26
|
+
void init_html_sax_parser()
|
27
|
+
{
|
28
|
+
VALUE klass = cNokogiriHtmlSaxParser =
|
29
|
+
rb_const_get(mNokogiriHtmlSax, rb_intern("Parser"));
|
30
|
+
rb_define_private_method(klass, "native_parse_memory", native_parse_memory, 2);
|
31
|
+
rb_define_private_method(klass, "native_parse_file", native_parse_file, 2);
|
32
|
+
}
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#include <native.h>
|
2
|
+
|
3
|
+
VALUE mNokogiri ;
|
4
|
+
VALUE mNokogiriXml ;
|
5
|
+
VALUE mNokogiriHtml ;
|
6
|
+
VALUE mNokogiriXslt ;
|
7
|
+
VALUE mNokogiriXmlSax ;
|
8
|
+
VALUE mNokogiriHtmlSax ;
|
9
|
+
|
10
|
+
void Init_native()
|
11
|
+
{
|
12
|
+
mNokogiri = rb_const_get(rb_cObject, rb_intern("Nokogiri"));
|
13
|
+
mNokogiriXml = rb_const_get(mNokogiri, rb_intern("XML"));
|
14
|
+
mNokogiriHtml = rb_const_get(mNokogiri, rb_intern("HTML"));
|
15
|
+
mNokogiriXslt = rb_const_get(mNokogiri, rb_intern("XSLT"));
|
16
|
+
mNokogiriXmlSax = rb_const_get(mNokogiriXml, rb_intern("SAX"));
|
17
|
+
mNokogiriHtmlSax = rb_const_get(mNokogiriHtml, rb_intern("SAX"));
|
18
|
+
|
19
|
+
rb_const_set( mNokogiri,
|
20
|
+
rb_intern("LIBXML_VERSION"),
|
21
|
+
rb_str_new2(LIBXML_DOTTED_VERSION)
|
22
|
+
);
|
23
|
+
|
24
|
+
xmlSetStructuredErrorFunc(NULL, Nokogiri_error_handler);
|
25
|
+
|
26
|
+
init_xml_document();
|
27
|
+
init_html_document();
|
28
|
+
init_xml_node();
|
29
|
+
init_xml_text();
|
30
|
+
init_xml_cdata();
|
31
|
+
init_xml_node_set();
|
32
|
+
init_xml_xpath_context();
|
33
|
+
init_xml_xpath();
|
34
|
+
init_xml_sax_parser();
|
35
|
+
init_xml_reader();
|
36
|
+
init_xml_dtd();
|
37
|
+
init_html_sax_parser();
|
38
|
+
init_xslt_stylesheet();
|
39
|
+
init_xml_syntax_error();
|
40
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#ifndef NOKOGIRI_NATIVE
|
2
|
+
#define NOKOGIRI_NATIVE
|
3
|
+
|
4
|
+
#include <stdlib.h>
|
5
|
+
#include <ruby.h>
|
6
|
+
#include <libxml/parser.h>
|
7
|
+
#include <libxml/xpath.h>
|
8
|
+
#include <libxml/xpathInternals.h>
|
9
|
+
#include <libxml/xmlreader.h>
|
10
|
+
#include <libxml/HTMLparser.h>
|
11
|
+
#include <libxml/HTMLtree.h>
|
12
|
+
|
13
|
+
#include <xml_document.h>
|
14
|
+
#include <html_document.h>
|
15
|
+
#include <xml_node.h>
|
16
|
+
#include <xml_text.h>
|
17
|
+
#include <xml_cdata.h>
|
18
|
+
#include <xml_node_set.h>
|
19
|
+
#include <xml_xpath.h>
|
20
|
+
#include <xml_dtd.h>
|
21
|
+
#include <xml_xpath_context.h>
|
22
|
+
#include <xml_sax_parser.h>
|
23
|
+
#include <xml_reader.h>
|
24
|
+
#include <html_sax_parser.h>
|
25
|
+
#include <xslt_stylesheet.h>
|
26
|
+
#include <xml_syntax_error.h>
|
27
|
+
|
28
|
+
extern VALUE mNokogiri ;
|
29
|
+
extern VALUE mNokogiriXml ;
|
30
|
+
extern VALUE mNokogiriXmlSax ;
|
31
|
+
extern VALUE mNokogiriHtml ;
|
32
|
+
extern VALUE mNokogiriHtmlSax ;
|
33
|
+
extern VALUE mNokogiriXslt ;
|
34
|
+
|
35
|
+
#ifdef DEBUG
|
36
|
+
|
37
|
+
#define NOKOGIRI_DEBUG_START_NODE(p) if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"\nnokogiri: %s:%d %p start node (%p %x %p/%s)\n", __FILE__, __LINE__, p, p->_private, p->type, p->name, p->name);
|
38
|
+
#define NOKOGIRI_DEBUG_START_TEXT(p) if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"\nnokogiri: %s:%d %p start node (%p %x %p/%s) '%s'\n", __FILE__, __LINE__, p, p->_private, p->type, p->name, p->name, p->content);
|
39
|
+
#define NOKOGIRI_DEBUG_START(p) if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"nokogiri: %s:%d %p start\n", __FILE__, __LINE__, p);
|
40
|
+
#define NOKOGIRI_DEBUG_END(p) if (getenv("NOKOGIRI_DEBUG")) fprintf(stderr,"nokogiri: %s:%d %p end\n", __FILE__, __LINE__, p);
|
41
|
+
|
42
|
+
#else
|
43
|
+
|
44
|
+
#define NOKOGIRI_DEBUG_START_NODE(p)
|
45
|
+
#define NOKOGIRI_DEBUG_START_TEXT(p)
|
46
|
+
#define NOKOGIRI_DEBUG_START(p)
|
47
|
+
#define NOKOGIRI_DEBUG_END(p)
|
48
|
+
|
49
|
+
#endif
|
50
|
+
|
51
|
+
#endif
|
Binary file
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#include <xml_cdata.h>
|
2
|
+
|
3
|
+
static void dealloc(xmlNodePtr node)
|
4
|
+
{
|
5
|
+
if (node->doc == NULL) {
|
6
|
+
NOKOGIRI_DEBUG_START_NODE(node);
|
7
|
+
xmlFreeNode(node);
|
8
|
+
NOKOGIRI_DEBUG_END(node);
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
/*
|
13
|
+
* call-seq:
|
14
|
+
* new(document, content)
|
15
|
+
*
|
16
|
+
* Create a new CData element on the +document+ with +content+
|
17
|
+
*/
|
18
|
+
static VALUE new(VALUE klass, VALUE doc, VALUE content)
|
19
|
+
{
|
20
|
+
xmlDocPtr xml_doc;
|
21
|
+
Data_Get_Struct(doc, xmlDoc, xml_doc);
|
22
|
+
|
23
|
+
xmlNodePtr node = xmlNewCDataBlock(
|
24
|
+
xml_doc,
|
25
|
+
(const xmlChar *)StringValuePtr(content),
|
26
|
+
NUM2INT(rb_funcall(content, rb_intern("length"), 0))
|
27
|
+
);
|
28
|
+
|
29
|
+
VALUE rb_node = Data_Wrap_Struct(klass, NULL, dealloc, node);
|
30
|
+
node->_private = (void *)rb_node;
|
31
|
+
|
32
|
+
if(rb_block_given_p()) rb_yield(rb_node);
|
33
|
+
|
34
|
+
return rb_node;
|
35
|
+
}
|
36
|
+
|
37
|
+
VALUE cNokogiriXmlCData;
|
38
|
+
void init_xml_cdata()
|
39
|
+
{
|
40
|
+
VALUE nokogiri = rb_define_module("Nokogiri");
|
41
|
+
VALUE xml = rb_define_module_under(nokogiri, "XML");
|
42
|
+
|
43
|
+
/*
|
44
|
+
* CData represents a CData node in an xml document.
|
45
|
+
*/
|
46
|
+
VALUE klass = rb_define_class_under(xml, "CData", cNokogiriXmlNode);
|
47
|
+
|
48
|
+
|
49
|
+
cNokogiriXmlCData = klass;
|
50
|
+
|
51
|
+
rb_define_singleton_method(klass, "new", new, 2);
|
52
|
+
}
|
@@ -0,0 +1,159 @@
|
|
1
|
+
#include <xml_document.h>
|
2
|
+
|
3
|
+
/*
|
4
|
+
* note that xmlDocPtr is being cast as an xmlNodePtr, which is legal for the
|
5
|
+
* "common part" struct header which contains only node pointers.
|
6
|
+
*/
|
7
|
+
static void gc_mark(xmlNodePtr node)
|
8
|
+
{
|
9
|
+
xmlNodePtr child ;
|
10
|
+
/* mark children nodes */
|
11
|
+
for (child = node->children ; child ; child = child->next) {
|
12
|
+
if (child->_private)
|
13
|
+
rb_gc_mark((VALUE)child->_private);
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
static void dealloc(xmlDocPtr doc)
|
18
|
+
{
|
19
|
+
NOKOGIRI_DEBUG_START(doc);
|
20
|
+
xmlFreeDoc(doc);
|
21
|
+
NOKOGIRI_DEBUG_END(doc);
|
22
|
+
}
|
23
|
+
|
24
|
+
/*
|
25
|
+
* call-seq:
|
26
|
+
* serialize
|
27
|
+
*
|
28
|
+
* Serialize this document
|
29
|
+
*/
|
30
|
+
static VALUE serialize(VALUE self)
|
31
|
+
{
|
32
|
+
xmlDocPtr doc;
|
33
|
+
xmlChar *buf;
|
34
|
+
int size;
|
35
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
36
|
+
|
37
|
+
xmlDocDumpMemory(doc, &buf, &size);
|
38
|
+
VALUE rb_str = rb_str_new((char *)buf, (long)size);
|
39
|
+
free(buf);
|
40
|
+
return rb_str;
|
41
|
+
}
|
42
|
+
|
43
|
+
/*
|
44
|
+
* call-seq:
|
45
|
+
* root=
|
46
|
+
*
|
47
|
+
* Set the root element on this document
|
48
|
+
*/
|
49
|
+
static VALUE set_root(VALUE self, VALUE root)
|
50
|
+
{
|
51
|
+
xmlDocPtr doc;
|
52
|
+
xmlNodePtr new_root;
|
53
|
+
|
54
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
55
|
+
Data_Get_Struct(root, xmlNode, new_root);
|
56
|
+
|
57
|
+
xmlDocSetRootElement(doc, new_root);
|
58
|
+
Nokogiri_xml_node_owned_set(new_root);
|
59
|
+
return root;
|
60
|
+
}
|
61
|
+
|
62
|
+
/*
|
63
|
+
* call-seq:
|
64
|
+
* root
|
65
|
+
*
|
66
|
+
* Get the root node for this document.
|
67
|
+
*/
|
68
|
+
static VALUE root(VALUE self)
|
69
|
+
{
|
70
|
+
xmlDocPtr doc;
|
71
|
+
Data_Get_Struct(self, xmlDoc, doc);
|
72
|
+
|
73
|
+
xmlNodePtr root = xmlDocGetRootElement(doc);
|
74
|
+
|
75
|
+
if(!root) return Qnil;
|
76
|
+
return Nokogiri_wrap_xml_node(root) ;
|
77
|
+
}
|
78
|
+
|
79
|
+
static VALUE read_memory( VALUE klass,
|
80
|
+
VALUE string,
|
81
|
+
VALUE url,
|
82
|
+
VALUE encoding,
|
83
|
+
VALUE options )
|
84
|
+
{
|
85
|
+
const char * c_buffer = StringValuePtr(string);
|
86
|
+
const char * c_url = (url == Qnil) ? NULL : StringValuePtr(url);
|
87
|
+
const char * c_enc = (encoding == Qnil) ? NULL : StringValuePtr(encoding);
|
88
|
+
int len = NUM2INT(rb_funcall(string, rb_intern("length"), 0));
|
89
|
+
|
90
|
+
xmlInitParser();
|
91
|
+
xmlDocPtr doc = xmlReadMemory(c_buffer, len, c_url, c_enc, NUM2INT(options));
|
92
|
+
|
93
|
+
if(doc == NULL) {
|
94
|
+
xmlFreeDoc(doc);
|
95
|
+
rb_raise(rb_eRuntimeError, "Couldn't create a document");
|
96
|
+
}
|
97
|
+
|
98
|
+
return Nokogiri_wrap_xml_document(klass, doc);
|
99
|
+
}
|
100
|
+
|
101
|
+
static VALUE new(int argc, VALUE *argv, VALUE klass)
|
102
|
+
{
|
103
|
+
VALUE version;
|
104
|
+
if(rb_scan_args(argc, argv, "01", &version) == 0)
|
105
|
+
version = rb_str_new2("1.0");
|
106
|
+
|
107
|
+
xmlDocPtr doc = xmlNewDoc((xmlChar *)StringValuePtr(version));
|
108
|
+
return Nokogiri_wrap_xml_document(klass, doc);
|
109
|
+
}
|
110
|
+
|
111
|
+
/*
|
112
|
+
* call-seq:
|
113
|
+
* substitute_entities_set bool)
|
114
|
+
*
|
115
|
+
* Set the global XML default for substitute entities.
|
116
|
+
*/
|
117
|
+
static VALUE substitute_entities_set(VALUE klass, VALUE value)
|
118
|
+
{
|
119
|
+
xmlSubstituteEntitiesDefault(NUM2INT(value));
|
120
|
+
return Qnil ;
|
121
|
+
}
|
122
|
+
|
123
|
+
/*
|
124
|
+
* call-seq:
|
125
|
+
* substitute_entities_set bool)
|
126
|
+
*
|
127
|
+
* Set the global XML default for load external subsets.
|
128
|
+
*/
|
129
|
+
static VALUE load_external_subsets_set(VALUE klass, VALUE value)
|
130
|
+
{
|
131
|
+
xmlLoadExtDtdDefaultValue = NUM2INT(value);
|
132
|
+
return Qnil ;
|
133
|
+
}
|
134
|
+
|
135
|
+
VALUE cNokogiriXmlDocument ;
|
136
|
+
void init_xml_document()
|
137
|
+
{
|
138
|
+
VALUE klass = cNokogiriXmlDocument = rb_const_get(mNokogiriXml, rb_intern("Document"));
|
139
|
+
|
140
|
+
rb_define_singleton_method(klass, "read_memory", read_memory, 4);
|
141
|
+
rb_define_singleton_method(klass, "new", new, -1);
|
142
|
+
rb_define_singleton_method(klass, "substitute_entities=", substitute_entities_set, 1);
|
143
|
+
rb_define_singleton_method(klass, "load_external_subsets=", load_external_subsets_set, 1);
|
144
|
+
|
145
|
+
rb_define_method(klass, "root", root, 0);
|
146
|
+
rb_define_method(klass, "root=", set_root, 1);
|
147
|
+
rb_define_method(klass, "serialize", serialize, 0);
|
148
|
+
}
|
149
|
+
|
150
|
+
|
151
|
+
/* this takes klass as a param because it's used for HtmlDocument, too. */
|
152
|
+
VALUE Nokogiri_wrap_xml_document(VALUE klass, xmlDocPtr doc)
|
153
|
+
{
|
154
|
+
if (doc->_private)
|
155
|
+
return (VALUE)doc->_private ;
|
156
|
+
VALUE rb_doc = Data_Wrap_Struct(klass ? klass : cNokogiriXmlDocument, gc_mark, dealloc, doc) ;
|
157
|
+
doc->_private = (void*)rb_doc ;
|
158
|
+
return rb_doc ;
|
159
|
+
}
|