SGMLS(3pm)

πŸ“¦
version 1.03ii Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

bestie, welcome to SGMLS, which is the Perl class (3pm β€” yes she lives in the Perl module pages, not the command pages, iykyk) that handles postprocessing the output from the onsgmls, sgmls, and nsgmls parsers.

like, those three parsers do the parsing thing, and then SGMLS picks up the output and lets you vibe with it in Perl. πŸ’…


🌸 SYNOPSIS 🌸

okay so this is literally the whole flow. you make a parse object, you keep asking it for next_event, and you get events back until you don't. no cap. πŸ¦‹

  use SGMLS;

  my $parse = new SGMLS(STDIN);

  my $event = $parse->next_event;
  while ($event) {

    SWITCH: {

      ($event->type eq 'start_element') && do {
        my $element = $event->data;    # An object of class SGMLS_Element
        [[your code for the beginning of an element]]
        last SWITCH;
      };

      ($event->type eq 'end_element') && do {
        my $element = $event->data;    # An object of class SGMLS_Element
        [[your code for the end of an element]]
        last SWITCH;
      };

      ($event->type eq 'cdata') && do {
        my $cdata = $event->data;      # A string
        [[your code for character data]]
        last SWITCH;
      };

      ($event->type eq 'sdata') && do {
        my $sdata = $event->data;      # A string
        [[your code for system data]]
        last SWITCH;
      };

      ($event->type eq 're') && do {
        [[your code for a record end]]
        last SWITCH;
      };

      ($event->type eq 'pi') && do {
        my $pi = $event->data;         # A string
        [[your code for a processing instruction]]
        last SWITCH;
      };

      ($event->type eq 'entity') && do {
        my $entity = $event->data;     # An object of class SGMLS_Entity
        [[your code for an external entity]]
        last SWITCH;
      };

      ($event->type eq 'start_subdoc') && do {
        my $entity = $event->data;     # An object of class SGMLS_Entity
        [[your code for the beginning of a subdoc entity]]
        last SWITCH;
      };

      ($event->type eq 'end_subdoc') && do {
        my $entity = $event->data;     # An object of class SGMLS_Entity
        [[your code for the end of a subdoc entity]]
        last SWITCH;
      };

      ($event->type eq 'conforming') && do {
        [[your code for a conforming document]]
        last SWITCH;
      };

      die "Internal error: unknown event type " . $event->type . "\n";
    }

    $event = $parse->next_event;
  }

so the tea on that loop: you build your parse object with new SGMLS(STDIN), then you call $parse->next_event over and over. each event has a ->type you switch on, and a ->data you reach for β€” and what ->data is totally depends on the type. sometimes it's an object (the element ones, the entity ones β€” those come back as SGMLS_Element and SGMLS_Entity respectively), sometimes it's just a string.

and notice the <red>die "Internal error: unknown event type " . $event->type . "\n";</red> sitting there as the catch-all β€” if an event type shows up that isn't one of the ones listed, that's the moment you find out. πŸ‘‘

the SWITCH: block plus last SWITCH is just Perl's classic fall-through trick, so each branch bounces out cleanly instead of falling into the next one. iconic, honestly. 🫢

πŸ’– DESCRIPTION πŸ’–

Okay bestie, so the SGMLS package is literally a whole squad of related classes β€” no cap! You got SGMLS, SGMLS_Event, SGMLS_Element, SGMLS_Attribute, SGMLS_Notation, and SGMLS_Entity. And the way they all just show up the moment you type:

use SGMLS;

It's giving iconic. Usually, the only object you'll ever create yourself is the SGMLS one, and the rest just manifest automatically over the course of the parse. Lowkey magical. If you want the full lore, there's much fuller documentation chilling in the .sgml files in the DOC/ directory of the SGMLS.pm distribution. ✨


πŸŽ€ The SGMLS class πŸŽ€

