Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Last active November 26, 2016 20:48
Show Gist options
  • Save pstuifzand/c61986ddb049736da55f19e5cad1e678 to your computer and use it in GitHub Desktop.
Save pstuifzand/c61986ddb049736da55f19e5cad1e678 to your computer and use it in GitHub Desktop.
Example of a perl program that uses Marpa::R2::HTML to convert part of an html file to blade syntax.
<html>
<body>
<div>
<div class="kw-content">
<h1>test</h1>
<div class="panel-content">
<div class="panel panel-default">
<div class="form-group">
<input id="name" type="text" name="title">
<input id="name" type="number" name="title">
<input id="name" type="checkbox" name="title">
<input id="name" type="text" name="title">
</div>
</div>
</div>
</div>
</div>
</body>
</html>
@section('content')
<h1>test</h1>
<div class="panel-content">
<div class="panel panel-default">
<div class="form-group">
{{ Form::text('title', null, ['id' => 'name']) }}
{{ Form::number('title', null, ['id' => 'name']) }}
{{ Form::checkbox('title', null, ['id' => 'name']) }}
{{ Form::text('title', null, ['id' => 'name']) }}
</div>
</div>
</div>
@stop
#!/usr/bin/env perl
use strict;
use Marpa::R2::HTML;
use File::Slurp 'read_file';
use Data::Dumper;
use List::Gather;
sub var_export {
my $arr = shift;
my $a = join ', ', gather {
while (my ($k, $v) = each %$arr) {
take qq{'$k' => '$v'};
}
};
return "[$a]";
}
my $html = read_file($ARGV[0]);
my $output = Marpa::R2::HTML::html(\$html, {
':TOP' => sub { return ( join q{}, @{ Marpa::R2::HTML::values() } ) },
'div.kw-content' => sub { return Marpa::R2::HTML::contents() },
'button' => sub {
my $attr = Marpa::R2::HTML::attributes();
my $a = var_export($attr);
my $text = Marpa::R2::HTML::contents();
return qq/{{ Form::button('$text', $a) }}/;
},
'input' => sub {
my $attr = Marpa::R2::HTML::attributes();
my $type = delete $attr->{type};
my $name = delete $attr->{name};
my $checked = $type eq 'checkbox' && delete $attr->{checked} eq 'checked';
my $a = var_export($attr);
my @a = (qq{'$name'}, 'null', $a);
if ($type eq 'checkbox') {
splice @a, 2, 0, ($checked ? 'true' : 'false');
}
return qq/{{ Form::$type(/ . join(', ', @a) . qq/) }}/;
},
});
my $x = $output;
$x =~ s/^\s+//m;
$x =~ m/^(\s+)</ms;
$x =~ s/^$1/ /gm;
$x =~ s/\A/ /gm;
$x =~ s/\s+$//gm;
print <<"OUTPUT";
\@section('content')
$x
\@stop
OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment