Zoffix Znet > POE-Component-Bundle-WebDevelopment-1.001004 > POE::Component::Syntax::Highlight::HTML

Download:
POE-Component-Bundle-WebDevelopment-1.001004.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 1.001004   Source  

NAME ^

POE::Component::Syntax::Highlight::HTML - non-blocking wrapper around Syntax::Highlight::HTML

SYNOPSIS ^

    use strict;
    use warnings;

    use POE qw/Component::Syntax::Highlight::HTML/;

    my $poco = POE::Component::Syntax::Highlight::HTML->spawn;

    POE::Session->create( package_states => [ main => [qw(_start results)] ], );

    $poe_kernel->run;

    sub _start {
        $poco->parse( {
                event => 'results',
                in    => '<p>Foo <a href="bar">bar</a></p>',
            }
        );
    }

    sub results {
        print "$_[ARG0]->{out}\n";
        $poco->shutdown;
    }

Using event based interface is also possible of course.

DESCRIPTION ^

The module is a non-blocking wrapper around Syntax::Highlight::HTML with added functionality of fetching the HTML code to highlight from a given URI. The Syntax::Highlight::HTML provides interface to highlight HTML code by wrapping syntax elements into HTML <span> elements with different class names.

CONSTRUCTOR ^

spawn

    my $poco = POE::Component::Syntax::Highlight::HTML->spawn;

    POE::Component::Syntax::Highlight::HTML->spawn(
        alias => 'highlighter',
        ua    => LWP::UserAgent->new( timeout => 30 ),
        options => {
            debug => 1,
            trace => 1,
            # POE::Session arguments for the component
        },
        debug => 1, # output some debug info
    );

The spawn method returns a POE::Component::Syntax::Highlight::HTML object. It takes a few arguments, all of which are optional. The possible arguments are as follows:

alias

    ->spawn( alias => 'highlighter' );

Optional. Specifies a POE Kernel alias for the component.

ua

    ->spawn( ua  => LWP::UserAgent->new( timeout => 30, agent => 'Opera 9.5' ) );

Optional. The ua argument takes an LWP::UserAgent-like object as a value, the object must have a get() method that returns HTTP::Response object and takes a URI to fetch as the first argument. Default to:

    LWP::UserAgent->new(
        timeout => 30,
        agent   => 'Opera 9.5',
    );

options

    ->spawn(
        options => {
            trace => 1,
            default => 1,
        },
    );

Optional. A hashref of POE Session options to pass to the component's session.

debug

    ->spawn(
        debug => 1
    );

When set to a true value turns on output of debug messages. Defaults to: 0.

METHODS ^

parse

    $poco->parse( {
            event       => 'event_for_output',
            uri         => 'http://zoffix.com/',
                # or
            in          => '<p>Foo <a href="bar">bar</a></p>',
            pre         => 1,
            nnn         => 1,
            _blah       => 'pooh!',
            session     => 'other',
        }
    );

Takes a hashref as an argument, does not return a sensible return value. See parse event's description for more information.

session_id

    my $poco_id = $poco->session_id;

Takes no arguments. Returns component's session ID.

shutdown

    $poco->shutdown;

Takes no arguments. Shuts down the component.

ACCEPTED EVENTS ^

parse

    $poe_kernel->post( highlighter => parse => {
            event       => 'event_for_output',
            uri         => 'http://zoffix.com/',
                # or
            in          => '<p>Foo <a href="bar">bar</a></p>',
            pre         => 1,
            nnn         => 1,
            _blah       => 'pooh!',
            session     => 'other',
        }
    );

Instructs the component to highlight HTML code. Takes a hashref as an argument, the possible keys/value of that hashref are as follows:

event

    { event => 'results_event', }

Mandatory. Specifies the name of the event to emit when results are ready. See OUTPUT section for more information.

uri

    { uri => 'http://zoffix.com/' }

Optional if in argument is set. Takes a URI as a value, that uri must point to HTML code you wish to highlight.