This class holds a single parse. When you make an instance of it, you pass a file handle as an argument (if you're reading the output of onsgmls, sgmls or nsgmls from a pipe, that file handle is usually STDIN):

my $parse = new SGMLS(STDIN);

The most important method here is next_event, which reads and returns the next major event from the input stream. And ugh, it's literally so smart β€” the SGMLS class deals with most ESIS events itself. Attributes and entity definitions? Collected, stored, handled automatically, and completely invisible to you. It's the main character so you don't have to be. πŸ’…

Here's every method the SGMLS class has:

  • next_event() β€” Return an SGMLS_Event object containing the next major event from the SGML parse.
  • element() β€” Return an SGMLS_Element object containing the current element in the document.
  • file() β€” Return a string containing the name of the current SGML source file (this will work only if the -l option was given to onsgmls, sgmls or nsgmls).
  • line() β€” Return a string containing the current line number from the source file (this will work only if the -l option was given to onsgmls, sgmls or nsgmls).
  • appinfo() β€” Return a string containing the APPINFO parameter (if any) from the SGML declaration.
  • notation(NNAME) β€” Return an SGMLS_Notation object representing the notation named NNAME. With newer versions of nsgmls, all notations are available; otherwise, only the notations which are actually used will be available.
  • entity(ENAME) β€” Return an SGMLS_Entity object representing the entity named ENAME. With newer versions of nsgmls, all entities are available; otherwise, only external data entities and internal entities used as attribute values will be available.
  • ext() β€” Return a reference to an associative array for user-defined extensions.

🌸 The SGMLS_Event class 🌸

This class holds a single major event, generated by the next_event method in the SGMLS class. It uses these methods:

  • type() β€” Return a string describing the type of event: start_element, end_element, cdata, sdata, re, pi, entity, start_subdoc, end_subdoc, and conforming. See SYNOPSIS, above, for the values associated with each of these.
  • data() β€” Return the data associated with the current event (if any). For start_element and end_element, returns an SGMLS_ELement object; for entity, start_subdoc, and end_subdoc, returns an SGMLS_Entity object; for cdata, sdata, and pi, returns a string; and for re and conforming, returns the empty string. See SYNOPSIS, above, for an example of this method's use.
  • key() β€” Return a string key to the event, such as an element or entity name (otherwise, the same as data()).
  • file() β€” Return the current file name, as in the SGMLS class.
  • line() β€” Return the current line number, as in the SGMLS class.
  • element() β€” Return the current element, as in the SGMLS class.
  • parse() β€” Return the SGMLS object which generated the event.
  • entity(ENAME) β€” Look up an entity, as in the SGMLS class.
  • notation(ENAME) β€” Look up a notation, as in the SGMLS class.
  • ext() β€” Return a reference to an associative array for user-defined extensions.

πŸ¦‹ The SGMLS_Element class πŸ¦‹

This class is used for elements, and it holds all the associated info (like the element's attributes, bestie). It recognises the following methods:

  • name() β€” Return a string containing the name, or Generic Identifier, of the element, in upper case.
  • parent() β€” Return the SGMLS_Element object for the element's parent (if any).
  • parse() β€” Return the SGMLS object for the current parse.
  • attributes() β€” Return a reference to an associative array of attribute names and SGMLS_Attribute structures. Attribute names will be all in upper case.
  • attribute_names() β€” Return an array of strings containing the names of all attributes defined for the current element, in upper case.
  • attribute(ANAME) β€” Return the SGMLS_Attribute structure for the attribute ANAME.
  • set_attribute(ATTRIB) β€” Add the SGMLS_Attribute object ATTRIB to the current element, replacing any other attribute structure with the same name.
  • in(GI) β€” Return true (ie. 1) if the string GI is the name of the current element's parent, or false (ie. 0) if it is not.
  • within(GI) β€” Return true (ie. 1) if the string GI is the name of any of the ancestors of the current element, or false (ie. 0) if it is not.
  • ext() β€” Return a reference to an associative array for user-defined extensions.

🌈 The SGMLS_Attribute class 🌈

Every single attribute for every SGMLS_Element is a whole object of this class. It recognises these methods:

  • name() β€” Return a string containing the name of the current attribute, all in upper case.
  • type() β€” Return a string containing the type of the current attribute, all in upper case. Available types are IMPLIED, CDATA, NOTATION, ENTITY, and TOKEN.
  • value() β€” Return the value of the current attribute, if any. This will be an empty string if the type is IMPLIED, a string of some sort if the type is CDATA or TOKEN (if it is TOKEN, you may want to split the string into a series of separate tokens), an SGMLS_Notation object if the type is NOTATION, or an SGMLS_Entity object if the type is ENTITY.
Not me dropping the biggest warning ever: if the value is CDATA, it will not have escape sequences for 8-bit characters, record ends, or SDATA processed β€” that will be your responsibility. You have been warned, bestie! 😭
  • is_implied() β€” Return true (ie. 1) if the value of the attribute is implied, or false (ie. 0) if it is specified in the document.
  • set_type(TYPE) β€” Change the type of the attribute to the string TYPE (which should be all in upper case). Available types are IMPLIED, CDATA, NOTATION, ENTITY, and TOKEN.
  • set_value(VALUE) β€” Change the value of the attribute to VALUE, which may be a string, an SGMLS_Entity object, or an SGMLS_Notation subject, depending on the attribute's type.
  • ext() β€” Return a reference to an associative array available for user-defined extensions.

🧚 The SGMLS_Notation class 🧚

All declared notations appear as objects belonging to this class, which recognises these methods:

  • name() β€” Return a string containing the name of the notation.
  • sysid() β€” Return a string containing the system identifier of the notation, if any.
  • pubid() β€” Return a string containing the public identifier of the notation, if any.
  • ext() β€” Return a reference to an associative array available for user-defined extensions.

πŸ”₯ The SGMLS_Entity class πŸ”₯

All declared entities appear as objects belonging to this class, which recognises these methods:

  • name() β€” Return a string containing the name of the entity, in mixed case.
  • type() β€” Return a string containing the type of the entity, in upper case. Available types are CDATA, SDATA, NDATA (external entities only), SUBDOC, PI (newer versions of nsgmls only), or TEXT (newer versions of nsgmls only).
  • value() β€” Return a string containing the value of the entity, if it is internal.
  • sysid() β€” Return a string containing the system identifier of the entity (if any), if it is external.
  • pubid() β€” Return a string containing the public identifier of the entity (if any), if it is external.
  • filenames() β€” Return an array of strings containing any file names generated from the identifiers, if the entity is external.
  • notation() β€” Return the SGMLS_Notation object associated with the entity, if it is external.
  • data_attributes() β€” Return a reference to an associative array of data attribute names (in upper case) and the associated SGMLS_Attribute objects for the current entity.
  • data_attribute_names() β€” Return an array of data attribute names (in upper case) for the current entity.
  • data_attribute(ANAME) β€” Return the SGMLS_Attribute object for the data attribute named ANAME for the current entity.
  • set_data_attribute(ATTRIB) β€” Add the SGMLS_Attribute object ATTRIB to the current entity, replacing any other data attribute with the same name.
  • ext() β€” Return a reference to an associative array for user-defined extensions.

Copyright 1994 and 1995 by David Megginson, "dmeggins@aix1.uottawa.ca". Distributed under the terms of the Gnu General Public License (version 2, 1991) β€” see the file "COPYING" which is included in the SGMLS.pm distribution.


πŸ“ SEE ALSO: πŸ“

SGMLS::Output and SGMLS::Refs.


And that's the whole vibe, bestie! This was brought to you by perl v5.36.0 on 2022-10-13, serving SGMLS(3pm) realness. Stay slaying those parses! πŸ’…πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ«Ά

πŸ“¦ where this page comes from