POE::Component::Syntax::Highlight::HTML - non-blocking wrapper around Syntax::Highlight::HTML
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.
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.
<span>
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:
get()
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.
0
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.
$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 => '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
<pre>
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 => 'random', _another => 'more', }
Optional. Any keys starting with _ (underscore) will not affect the component and will be passed back in the result intact.
_
$poe_kernel->post( highlighter => 'shutdown' );
Takes no arguments. Tells the component to shut itself down.
$VAR1 = { 'out' => '<pre> <span class="h-ab"><</span><span class="h-tag">p</span><span class="h-ab">></span>Foo <span class="h-ab"><</span><span class="h-tag">a</span> <span class="h-attr">href</span>=<span class="h-attv">"bar</span>"<span class="h-ab">></span>bar<span class="h-ab"></</span><span class="h-tag">a</span><span class="h-ab">></span><span class="h-ab"></</span><span class="h-tag">p</span><span class="h-ab">></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:
event
parse()
$_[ARG0]
out
{ 'out' => '<pre> <span class="h-ab"><</span><span class="h-tag">p</span><span class="h-ab">></span>Foo <span class="h-ab"><</span><span class="h-tag">a</span> <span class="h-attr">href</span>=<span class="h-attv">"bar</span>"<span class="h-ab">></span>bar<span class="h-ab"></</span><span class="h-tag">a</span><span class="h-ab">></span><span class="h-ab"></</span><span class="h-tag">p</span><span class="h-ab">></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.
class=""
{ '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' => 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.
{ '_blah' => 'foos' }
Any arguments beginning with _ (underscore) passed into the parse() event/method will be present intact in the result.
POE, Syntax::Highlight::HTML
Fork this module on GitHub: https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment
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
bug-POE-Component-Bundle-WebDevelopment at rt.cpan.org
Zoffix Znet <zoffix at cpan.org> (http://zoffix.com/, http://haslayout.net/)
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.
LICENSE
To install POE::Component::Bundle::WebDevelopment, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POE::Component::Bundle::WebDevelopment
CPAN shell
perl -MCPAN -e shell install POE::Component::Bundle::WebDevelopment
For more information on module installation, please visit the detailed CPAN module installation guide.