Created
November 6, 2011 22:00
-
-
Save broquaint/1343603 to your computer and use it in GitHub Desktop.
Last Mangle in Paris - a POC for Gitalist's simplified ContentMangler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lmip; | |
use Modern::Perl; | |
use Method::Signatures; | |
use Clone qw(clone); | |
use Term::ANSIColor qw(:constants); | |
my %config = ( | |
diff => { | |
default => 'synhl_diff', | |
}, | |
blob => { | |
default => 'synhl_blob', | |
doc => 'enpodulate', | |
}, | |
); | |
sub synhl { | |
my($stuff) = @_; | |
local $Term::ANSIColor::AUTORESET = 1; | |
$stuff =~ s/(\w+)/BLUE $1/eg; | |
return $stuff; | |
} | |
func synhl_blob($stuff) { | |
return unless $stuff->{filename} =~ /\.pl/; | |
$stuff->{blob} = synhl $stuff->{blob}; | |
} | |
func synhl_diff($stuff) { | |
$_ = synhl $_ for @{ $stuff->{diffs} || [] }; | |
} | |
func enpodulate($stuff) { | |
return unless $stuff->{filename} =~ /\.p([lm]|od)$/; | |
$stuff->{blob} = "DOCS: $stuff->{blob}"; | |
} | |
func find_mangler($action, $mangler?) { | |
my $a = $config{$action} or return; | |
my $m = $a->{$mangler || 'default'}; | |
__PACKAGE__->can($m); | |
} | |
# XXX clone $clay? | |
func mangle($action, $clay_orig, $params = {}) { | |
my $clay = clone $clay_orig; | |
my $m = find_mangler $action => $params->{mangler}; | |
$m and $m->($clay); | |
$clay; | |
} | |
package main; | |
use Test::More 'no_plan'; | |
my $blobby = { | |
blob => 'my $self = @_;', | |
filename => '/foo.pl', | |
}; | |
my $b = lmip::mangle blob => $blobby; | |
ok $b->{blob} ne $blobby->{blob}, "The perl blob was highlighted"; | |
$blobby->{filename} = 'foo.pod'; | |
$b = lmip::mangle blob => $blobby; | |
ok $b->{blob} eq $blobby->{blob}, "The POD blob wasn't highlighted"; | |
$b = lmip::mangle blob => $blobby, { mangler => 'doc' }; | |
ok $b->{blob} ne $blobby->{blob}, "The POD blob was enpodulated"; | |
like $b->{blob}, qr/^DOC/, "The POD blob got docs"; | |
$b = lmip::mangle diff => { diffs => [qw/+abc -123/] }; | |
ok "@{$b->{diffs}}" ne "+abc -123", "The diffs were endiffened"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment