package LXRng::Markup::File;
use strict;
use HTML::Entities;
sub new {
my ($class, %args) = @_;
return bless(\%args, $class);
}
sub context {
my ($self) = @_;
return $$self{'context'};
}
sub safe_html {
my ($str) = @_;
return encode_entities($str, '^\n\r\t !\#\$\(-;=?-~\200-\377');
}
sub make_format_newline {
my ($self, $node) = @_;
my $line = 0;
my $tree = $self->context->vtree();
my $name = $node->name;
sub {
my ($nl) = @_;
$line++;
$nl = safe_html($nl);
return qq{$nl
}.
qq{}.
qq{};
}
}
sub format_comment {
my ($self, $com) = @_;
$com = safe_html($com);
return qq{};
}
sub format_string {
my ($self, $str) = @_;
$str = safe_html($str);
return qq{$str}
}
sub format_include {
my ($self, $paths, $all, $pre, $inc, $suf) = @_;
my $tree = $self->context->vtree();
if (@$paths > 1) {
$pre = safe_html($pre);
$inc = safe_html($inc);
$suf = safe_html($suf);
my $alts = join("|", map { $_ } @$paths);
return qq{$pre$inc$suf};
}
elsif (@$paths > 0) {
$pre = safe_html($pre);
$inc = safe_html($inc);
$suf = safe_html($suf);
return qq{$pre$inc$suf};
}
else {
return safe_html($all);
}
}
sub format_code {
my ($self, $idre, $res, $frag) = @_;
my $tree = $self->context->vtree();
my $path = $self->context->path();
$frag =~ s{(.*?)$idre|(.+)}{
if ($2) {
unless (exists($$res{$2})) {
my $pre = $1;
my $sym = safe_html($2);
safe_html($pre).
qq{$sym};
}
else {
safe_html($1.$2);
}
}
else {
safe_html($3);
}
}ge;
return $frag;
}
sub format_raw {
my ($self, $str) = @_;
$str = safe_html($str);
$str =~ s((http://[^/\"]+(/[^\s\"]*|)[^.\,\)\>\"]))
($1)g;
return $str;
}
sub markupfile {
my ($self, $subst, $parse) = @_;
my ($btype, $frag) = $parse->nextfrag;
return () unless defined $frag;
$btype ||= 'code';
if ($btype and exists $$subst{$btype}) {
return $$subst{$btype}->s($frag);
}
else {
return $frag;
}
}
1;