Markdent:
Event-Driven Markdown Parsing

Dave Rolsky

What is Markdown?

Why is Markdown Important?

An Example

This is **bold**, this is *italic*.

* A list
* of items

> A blockquote which contains
> [a link](http://example.com)

Why a New Parser?

With Markdent You Can ...

But Wait, There's More!

What is "Event-Driven"?

What is "Event-Driven"?

Handlers Can Chain

           Parser
             |
             |
           Filter
             |
             |
         Multiplexer
          /       \
         /         \
Event Cache       HTML Output

Chained Handlers

my $buffer = q{};
open my $fh, '>', \$buffer;

my $capture = Markdent::Handler::CaptureEvents->new;
^ Event Cache
my $html = Markdent::Handler::HTMLStream( output => $fh );
^ HTML Output

my $multi = Markdent::Handler::Multiplexer->new(
    handlers => [ $capture, $html ],
);
^ Multiplexer

my $filter =
    Markdent::Handler::HTMLFilter->new( handler => $multi );
^ Filter

my $parser = Markdent::Parser->new( handler => $filter );
^ Parser

$parser->parse( markdown => ... );

Custom Dialect

package MyWiki::Dialect::SpanParser;

use Moose;
extends 'Markdent::Dialect::Standard::SpanParser';

overrides _possible_span_matches => sub {
    my $self = shift;
    my @look_for = super();

    # inside code span
    return @look_for if @look_for == 1;

    insert_after_string
        'code_start', 'wiki_link', @look_for;
    return @look_for;
};

Custom Dialect

package MyWiki::Dialect::SpanParser;

sub _match_wiki_link {
    my $self = shift;
    my $text = shift;

    return unless ${$text} =~ / ... /xmgc;

    my %p = ( link_text => $1 );
    $p{display_text} = $2
        if defined $2;

    my $event = $self->_make_event(
        'MyWiki::Event::WikiLink' => %p );

    $self->_markup_event($event);

    return 1;
}

In the Box - Standard Dialect

In the Box - GitHub Dialect

In the Box - Theory Dialect

+------+-------------+-----------------------+--------+
| id   | name        | description           | price  |
+------+-------------+-----------------------+--------+
|    1 | gizmo       | Frabbles the blatzer  |   1.99 |
|    2 | doodad      | Collects *gizmos*     |  23.80 |
|   10 | dojigger    | Foo                   | 102.98 |
| 1024 | thingamabob | Self-explanatory, no? |   0.99 |
+------+-------------+-----------------------+--------+

In the Box - Handlers

In the Box - Simple HTML Output

use Markdent::Simple::Document;

my $msd  =
    Markdent::Simple::Document->new;

my $html = $msd->markdown_to_html(
    title    => 'My Document',
    markdown => $markdown,
);

Thank you