App-sdview

 view release on metacpan or  search on metacpan

lib/App/sdview/Parser/Pod.pm  view on Meta::CPAN


C<=code> directive to set the highlighter language name for the next verbatim
paragraph.

=item *

Also follows the C<=for highlighter ...> spec used by
L<https://metacpan.org/pod/Pod::Simple::XHTML::WithHighlightConfig> for
setting the language name for following verbatim paragraphs.

=item *

Tables are I<partially> supported according to the suggestion given in
L<https://www.nntp.perl.org/group/perl.perl5.porters/2021/11/msg261904.html>,
within a section marked C<=begin table> or C<=begin table md>.

=item *

Tables are also partially supported by a format similar to mediawiki notation,
within a section marked C<=begin table mediawiki>.

=back

=cut

sub find_file ( $class, $name )
{
   # We could use `perldoc -l` but it's slow and noisy when it fails
   require Pod::Perldoc;
   my ( $found ) = Pod::Perldoc->new->searchfor( 0, $name, @INC );
   return $found;
}

sub can_parse_file ( $class, $file )
{
   return $file =~ m/\.pm$|\.pl$|\.pod$/;
}

ADJUST
{
   $self->accept_target( 'highlighter' );
   $self->accept_directive_as_data( 'code' );

   $self->accept_target( 'table' );
}

field @_indentstack;
field @_parastack;

field $_curpara;

field %_verbatim_options = ( language => "__AUTO__" );
field %_next_verbatim_options;

field $_conv_nbsp;

method parse_file ( $fh )
{
   push @_indentstack, 0;
   push @_parastack, [];
   $self->SUPER::parse_file( $fh );
   return $_parastack[0]->@*;
}

method parse_string ( $str )
{
   push @_indentstack, 0;
   push @_parastack, [];
   $self->SUPER::parse_string_document ( $str );
   return $_parastack[0]->@*;
}

my %PARA_TYPES = (
   Para     => "App::sdview::Para::Plain",
   Verbatim => "App::sdview::Para::Verbatim",
);

field $_redirect_text;

method start_Document { $self->reset_tags; }

method start_head1 { $self->_start_head( 1 ); }
method start_head2 { $self->_start_head( 2 ); }
method start_head3 { $self->_start_head( 3 ); }
method start_head4 { $self->_start_head( 4 ); }
method _start_head ( $level )
{
   push $_parastack[-1]->@*, $_curpara = App::sdview::Para::Heading->new(
      level => $level,
      text  => String::Tagged->new,
   );
   $self->reset_tags;
}

method start_code { $self->_start_highlighter( \%_next_verbatim_options ); }
method end_code { undef $_redirect_text; }

method start_for ( $attrs )
{
   my $target = $attrs->{target};
   my $code = $self->can( "start_for_$target" ) or return;
   return $self->$code( $attrs );
}
method end_for  { undef $_redirect_text; }

method start_for_highlighter { $self->_start_highlighter( \%_verbatim_options ) }

method start_for_table ( $attrs )
{
   my @spec = split m/\s+/, $attrs->{title} // "";
   $spec[0] = "style=$spec[0]" if @spec and $spec[0] !~ m/=/;
   my %spec = map { m/^(.*?)=(.*)$/ ? ( $1, $2 ) : () } @spec;

   my $style = $spec{style} // "md";

   $style eq "md" and
      $_redirect_text = \&_handle_text_table_md, return;
   $style eq "mediawiki" and
      $_redirect_text = \&_handle_text_table_mediawiki, return;

   warn "TODO unrecognised table style $style\n";
}

method _start_highlighter ( $options )
{
   $_redirect_text = method ( $text ) {
      my @args = split m/\s+/, $text;
      $args[0] = "language=$args[0]" if @args and $args[0] !~ m/=/;

lib/App/sdview/Parser/Pod.pm  view on Meta::CPAN


sub _split_table_row ( $str )
{
   # Leading/trailing pipes are optional
   $str =~ s/^\s*\|//;
   $str =~ s/\|\s*$//;

   my @cols = split m/\|/, $str;
   s/^\s+//, s/\s+$// for @cols;

   # TODO: Find out why these parsers aren't reusable
   $_ = App::sdview::Parser::Pod::_TableCellParser->new->parse_string( $_ ) for @cols;

   return \@cols;
}

method _handle_text_table_mediawiki ( $text )
{
   my @lines = split m/\n/, $text
      or return;

   my @rows;
   foreach my $line ( @lines ) {
      $line =~ m/^\|-/ and
         push @rows, [] and next;

      $line =~ s/^([!|])\s*// or
         warn "Unsure what to do with line $line" and next;
      my $chr = $1;
      my $heading = ( $chr eq "!" );

      foreach my $cell ( split m/\s*\Q$chr$chr\E\s*/, $line ) {
         @rows or push @rows, [];
         push $rows[-1]->@*, App::sdview::Para::TableCell->new(
            align => "left", # TODO
            heading => $heading,
            text => App::sdview::Parser::Pod::_TableCellParser->new->parse_string( $cell ),
         );
      }
   }

   push $_parastack[-1]->@*, App::sdview::Para::Table->new(
      rows => \@rows,
   );
}

class App::sdview::Parser::Pod::_TableCellParser
{
   inherit Pod::Simple::Methody;
   apply App::sdview::Parser::Pod::_TagHandler;

   field $body;

   method parse_string ( $str )
   {
      $body = String::Tagged->new;

      # Protect a leading equals sign
      $str =~ s/^=/E<61>/;

      $self->SUPER::parse_string_document( "=pod\n\n$str" );

      return $body;
   }

   method handle_text ( $text )
   {
      $body->append_tagged( $text, $self->curtags );
   }
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;



( run in 3.146 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )