NAME
Pistachio - turns source code into stylish HTML
VERSION
version 0.06
SYNOPSIS
use Pistachio;
# List supported languages and styles.
print Pistachio::supported;
# Get a Pistachio::Html object
my $handler = Pistachio::html_handler('Perl5', 'Github');
# Perl source code text (in typical usage, read from a file)
my $perl = join "\n", 'use strict;', 'package Foo::Bar', '...';
# Github-like CSS-styled HTML snippet.
my $snip = $handler->snippet(\$perl);
ROLL YOUR OWN LANGUAGE SUPPORT
Currently, only Perl 5 support is baked into Pistachio (via PPI::Tokenizer).
However, using Pistachio::Language, you can roll your own support for any language, as long as you can provide two subroutines. They are:
First, a subroutine that returns Pistachio::Tokens for that language.
And second, a subroutine that maps those tokens' types to CSS style definitions.
Generate HTML From Tokenized JSON
Using modules:
https://github.com/joeldalley/lib-JBD (JBD::JSON).
Providing a Pistachio::Language that knows how to parse and style arbitrary tokens allows the Pistachio core to render stylish HTML for those tokens.
In this example, we use JBD::JSON to parse JSON text, then map those tokens to Pistachio::Tokens. And we map the token types parsing with JBD::JSON produces to CSS style definitions.
use Pistachio;
use Pistachio::Token;
use Pistachio::Language;
use JBD::JSON 'parse';
# Argument: JSON input text. Returns arrayref of Pistachio::Tokens.
my $tokens = sub {
my $tokens = std_parse 'json_text', $_[0];
[map Pistachio::Token->new($_->type, $_->value), @$tokens];
};
# Argument: a token type. Returns corresponding CSS definition.
my $css = sub {
my %type_to_style = (
JsonNum => 'color:#008080',
JsonNull => 'color:#000',
JsonBool => 'color:#000',
JsonString => 'color:#D14',
JsonColon => 'color:#333',
JsonComma => 'color:#333',
JsonSquareBracket => 'color:#333',
JsonCurlyBrace => 'color:#333',
);
$type_to_style{$_[0] || ''} || '';
},
# Construct a Pistachio::Html, loaded with our JSON language object.
my $lang = Pistachio::Language->new(
'JSON',
tokens => $tokens,
css => $css
);
my $handler = Pistachio::html_handler($lang, 'GitHub');
# Now Pistachio understands how to convert JSON input texts
# into Github-styled HTML output. Proceed as in the synopsis:
my $json = join "\n", '{', '"key1":"value1", ..., '}';
my $snip = $handler->snippet(\$json);
AUTHOR
Joel Dalley <joeldalley@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Joel Dalley.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.