<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package JavaScript::Syntax::HTML;

=head1 NAME

JavaScript::Syntax::HTML - Convert JavaScript sourcecode to HTML

=head1 SYNOPSIS

    use JavaScript::Syntax::HTML qw(to_html to_html_document);
    my $html_fragment = to_html($js_src);
    my $html_doc = output_html_document($js_src);

=head1 DESCRIPTION

JavaScript::Syntax::HTML processes JavaScript code and outputs HTML with
all reserved words marked up. 

The to_html method only outputs an HTML fragment (no &lt;body&gt; or &lt;html&gt; tags),
and only marks up the reserved words with CSS styles.

The to_html_document method outputs a full HTML document, and does include
style information for the reserved words markup.

The style classes that can be defined for use with to_html are C&lt;comment&gt;, 
C&lt;literal&gt;, and C&lt;reserved&gt;.

=head1 AUTHOR

Gabriel Reid gab_reid@users.sourceforge.net

=cut

use warnings;
use strict;
use Exporter;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(to_html to_html_document);

sub to_html {
    local $_ = shift;
    s/\&amp;/&amp;amp;/g;
    s/&lt;/&amp;lt;/g;
    s/&gt;/&amp;gt;/g;
    s/
        ((?:\/\*.*?\*\/)
        |
        (?:\/\/[^\n]*$))
        |
        ('[^']*'|"[^"]*")
        |
        \b(function|for|if|while|return|else|prototype|this)\b
        / get_substitution($1, $2, $3) /egsxm; 
    $_;
}

sub get_substitution {
    my ($comment, $stringliteral, $resword) = @_;
    my $content;
    if ($comment){
        $comment =~ s/(\@\w+)\b/get_span('attrib', $1)/eg;
        return get_span('comment', $comment);
    } elsif ($stringliteral){
        return get_span('literal', $stringliteral);
    } elsif ($resword){
        return get_span('reserved', $resword);
    }
}

sub get_span {
    my ($class, $inner) = @_;
    qq(&lt;span class="$class"&gt;$inner&lt;/span&gt;);
}

sub to_html_document {
    my ($src) = @_;
    $src = &amp;to_html($src);
    qq(
&lt;html&gt;
    &lt;head&gt;
	&lt;style&gt;
            body { background: #FFFFFF }
	    .reserved { color: #DD3333 }
            .comment { color: #339933 }
            .attrib { color: #FF0000 }
            .literal { color: #5555FF }
	&lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
	&lt;pre&gt;
$src
	&lt;/pre&gt;
    &lt;/body&gt;
&lt;/html&gt;);
}
</pre></body></html>