in

    { in => '<p>Foo <a href="bar">bar</a></p>', }

Optional if uri argument is set. Takes a string as a value which represents HTML code to syntax-highlight. If uri argument is specified then the in argument is ignored.

nnn

    { nnn => 1, }

Optional. Takes either true or false values. When set to a true value will insert line numbers into the highlighted HTML code. Defaults to: 0

pre

    { pre => 1, }

Optional. Takes either true or false values. When set to a true value will wrap highlighted HTML code into a <pre> element. Defaults to: 1

session

    { session => 'other' }

    { session => $other_session_reference }

    { session => $other_session_ID }

Optional. Takes either an alias, reference or an ID of an alternative session to send output to.

user defined

    {
        _user    => 'random',
        _another => 'more',
    }

Optional. Any keys starting with _ (underscore) will not affect the component and will be passed back in the result intact.

shutdown

    $poe_kernel->post( highlighter => 'shutdown' );

Takes no arguments. Tells the component to shut itself down.

OUTPUT ^

    $VAR1 = {
          'out' => '<pre>
                <span class="h-ab">&lt;</span><span class="h-tag">p</span><span
                class="h-ab">&gt;</span>Foo <span class="h-ab">&lt;</span><span
                class="h-tag">a</span> <span class="h-attr">href</span>=<span
                class="h-attv">"bar</span>"<span class="h-ab">&gt;</span>bar<span
                class="h-ab">&lt;/</span><span class="h-tag">a</span><span
                class="h-ab">&gt;</span><span class="h-ab">&lt;/</span><span
                class="h-tag">p</span><span class="h-ab">&gt;</span></pre>',
        'in' => '<p>Foo <a href="bar">bar</a></p>',
        'nnn'   => 1,
        'pre'   => 1,
        '_blah' => 'foos'
    };

The event handler set up to handle the event which you've specified in the event argument to parse() method/event will receive input in the $_[ARG0] in a form of a hashref. The possible keys/value of that hashref are as follows:

out

    {
          'out' => '<pre>
                <span class="h-ab">&lt;</span><span class="h-tag">p</span><span
                class="h-ab">&gt;</span>Foo <span class="h-ab">&lt;</span><span
                class="h-tag">a</span> <span class="h-attr">href</span>=<span
                class="h-attv">"bar</span>"<span class="h-ab">&gt;</span>bar<span
                class="h-ab">&lt;/</span><span class="h-tag">a</span><span
                class="h-ab">&gt;</span><span class="h-ab">&lt;/</span><span
                class="h-tag">p</span><span class="h-ab">&gt;</span></pre>',
    }

The out key will contain a string representing highlighted HTML code. See documentation for Syntax::Highlight::HTML for explanation of each of the possible class="" names on the generated <span>s.

in and uri

    { 'in' => '<p>Foo <a href="bar">bar</a></p>', }

    { 'uri' => 'http://zoffix.com' }

If in argument was specified to parse event/method the in key will contain the original HTML code. If uri argument was specified the uri key will contain the original URI.

nnn and pre

    {
        'nnn' => 1,
        'pre' => 1,
    }

If you specified either nnn or pre arguments to the parse() event/method they will be present in the output with the values that you set to them.

user defined

    { '_blah' => 'foos' }

Any arguments beginning with _ (underscore) passed into the parse() event/method will be present intact in the result.

SEE ALSO ^

POE, Syntax::Highlight::HTML

REPOSITORY ^

Fork this module on GitHub: https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment

BUGS ^

To report bugs or request features, please use https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment/issues

If you can't access GitHub, you can email your request to bug-POE-Component-Bundle-WebDevelopment at rt.cpan.org

AUTHOR ^

Zoffix Znet <zoffix at cpan.org> (http://zoffix.com/, http://haslayout.net/)

LICENSE ^

You can use and distribute this module under the same terms as Perl itself. See the LICENSE file included in this distribution for complete details.

syntax highlighting: