App-WRT

 view release on metacpan or  search on metacpan

lib/App/WRT/Markup.pm  view on Meta::CPAN

    include_process($self, $everything);
    textile_process($everything);
    markdown_process($everything);
    $everything =~ s!<image>(.*?)</image>!$self->image_markup($file, $1)!seg;

    foreach my $key (keys %tags) {
       # Set some replacements, unless they've been explicitly set already:
       $end_tags{$key} ||= $tags{$key};

        # Transform blocks:
        while ($everything =~ m| (<$key>\n?) (.*?) (\n?</$key>) |sx) {
            my $open = $1;
            my $block = $2;
            my $close = $3;

            # Save the bits between instances of the block:
            my (@interstices) = split /\Q$open$block$close\E/s, $everything;

            # Transform dashes, blank lines, and newlines:
            dashes($dashes{$key}, $block)          if defined $dashes{$key};
            $block =~ s/\n\n/$blank_lines{$key}/gs if defined $blank_lines{$key};
            newlines($newlines{$key}, $block)      if defined $newlines{$key};

            # Slap it all back together as $everything, with start and end
            # tags:
            $block = "<$tags{$key}>$block</$end_tags{$key}>";
            $everything = join $block, @interstices;
        }
    }

    return $everything;
}

=item newlines($replacement, $block)

Inline replace single newlines (i.e., line ends) within the block, except those
preceded by a double-quote, which probably indicates a still-open tag.

=cut

sub newlines {
  $_[1] =~ s/(?<=[^"\n])  # not a double-quote or newline
                          # don't capture

             \n           # end-of-line

             (?=[^\n])    # not a newline
                          # don't capture
            /$_[0]/xgs;
}

=item dashes($replacement, $block)

Inline replace double dashes in a block - " -- " - with a given replacement.

=cut

sub dashes {
  $_[1] =~ s/(\s+)      # whitespace - no capture
             \-{2}      # two dashes
             (\n|\s+|$) # newline, whitespace, or eol
            /$1$_[0]$2/xgs;

}

=item include_process

Inline replace <include>filename</include> tags, replacing them with the
contents of files.

=cut

sub include_process {
  my $wrt = shift;

  $_[0] =~ s{

    <include>  # start tag
      (.*?)     # anything (non-greedy)
    </include> # end tag

  }{
    retrieve_include($wrt, $1);
  }xesg;
}

=item retrieve_include

Get the contents of an included file.  This probably needs a great
deal more thought than I am presently giving it.

=cut

sub retrieve_include {
  my $wrt = shift;
  my ($file) = @_;

  # Trim leading and trailing spaces:
  $file =~ s/^\s+//;
  $file =~ s/\s+$//;

  if ($file =~ m{^ (/ | [.]/) }x) {
    # TODO: Leads with a slash or a ./
    croak('Tried to open an include path with a leading / or ./ - not yet supported.');
  } else {
    # Use the archive root as path.
    $file = $wrt->{root_dir} . '/' . $file;
  }

  if ($wrt->{cache_includes}) {
    if (defined $wrt->{include_cache}->{$file}) {
      return $wrt->{include_cache}->{$file};
    }
  }

  unless (-e $file) {
    carp "No such file: $file";
    return '';
  }

  if (-d $file) {



( run in 1.490 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )