Skip to content

Instantly share code, notes, and snippets.

@karupanerura
Forked from issm/gist:3855912
Created October 9, 2012 23:26
Show Gist options
  • Save karupanerura/3862129 to your computer and use it in GitHub Desktop.
Save karupanerura/3862129 to your computer and use it in GitHub Desktop.
### <: $base_uri :> を $vars->{base_uri} の値で置き換えるプラグイン
package Text::Textile::Pluggable::Plugin::FooBar;
use strict;
use warnings;
use parent qw/Text::Textile::Pluggable::Plugin::Base/; ## コンストラクタを提供する
## 中で bless +{ base_uri => 'http://localhost:5000' } => 'FooBar'; みたいなかんじ
sub init {## object生成時に呼ばれる
my ($self,) = @_;
$self->{base_uri} //= 'http://localhost/';
$self->{base_uri} .= '/' if $self->{base_uri} !~ m{/$};
}
sub pre {## textileの前に呼ばれる
my ($self, $o, $text) = @_;
my $base_uri = $self->{base_uri};
$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 $textile = get_data_section('sample.textile');
say 'Textile:';
say $textile;
say 'Case 0:';
say Text::Textile::Pluggable->new
->load_plugin('FooBar')
->textile( $textile );
say 'Case 1:';
say Text::Textile::Pluggable->new
->load_plugin(FooBar => +{ base_uri => 'http://localhost:5000' })
->textile( $textile );
__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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment