Class: LibXML::XML::HTMLParser::Context
- Inherits:
-
Parser::Context
- Object
- Parser::Context
- LibXML::XML::HTMLParser::Context
- Defined in:
- ext/libxml/ruby_xml_html_parser_context.c,
ext/libxml/ruby_xml_html_parser_context.c
Overview
The XML::HTMLParser::Context class provides in-depth control over how a document is parsed.
Class Method Summary collapse
-
.XML::HTMLParser::Context.file(file) ⇒ XML::HTMLParser::Context
Creates a new parser context based on the specified file or uri.
-
.XML::HTMLParser::Context.io(io) ⇒ XML::HTMLParser::Context
Creates a new parser context based on the specified io object.
-
.XML::HTMLParser::Context.string(string) ⇒ XML::HTMLParser::Context
Creates a new parser context based on the specified string.
Instance Method Summary collapse
-
#close ⇒ nil
Closes the underlying input streams.
-
#disable_cdata=(value) ⇒ Object
Control whether the CDATA nodes will be created in this context.
-
#options=(options) ⇒ Object
Provides control over the execution of a parser.
Methods inherited from Parser::Context
#base_uri, #base_uri=, #data_directory, #depth, #disable_cdata?, #disable_sax?, #docbook?, document, #encoding, #encoding=, #errno, #html?, #io_max_num_streams, #io_num_streams, #keep_blanks?, #name_depth, #name_depth_max, #name_node, #name_tab, #node, #node_depth, #node_depth_max, #num_chars, #options, #recovery=, #recovery?, #replace_entities=, #replace_entities?, #space_depth, #space_depth_max, #standalone?, #stats?, #subset_external?, #subset_external_system_id, #subset_external_uri, #subset_internal?, #subset_internal_name, #valid, #validate?, #version, #well_formed?
Class Method Details
.XML::HTMLParser::Context.file(file) ⇒ XML::HTMLParser::Context
Creates a new parser context based on the specified file or uri.
Parameters:
file - A filename or uri
options - A or'ed together list of LibXML::XML::HTMLParser::Options values
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 159
static VALUE rxml_html_parser_context_file(int argc, VALUE* argv, VALUE klass)
{
VALUE file, options;
rb_scan_args(argc, argv, "11", &file, &options);
htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
if (!ctxt)
rxml_raise(xmlGetLastError());
/* This is annoying, but xmlInitParserCtxt (called indirectly above) and
xmlCtxtUseOptionsInternal (called below) initialize slightly different
context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
return rxml_html_parser_context_wrap(ctxt);
}
|
.XML::HTMLParser::Context.io(io) ⇒ XML::HTMLParser::Context
Creates a new parser context based on the specified io object.
Parameters:
io - A ruby IO object
options - A or'ed together list of LibXML::XML::HTMLParser::Options values
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 187
static VALUE rxml_html_parser_context_io(int argc, VALUE* argv, VALUE klass)
{
VALUE io, options;
rb_scan_args(argc, argv, "11", &io, &options);
VALUE result;
htmlParserCtxtPtr ctxt;
xmlParserInputBufferPtr input;
xmlParserInputPtr stream;
if (NIL_P(io))
rb_raise(rb_eTypeError, "Must pass in an IO object");
input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL,
(void*)io, XML_CHAR_ENCODING_NONE);
ctxt = htmlNewParserCtxt();
if (!ctxt)
{
xmlFreeParserInputBuffer(input);
rxml_raise(xmlGetLastError());
}
/* This is annoying, but xmlInitParserCtxt (called indirectly above) and
xmlCtxtUseOptionsInternal (called below) initialize slightly different
context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
if (!stream)
{
xmlFreeParserInputBuffer(input);
xmlFreeParserCtxt(ctxt);
rxml_raise(xmlGetLastError());
}
inputPush(ctxt, stream);
result = rxml_html_parser_context_wrap(ctxt);
/* Attach io object to parser so it won't get freed.*/
rb_ivar_set(result, IO_ATTR, io);
return result;
}
|
.XML::HTMLParser::Context.string(string) ⇒ XML::HTMLParser::Context
Creates a new parser context based on the specified string.
Parameters:
string - A string that contains the data to parse
options - A or'ed together list of LibXML::XML::HTMLParser::Options values
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 243
static VALUE rxml_html_parser_context_string(int argc, VALUE* argv, VALUE klass)
{
VALUE string, options;
rb_scan_args(argc, argv, "11", &string, &options);
Check_Type(string, T_STRING);
if (RSTRING_LEN(string) == 0)
rb_raise(rb_eArgError, "Must specify a string with one or more characters");
htmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
(int)RSTRING_LEN(string));
if (!ctxt)
rxml_raise(xmlGetLastError());
/* This is annoying, but xmlInitParserCtxt (called indirectly above) and
xmlCtxtUseOptionsInternal (called below) initialize slightly different
context options, in particular XML_PARSE_NODICT which xmlInitParserCtxt
sets to 0 and xmlCtxtUseOptionsInternal sets to 1. So we have to call both. */
htmlCtxtUseOptions(ctxt, options == Qnil ? 0 : NUM2INT(options));
// Setup sax handler
// TODO - there must be a better way? The sax handler is initialized for XML, but we want
// to use HTML
memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax);
return rxml_html_parser_context_wrap(ctxt);
}
|
Instance Method Details
#close ⇒ nil
Closes the underlying input streams. This is useful when parsing a large amount of files and you want to close the files without relying on Ruby’s garbage collector to run.
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 281
static VALUE rxml_html_parser_context_close(VALUE self)
{
htmlParserCtxtPtr ctxt;
xmlParserInputPtr xinput;
TypedData_Get_Struct(self, htmlParserCtxt, &rxml_html_parser_context_type, ctxt);
while ((xinput = inputPop(ctxt)) != NULL)
{
xmlFreeInputStream(xinput);
}
return Qnil;
}
|
#disable_cdata=(value) ⇒ Object
Control whether the CDATA nodes will be created in this context.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 300
static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE value)
{
htmlParserCtxtPtr ctxt;
TypedData_Get_Struct(self, htmlParserCtxt, &rxml_html_parser_context_type, ctxt);
if (ctxt->sax == NULL)
rb_raise(rb_eRuntimeError, "Sax handler is not yet set");
/* LibXML controls this internally with the default SAX handler. */
if (value)
ctxt->sax->cdataBlock = NULL;
else
ctxt->sax->cdataBlock = xmlSAX2CDataBlock;
return value;
}
|
#options=(XML) ⇒ Object #XML::Parser::Options::NOCDATA ⇒ Object
Provides control over the execution of a parser. Valid values are the constants defined on XML::Parser::Options. Multiple options can be combined by using Bitwise OR (|).
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 326
static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
{
int xml_options = NUM2INT(options);
htmlParserCtxtPtr ctxt;
TypedData_Get_Struct(self, htmlParserCtxt, &rxml_html_parser_context_type, ctxt);
htmlCtxtUseOptions(ctxt, xml_options);
#if LIBXML_VERSION >= 20707
/* Big hack here, but htmlCtxtUseOptions doens't support HTML_PARSE_NOIMPLIED.
So do it ourselves. There must be a better way??? */
if (xml_options & HTML_PARSE_NOIMPLIED)
{
ctxt->options |= HTML_PARSE_NOIMPLIED;
}
#endif
return self;
}
|