dg-x/lib/Classy/Dialect/BlockParser.pm

141 lines
3.1 KiB
Perl
Raw Permalink Normal View History

package Classy::Dialect::BlockParser;
use Classy::Event::AsideBlock;
use Markdent::Regexes qw( :block );
use Markdent::Parser::BlockParser;
use Moose::Role;
with 'Markdent::Dialect::GitHub::BlockParser';
around _possible_block_matches => sub {
my $orig = shift;
my $self = shift;
my @look_for = $self->$orig();
unshift @look_for, 'aside_block';
return @look_for;
};
after parse_document => sub {
my $self = shift;
return if $self->_span_parser->_fn_list_count eq 0;
print('Dumping footnotes!') if $self->debug;
$self->_send_event(
'StartHTMLTag',
tag => 'div',
attributes => {
id => 'footnotes',
},
);
$self->_send_event('HorizontalRule');
$self->_send_event('StartOrderedList');
for my $fn_id (@{ $self->_span_parser->_note_idx_map }) {
my $fndata = $self->_span_parser->_get_fn_by_id($fn_id);
print("fn-> $fn_id = $fndata\n");
$self->_send_event(
'StartHTMLTag',
tag => 'li',
attributes => {
id => "fn:$fn_id",
}
);
$self->_span_parser->parse_block( "$fndata " );
$self->_send_event(
'StartLink',
uri => "#fnref:$fn_id",
);
# TODO: make this editable
$self->_send_event(
'HTMLTag',
tag => 'img',
attributes => {
src => '/assets/img/return.gif',
class => 'backbtn',
},
);
$self->_send_event('EndLink');
$self->_send_event(
'EndHTMLTag',
tag => 'li',
);
}
$self->_send_event('EndOrderedList');
$self->_send_event(
'EndHTMLTag',
tag => 'div',
);
};
sub _match_aside_block {
my $self = shift;
my $text = shift;
return unless ${$text} =~ / \G
$BlockStart
!!!
([\w-]+)? # optional extra class name
\n
( # alert block content
(?:.|\n)+?
\n # last newline required for _parse_text
)
!!!
\n
/xmgc;
my $inner = $2;
my $classes = "aside" . (defined $1 ? " $1" : "");
$self->_debug_parse_result(
$inner,
'aside block',
) if $self->debug;
$self->_send_event(
'StartHTMLTag',
tag => 'div',
attributes => {
class => $classes,
}
);
$self->_parse_text( \$inner );
$self->_send_event(
'EndHTMLTag',
tag => 'div',
);
return 1;
}
1;
__END__
=pod
=head1 DESCRIPTION
This role is similar to L<Markdent::Dialect::GitHub>, but adds parsing for
custom "aside" contexts used by datagirl.xyz.
=head1 ROLES
This role does the L<Markdent::Role::Dialect::BlockParser> role.
=head1 BUGS
We'll find out!
=cut