Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bad Code Example
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
You can’t perform that action at this time.