POE::Component::Syntax::Highlight::CSS - non-blocking wrapper around Syntax::Highlight::CSS
use strict; use warnings; use POE qw/Component::Syntax::Highlight::CSS/; my $poco = POE::Component::Syntax::Highlight::CSS->spawn; POE::Session->create( package_states => [ main => [qw(_start results)] ], ); $poe_kernel->run; sub _start { $poco->parse({ event => 'results', in => 'a:hover { font-weight: bold; }', } ); } 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::CSS with added functionality of fetching the CSS code to highlight from a given URI. The Syntax::Highlight::CSS provides interface to highlight CSS code by wrapping syntax elements into HTML <span> elements with different class names.
<span>
spawn
my $poco = POE::Component::Syntax::Highlight::CSS->spawn; POE::Component::Syntax::Highlight::CSS->spawn( alias => 'highlighter', ua => LWP::UserAgent->new( timeout => 30, agent => 'Opera 9.5' ), 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::CSS 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/main.css', # or in => 'a:hover { font-weight: bold; }', nnn => 1, pre => 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/main.css', # or in => 'a:hover { font-weight: bold; }', nnn => 1, pre => 1, _blah => 'pooh!', session => 'other', } );
Instructs the component to parse given CSS 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 CSS code you wish to highlight.
in
{ in => 'a:hover { font-weight: bold; }', }
Optional if uri argument is set. Takes a string as a value which represents CSS 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 CSS code. Defaults to: 0
pre
{ pre => 1, }
Optional. Takes either true or false values. When set to a true value will wrap highlighted CSS 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 class="css-code"><span class="ch-l"> 0</span> <span class="ch-sel">a<span class="ch-ps">:hover</span></span> { <span class="ch-p">font-weight</span>: <span class="ch-v">bold</span>; }</pre>', 'in' => 'a:hover { font-weight: bold; }', '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:
parse()
$_[ARG0]
out
{ 'out' => '<pre class="css-code"><span class="ch-l"> 0</span> <span class="ch-sel">a<span class="ch-ps">:hover</span></span> { <span class="ch-p">font-weight</span>: <span class="ch-v">bold</span>; }</pre>', }
The out key will contain a string representing highlighted CSS code. See documentation for Syntax::Highlight::CSS for explanation of each of the possible class="" names on the generated <span>s.
class=""
{ 'in' => 'a:hover { font-weight: bold; }', } { 'uri' => 'http://zoffix.com' }
If in argument was specified to parse event/method the in key will contain the original CSS code. If uri argument was specified the uri key will contain the original URI.
error
{ 'error' => '404 Not Found' }
If you are using the uri argument to parse event/method then there is a chance for a network error to occur while the module is fetching the CSS code. If that's the case then the error key will be present and its value will be a human parsable error message.
{ '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::CSS
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.