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

Commit

Permalink
implement reference link
Browse files Browse the repository at this point in the history
  • Loading branch information
fperrad committed Mar 22, 2009
1 parent 866ea7f commit 8bbb8a7
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/Compiler.pir
Expand Up @@ -18,6 +18,9 @@ Markdown::HTML::Compiler implements a compiler for MAST nodes.
.sub '__onload' :anon :load :init
$P0 = get_hll_global 'P6metaclass'
$P0 = $P0.'new_class'('Markdown::HTML::Compiler')

$P0 = new 'Hash'
set_hll_global [ 'Markdown';'HTML';'Compiler' ], '%ref', $P0
.end

.sub 'to_html' :method
Expand Down Expand Up @@ -246,15 +249,29 @@ Return generated HTML for all of its children.
.sub 'html' :method :multi(_,['Markdown';'RefLink'])
.param pmc node
.local pmc code
$P0 = get_hll_global [ 'Markdown';'HTML';'Compiler' ], '%ref'
$S2 = node.'key'()
$S0 = downcase $S2
$P1 = $P0[$S0]
new code, 'CodeString'
$S0 = "<a href=\"\">"
$S1 = self.'html_children'(node)
if $S1 goto L1
$S1 = node.'key'()
$S0 = "<a href=\""
$S1 = $P1[0]
$S0 .= $S1
$S1 = $P1[1]
unless $S1 goto L1
$S0 .= "\" title=\""
$I0 = length $S1
$I0 -= 2
$S1 = substr $S1, 1, $I0
$S0 .= $S1
L1:
$S0 .= "\">"
$S1 = self.'html_children'(node)
if $S1 goto L2
$I0 = length $S2
$I0 -= 2
$S1 = substr $S2, 1, $I0
L2:
$S0 .= $S1
$S0 .= "</a>"
set code, $S0
Expand Down
16 changes: 16 additions & 0 deletions src/Node.pir
Expand Up @@ -70,6 +70,22 @@ for Markdown.
.end
.namespace [ 'Markdown';'Reference' ]
.sub 'insert' :method
.param string key
.param string url
.param string title
$P0 = get_hll_global [ 'Markdown';'HTML';'Compiler' ], '%ref'
$P1 = new 'FixedStringArray'
set $P1, 2
$P1[0] = url
$P1[1] = title
$S0 = downcase key
$P0[$S0] = $P1
.end
.namespace [ 'Markdown';'Title' ]
.sub 'level' :method
Expand Down
4 changes: 3 additions & 1 deletion src/parser/actions.pm
Expand Up @@ -256,7 +256,9 @@ method AutoLinkEmail($/) {
}

method Reference($/) {
make Markdown::Reference.new( );
my $mast := Markdown::Reference.new( );
$mast.insert( ~$<Label>.text(), ~$<RefSrc>.text(), ~$<RefTitle>.text());
make $mast
}

method Code($/) {
Expand Down
63 changes: 63 additions & 0 deletions t/32-reflink.t
@@ -0,0 +1,63 @@
#! perl
# Copyright (C) 2009, Parrot Foundation.
# $Id$

=head1 Markdown reference link
=head2 Synopsis
% perl t/32-reflink.t
=cut

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

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

language_output_is( 'markdown', <<'CODE', <<'OUT', 'reference link double' );
You can also put the [link URL][1] below the current paragraph like [this][2].
[1]: http://url
[2]: http://another.url "A funky title"
CODE
<p>You can also put the <a href="http://url">link URL</a> below the current paragraph like <a href="http://another.url" title="A funky title">this</a>.</p>

OUT

language_output_is( 'markdown', <<'CODE', <<'OUT', 'reference link simple' );
Or you can use a [shortcut][] reference, which links the text "shortcut"
[shortcut]: http://goes/with/the/link/name/text
CODE
<p>Or you can use a <a href="http://goes/with/the/link/name/text">shortcut</a> reference, which links the text "shortcut"</p>

OUT

language_output_is( 'markdown', <<'CODE', <<'OUT', 'reference link simple', todo => 'bug with quoted ref' );
Or you can use a [shortcut][] reference, which links the text "shortcut"
to the link named "[shortcut]" on the next paragraph.
[shortcut]: http://goes/with/the/link/name/text
CODE
<p>Or you can use a <a href="http://goes/with/the/link/name/text">shortcut</a> reference, which links the text "shortcut"
to the link named "[shortcut]" on the next paragraph.</p>

OUT


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

0 comments on commit 8bbb8a7

Please sign in to comment.