Skip to content

Instantly share code, notes, and snippets.

@tene
Created March 20, 2009 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tene/82245 to your computer and use it in GitHub Desktop.
Save tene/82245 to your computer and use it in GitHub Desktop.
[sweeks@kweh web]$ r t.pl
<html>hi
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li></ul></html>
use v6;
our @frames;
sub show(&code) is export() {
new_buffer_frame();
&code();
return end_buffer_frame();
}
sub ul(&c) is export() {
_tag('ul', &c);
}
sub li(&c) is export() {
_tag('li', &c);
}
sub html(&c) is export() {
_tag('html', &c);
}
sub _tag(Str $tag, &code) {
my $buf = "\n<$tag>";
new_buffer_frame();
my $ret = &code();
my $frame = end_buffer_frame();
if $frame.chars() > 0 {
$buf ~= $frame;
}
else {
$buf ~= $ret;
}
$buf ~= "</$tag>";
outs($buf);
return '';
}
sub outs($text) is export() {
@frames[0] ~= ($text);
}
sub new_buffer_frame {
@frames.unshift('');
}
sub end_buffer_frame {
@frames.shift();
}
class Web::Tags::Buffer {
has $.data is rw;
method append($text) {
$.data ~= $text;
}
}
# vim:ft=perl6
use Web::Tags;
say show {
html {
outs "hi";
ul {
for 1..10 {
li { $_ }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment