Created
April 29, 2014 12:53
-
-
Save hirokidaichi/11399417 to your computer and use it in GitHub Desktop.
Bad Code Example
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
use strict; | |
use warnings; | |
my $Attribute = 0; | |
my $Text = 0; | |
my @Stack = (); | |
my @Attribute = (); | |
sub oremark2html { | |
my ($oremark) = @_; | |
my $word = ''; | |
my $res = ""; | |
my $attrSp = 0; | |
for my $c ( split //, $oremark ) { | |
if ($Text) { | |
if ( $c eq '"' ) { | |
$Text = 0; | |
} | |
if ( $c eq '"' ) { | |
if ($Attribute) { | |
$res.= $c; | |
next; | |
} | |
next; | |
} | |
$res.= $c; | |
next; | |
} | |
if ($Attribute) { | |
if( $c =~ /\s/){ | |
$attrSp = 1; | |
next; | |
} | |
if ( $c eq ')' ) { | |
$Attribute = 0; | |
next; | |
} | |
if( $attrSp ){ | |
$attrSp = 0; | |
$res.= " "; | |
} | |
$res.= $c; | |
next; | |
} | |
if ( $c =~ /\w{1}/ ) { | |
if ( !$word ) { | |
$res.= "<"; | |
} | |
$word .= $c; | |
$res.= $c; | |
next; | |
} | |
if ( $c =~ /\s{1}/ ) { | |
next; | |
} | |
if ( $c eq '(' ) { | |
$Attribute = 1; | |
$attrSp = 1; | |
next; | |
} | |
if ( $c eq '{' ) { | |
push @Stack, $word; | |
$word = ''; | |
$res.= ">"; | |
next; | |
} | |
if ( $c eq '}' ) { | |
my $word = pop @Stack; | |
$res.= "</$word>"; | |
next; | |
} | |
if ( $c eq '"' ) { | |
$Text = 1; | |
if ($Attribute) { | |
$res.= $c; | |
next; | |
} | |
next; | |
} | |
die("unexpected token $c"); | |
} | |
return $res; | |
} | |
my $test = [ | |
q//, | |
q/table{}/, | |
q/html{ body{div{span{}}}}/, | |
q/html(debug){}/, | |
q/div( | |
data-attr="1" | |
data-piyo=checked | |
){}/ | |
]; | |
my $expects = [ | |
'', | |
'<table></table>', | |
'<html><body><div><span></span></div></body></html>', | |
'<html debug></html>', | |
'<div data-attr="1" data-piyo=checked></div>' | |
]; | |
use Test::More; | |
for my $t (@$test){ | |
my $expected = shift @$expects; | |
is(oremark2html($t) , $expected); | |
} | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment