Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
implement automatic links (URL & Email)
  • Loading branch information
fperrad committed Mar 21, 2009
1 parent a81d337 commit 27294d1
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Compiler.pir
Expand Up @@ -38,6 +38,12 @@ Markdown::HTML::Compiler implements a compiler for MAST nodes.
.return (str)
.end

.sub 'obscure_text' :anon
.param string str
# TODO
.return (str)
.end

=item html_children(node)

Return generated HTML for all of its children.
Expand Down Expand Up @@ -206,6 +212,52 @@ Return generated HTML for all of its children.
.return (code)
.end

=item html(Markdown::Link node)

=cut

.sub 'html' :method :multi(_,['Markdown';'Link'])
.param pmc node
.local pmc code
new code, 'CodeString'
$S0 = "<a href=\""
$S1 = node.'url'()
$S0 .= $S1
$S1 = node.'title'()
unless $S1 goto L1
$S0 .= "\" title=\""
$S0 .= $S1
L1:
$S0 .= "\">"
$S1 = node.'text'()
$S0 .= $S1
$S0 .= "</a>"
set code, $S0
.return (code)
.end

=item html(Markdown::Email node)

=cut

.sub 'html' :method :multi(_,['Markdown';'Email'])
.param pmc node
.local pmc code
new code, 'CodeString'
$S0 = "<a href=\""
$S1 = node.'text'()
$S1 = 'mailto:' . $S1
$S1 = obscure_text($S1)
$S0 .= $S1
$S0 .= "\">"
$S1 = node.'text'()
$S1 = obscure_text($S1)
$S0 .= $S1
$S0 .= "</a>"
set code, $S0
.return (code)
.end

=item html(Markdown::Emphasis node)

=cut
Expand Down
17 changes: 17 additions & 0 deletions src/Node.pir
Expand Up @@ -21,10 +21,12 @@ for Markdown.
p6meta.'new_class'('Markdown::Code', 'parent'=>base)
p6meta.'new_class'('Markdown::Document', 'parent'=>base)
p6meta.'new_class'('Markdown::Emphasis', 'parent'=>base)
p6meta.'new_class'('Markdown::Email', 'parent'=>base)
p6meta.'new_class'('Markdown::Entity', 'parent'=>base)
p6meta.'new_class'('Markdown::HorizontalRule', 'parent'=>base)
p6meta.'new_class'('Markdown::ItemizedList', 'parent'=>base)
p6meta.'new_class'('Markdown::Line', 'parent'=>base)
p6meta.'new_class'('Markdown::Link', 'parent'=>base)
p6meta.'new_class'('Markdown::ListItem', 'parent'=>base)
p6meta.'new_class'('Markdown::OrderedList', 'parent'=>base)
p6meta.'new_class'('Markdown::Para', 'parent'=>base)
Expand All @@ -42,6 +44,21 @@ for Markdown.
.end
.namespace [ 'Markdown';'Link' ]
.sub 'title' :method
.param pmc value :optional
.param int has_value :opt_flag
.tailcall self.'attr'('title', value, has_value)
.end
.sub 'url' :method
.param pmc value :optional
.param int has_value :opt_flag
.tailcall self.'attr'('url', value, has_value)
.end
.namespace [ 'Markdown';'Title' ]
.sub 'level' :method
Expand Down
17 changes: 17 additions & 0 deletions src/parser/actions.pm
Expand Up @@ -212,6 +212,23 @@ method StrongUI($/) {
make $mast;
}

method Link($/, $key) {
make $( $/{$key} );
}

method AutoLink($/, $key) {
make $( $/{$key} );
}

method AutoLinkUrl($/) {
make Markdown::Link.new( :text( $/[0].text() ),
:url( $/[0].text() ) );
}

method AutoLinkEmail($/) {
make Markdown::Email.new( :text( $/[0].text() ) );
}

method Code($/) {
make Markdown::Code.new( :text( $/[0].text() ) );
}
Expand Down
22 changes: 22 additions & 0 deletions src/parser/grammar.pg
Expand Up @@ -184,6 +184,7 @@ token Inline {
| <Space> {*} #= Space
| <Strong> {*} #= Strong
| <Emph> {*} #= Emph
| <Link> {*} #= Link
| <Code> {*} #= Code
| <Entity> {*} #= Entity
| <EscapedChar> {*} #= EscapedChar
Expand Down Expand Up @@ -288,6 +289,27 @@ token StrongUI {
{*}
}

token Link {
| <AutoLink> {*} #= AutoLink
}

token AutoLink {
| <AutoLinkUrl> {*} #= AutoLinkUrl
| <AutoLinkEmail> {*} #= AutoLinkEmail
}

token AutoLinkUrl {
'<' ( <[A..Za..z]>+ '://' [ <!Newline> <!_Gt> . ]+ ) '>'
{*}
}

token AutoLinkEmail {
'<' ( <[\-A..Za..z0..9+_]>+ '@' [ <!Newline> <!_Gt> . ]+ ) '>'
{*}
}

token _Gt { '>' }

token Ticks1 { '`' }
token Ticks2 { '``' }
token Ticks3 { '```' }
Expand Down
45 changes: 45 additions & 0 deletions t/30-autolink.t
@@ -0,0 +1,45 @@
#! perl
# Copyright (C) 2009, Parrot Foundation.
# $Id$

=head1 Markdown emphasis
=head2 Synopsis
% perl t/30-autolink.t
=cut

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../../lib", "$FindBin::Bin";

use Parrot::Test tests => 2;
use Test::More;

language_output_is( 'markdown', <<'CODE', <<'OUT', 'autolink url' );
<http://someurl>
CODE
<p><a href="http://someurl">http://someurl</a></p>

OUT

language_output_is( 'markdown', <<'CODE', <<'OUT', 'autolink email' );
<somebbob@example.com>
CODE
<p><a href="mailto:somebbob@example.com">somebbob@example.com</a></p>

OUT


# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4:

0 comments on commit 27294d1

Please sign in to comment.