Pod-Elemental-Transformer-ExampleRunner

 view release on metacpan or  search on metacpan

lib/Pod/Elemental/Transformer/ExampleRunner.pm  view on Meta::CPAN

    is => 'rw', 
    default => sub { sub {} } 
);

has indent => (
    is => 'ro',
    isa => 'Str',
    default => '  ',
);

sub transform_node {
  my ($self, $doc) = @_;
  use Data::Dumper;

  for my $i (reverse(0 .. $#{ $doc->children })) {
    my $node = $doc->children->[ $i ];
    next unless $node->isa('Pod::Elemental::Element::Generic::Command')
                and $node->{command} eq $self->command
                ;

    my ($command, $subcommand) = split ' ', $node->{'content'}, 2;

    $self->debug->("going to $command with $subcommand on ".Dumper $node);
    my @replacements;
    if (my $action = $self->can("_$command") ) { 
        @replacements = $self->$action( $node,  $subcommand )
    }
    splice @{ $doc->children }, $i, 1, @replacements;
 }
}

sub _run {
    my ($self, $node, $subcommand) = @_;
    my ($filename, @args) = split ' ', $subcommand;
    my $path   = $self->script_path . $filename;

    open my $read, '-|', $path, @args 
        or return Pod::Elemental::Element::Generic::Nonpod->new({
            content => "# Couldn't run $path: $!\n\n"
        });
    my $output = do {local $/; <$read>; };
    close $read;
    my $exit   = $? >> 8;
    $output    = $self->indent_text_in($output);
    local $" = ' ';
    chomp $output;
    my $end = '.';
    $end = "# $filename exited with $exit" if $exit;

    Pod::Elemental::Element::Pod5::Verbatim->new({
        content => "#running $path @args\n\n$output\n\n#$end"
    });

}

sub _source {
    my ($self, $node, $subcommand) = @_;
    my ($filename, @args) = split ' ', $subcommand;
    my $path   = $self->script_path . $filename;
    my $source = do {
        local (@ARGV,$/) = $path;
        <>
    };
    $source = $self->remove_yadas_from($source);
    $source = $self->indent_text_in($source);
    chomp $source;
    Pod::Elemental::Element::Pod5::Verbatim->new({
        content => "#see $path\n\n$source\n\n#end of listing"
    });
}

sub indent_text_in {
    my ($self, $source) = @_;
    $source =~ s/^/@{[ $self->indent ]}/mg;
    $source
}

has yada_start   => (
    is => 'ro',
    isa => 'Str',
    default => "# {{{ ExampleRunnerHide(?: ([^\n]+))?"
);

has yada_end => (
    is => 'ro',
    isa => 'Str',
    default => "# ExampleRunnerShow }}}"
);

# lines matching this will be collapsed into a single yada_pattern 
has yada_pattern => (
    is => 'ro',
    isa => 'Str',
    default => "# boring(?: ([^\n]+))?",
);

has yada_replace => (
    is => 'ro',
    isa => 'Str',
    default => "#...",
);
sub remove_yadas_from {
    my ($self, $source) = @_;

    # father, forgive me, for I have sinned,
    # it has been 8 months since my last code review,
    # i have been coding horrible things,  wicked things!
    # calling methods in a de-referenced array-ref, for interpolation sake.

    # single line yada
    $source =~ s/
        (?:
            ^
            (\s*)
            [^\n]*
            (?-x:@{[ $self->yada_pattern ]})
            $
        \n*)+/$1@{[ $self->yada_replace ]}$2\n/smgx;

    # a lengthy yada ...
    my $long_yada = qr{(?:\n)*@{[ $self->yada_start ]}(?s:.*?)@{[ $self->yada_end ]}(\n*)};



( run in 0.560 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )