SGML::P...P::Tools(3)

🌸 NAME 🌸

Okay so bestie, this is the manual page for SGML::Parser::OpenSP::Tools β€” straight out of the User Contributed Perl Documentation, section 3. It’s the utility squad for OpenSP output, and honestly? It’s giving β€œsmall module, big helper energy.” πŸ’…


✨ DESCRIPTION ✨

These are the routines that post-process OpenSP event data. Lowkey the cleanup crew for parser events, and they ate. 🫢


πŸ› οΈ UTILITY FUNCTIONS πŸ› οΈ

Here’s the function lineup, in the exact order the page serves it. No skipping, no cap.

πŸ’– specified_attribute($attribute) πŸ’–

specified_attribute returns a true value if the attribute is of type "cdata" or "tokenized" and has its Defaulted property set to "specified". For example

sub start_element
{
  my $self = shift;
  my $elem = shift;
  my @spec = grep specified_attribute($_),
                  values %{$elem->{Attributes}};

  # @spec contains all explicitly specified attributes
}

πŸ’œ defaulted_attribute($attribute) πŸ’œ

defaulted_attribute returns a true value if the attribute is of type "cdata" or "tokenized" and has its Defaulted property set to something other than "specified". For all attributes, the following always holds true,

!defined(attribute_value($_)) or
defaulted_attribute($_) or
specified_attribute($_)

since only defaulted and specified attributes can have a value.

πŸ¦‹ value_attribute($attribute) πŸ¦‹

Returns true if the value can have a value, i.e., it is either specified or defaulted.

πŸŽ€ attribute_value($attribute) πŸŽ€

attribute_value returns a textual representation of the value of an attribute as reported to a "start_element" handler or undef if no value is available.

πŸ”₯ split_message($message, $filename, $open_ent, $error_num, $open_elem) πŸ”₯

split_message splits an OpenSP error message into its components: the error or warning message, an optional auxiliary message that provides additional information about the error β€” like the first occurrence of an ID when there are duplicate IDs in a document β€” each accompanied by line and column numbers relevant to the message. Depending on the parser configuration, it also gives you the open entities for the message, the error number of the message, and a list of the current open elements.

It returns a hash reference like

# this is always present
primary_message =>
{
  Number       => 141,       # only if $p->show_error_numbers(1)
  Module       => 554521624, # only if $p->show_error_numbers(1)
  ColumnNumber => 9,
  LineNumber   => 12,
  Severity     => 'E',
  Text         => 'ID "a" already defined'
},

# only some messages have an aux_message
aux_message =>
{
  ColumnNumber => 9,
  LineNumber   => 11,
  Text         => 'ID "a" first defined here'
},

# iff $p->show_open_elements(1) and there are open elements
open_elements => 'html body[1] (p[1])',

# iff $p->show_open_entities(1) and there are open entities
# other than the document, but the document will be reported
# if the error is in some other entity
open_entities => [
{
  ColumnNumber => 55,
  FileName     => 'example.xhtml',
  EntityName   => 'html',
  LineNumber   => 2
}, ... ],

This would typically be used like

sub error
{
  my $self = shift;
  my $erro = shift;
  my $mess = $erro->{Message};

  # parser is the SGML::Parser::OpenSP
  # object stored in the handler object
  my $loca = $self->{parser}->get_location;
  my $name = $loca->{FileName};

  my $splt = split_message($mess, $name,
                           $self->{parser}->show_open_entities,
                           $self->{parser}->show_error_numbers,
                           $self->{parser}->show_open_elements);

  # ...
}

A more convenient way to access this function is provided by the "SGML::Parser::OpenSP" module, which you can use like

sub error
{
  my $self = shift;
  my $erro = shift;

  my $mess = $self->{parser}->split_message($erro);

  # relevant data is now $mess and $erro->{Severity}
  # of which the latter provides more detailed information
  # than $mess->{primary_message}->{Severity}, see the
  # SGML::Parser::OpenSP documentation for details
}

🧚 split_pi($data) 🧚

split_pi splits the data of a processing instruction at the first white space character into two components, where white space character is defined in the $WHITESPACE package variable, qr/[\x20\x09\x0d\x0a]/ by default. It returns undef if there is no data to split.

sub pi
{
  my $self = shift;
  my $proc = shift;

  my ($target, $data) = split_pi($proc->{Data});

  # ...
}

Okay so the legal/source tea, no cap:

Copyright (c) 2006-2008 Bjoern Hoehrmann <bjoern@hoehrmann.de>.

This module is licensed under the same terms as Perl itself.


Bestie, that’s the whole manual page β€” footer tea time: perl v5.42.0, dated 2023-08-09, and you’re all caught up. Periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from