Skip to content

Instantly share code, notes, and snippets.

@issm
Created October 9, 2012 00:53
Show Gist options
  • Save issm/3855912 to your computer and use it in GitHub Desktop.
Save issm/3855912 to your computer and use it in GitHub Desktop.
### <: $base_uri :> を $vars->{base_uri} の値で置き換えるプラグイン
package Text::Textile::Pluggable::Plugin::FooBar;
use strict;
use warnings;
sub pre {
my ($o, $text, $vars) = @_;
my $base_uri = $vars->{base_uri} || 'http://localhost/';
$base_uri .= '/' if $base_uri !~ m{/$};
$text =~ s/<:\s*\$base_uri\s*:>/$base_uri/g;
return $text;
}
1;
### メイン
package main;
use 5.10.0;
use warnings;
use Text::Textile::Pluggable;
use Data::Section::Simple qw/get_data_section/;
my $tpp = Text::Textile::Pluggable->new( plugins => [qw/FooBar/] );
my $textile = get_data_section('sample.textile');
say 'Textile:';
say $textile;
say 'Case 0:';
say $tpp->textile( $textile );
say 'Case 1:';
say $tpp->textile( $textile, { base_uri => 'http://localhost:5000' } );
say 'Case 2:';
say $tpp->textile( $textile, { base_uri => 'http://www.example.com/app/' } );
__DATA__
@@ sample.textile
* "Link1":<: $base_uri :>
* "Link2":<: $base_uri :>foo/bar/
@@ output.expected
Textile:
* "Link1":<: $base_uri :>
* "Link2":<: $base_uri :>foo/bar/
Case 0:
<ul>
<li><a href="http://localhost/">Link1</a></li>
<li><a href="http://localhost/foo/bar/">Link2</a></li>
</ul>
Case 1:
<ul>
<li><a href="http://localhost:5000/">Link1</a></li>
<li><a href="http://localhost:5000/foo/bar/">Link2</a></li>
</ul>
Case 2:
<ul>
<li><a href="http://www.example.com/app/">Link1</a></li>
<li><a href="http://www.example.com/app/foo/bar/">Link2</a></li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment