Class: LibXML::XML::HTMLParser::Context

Inherits:
Parser::Context show all
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

Instance Method Summary collapse

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.


152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 152

static VALUE rxml_html_parser_context_file(VALUE klass, VALUE file)
{
  htmlParserCtxtPtr ctxt = htmlCreateFileParserCtxt(StringValuePtr(file), NULL);
  if (!ctxt)
    rxml_raise(&xmlLastError);

  /* 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, rxml_libxml_default_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.


176
177
178
179
180
181
182
183
184
185
186
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
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 176

static VALUE rxml_html_parser_context_io(VALUE klass, VALUE io)
{
  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(&xmlLastError);
  }

  /* 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, rxml_libxml_default_options());

  stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);

  if (!stream)
  {
    xmlFreeParserInputBuffer(input);
    xmlFreeParserCtxt(ctxt);
    rxml_raise(&xmlLastError);
  }
  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.


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 228

static VALUE rxml_html_parser_context_string(VALUE klass, VALUE string)
{
  htmlParserCtxtPtr ctxt;
  Check_Type(string, T_STRING);

  if (RSTRING_LEN(string) == 0)
    rb_raise(rb_eArgError, "Must specify a string with one or more characters");

  ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
                                   RSTRING_LEN(string));
  if (!ctxt)
    rxml_raise(&xmlLastError);

  /* 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, rxml_libxml_default_options());

  htmlDefaultSAXHandlerInit();
  if (ctxt->sax != NULL)
    memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
  
  return rxml_html_parser_context_wrap(ctxt);
}

Instance Method Details

#disable_cdata=(bool) ⇒ Object

Control whether the CDATA nodes will be created in this context.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 260

static VALUE rxml_html_parser_context_disable_cdata_set(VALUE self, VALUE bool)
{
  htmlParserCtxtPtr ctxt;
  Data_Get_Struct(self, htmlParserCtxt, 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 (bool)
    ctxt->sax->cdataBlock = NULL;
  else
    ctxt->sax->cdataBlock = htmlDefaultSAXHandler.cdataBlock;

  return bool;
}

#options=(XML) ⇒ Object #XML::Parser::Options::NOCDATAObject

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 (|).



286
287
288
289
290
291
292
293
294
295
296
# File 'ext/libxml/ruby_xml_html_parser_context.c', line 286

static VALUE rxml_html_parser_context_options_set(VALUE self, VALUE options)
{
  int result;
  htmlParserCtxtPtr ctxt;
  Check_Type(options, T_FIXNUM);

  Data_Get_Struct(self, htmlParserCtxt, ctxt);
  result = htmlCtxtUseOptions(ctxt, NUM2INT(options));

  return self;
}