my %rulesets = (
# The "null" ruleset exists to force us into an initial scope
0 => {
can_enter =>
sub { },
on_enter =>
sub { },
maybe_emit_markup =>
sub { },
maybe_push_scope =>
sub { 1 },
maybe_exit_scope =>
sub { },
on_exit =>
sub { }
},
# The "Plain" (P) ruleset handles normal, unindented paragraphs
P => {
can_enter =>
sub { $indent == 0 },
on_enter =>
sub { },
maybe_emit_markup =>
sub {
if ( $type == $BLANK ) {
emit("
\n") if $prevtype == $BLANK;
}
elsif ( $type == $PLAIN ) {
emit("
") if $prevtype != $PLAIN; } }, maybe_push_scope => sub { $indent > 0 }, maybe_exit_scope => sub { 0 }, on_exit => sub { } }, # The "BQ" (Blockquote) ruleset handles indented paragraphs BQ => { can_enter => sub { $type == $PLAIN && $indent > $indentstack[-1] && ($indent - $indentstack[-1]) < 8 # hack for
},
on_enter =>
sub {
# emit("") if $prevtype == $BLANK;
emit("
\n");
$prevtype = 0; # prime pump for maybe_emit_markup
},
maybe_emit_markup =>
sub {
if ( $type == $BLANK ) {
# emit("
\n") if $prevtype == $BLANK;
}
elsif ( $type == $PLAIN ) {
emit("") if $prevtype == $BLANK; #TBD && $prevtype != 0;
}
},
maybe_push_scope =>
sub { $indent > $indentstack[-1] }, #TBD fix me for
maybe_exit_scope =>
sub {
$type == $BULLET ||
$type == $NUMBER ||
$indent < $indentstack[-1]
},
on_exit =>
sub { emit("
\n") }
},
# The "UL" (Unordered List) ruleset handles bullet lists
UL => {
can_enter =>
sub { $type == $BULLET &&
$indent > $indentstack[-1] &&
($indent - $indentstack[-1]) < 8 # hack for
},
on_enter =>
sub { emit("
},
on_enter =>
sub { emit("\n") },
maybe_emit_markup =>
sub {
emit("- ") if $type == $NUMBER;
if ( $type == $BLANK ) {
emit("
") unless $prevtype == $BLANK;
emit("
");
}
},
maybe_push_scope =>
sub { $indent > $indentstack[-1] },
maybe_exit_scope =>
sub {
$type == $BULLET ||
$indent < $indentstack[-1]
},
on_exit =>
sub { emit("
\n") }
},
# The "PRE" (Preformatted) ruleset handles preformatted text
PRE => {
can_enter =>
sub { $indent > $indentstack[-1] && $indent >= 8 }, #TBD hack
on_enter => sub { emit("") },
maybe_emit_markup =>
sub { emit(" " x ($indent - 8)) if $indent > 8 },
maybe_push_scope =>
sub { 0 },
maybe_exit_scope =>
sub { $indent < $indentstack[-1] },
on_exit =>
sub { emit("\n") },
}
);