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

Commit

Permalink
implement code block
Browse files Browse the repository at this point in the history
  • Loading branch information
fperrad committed Mar 23, 2009
1 parent aa3989c commit 1fbf6a8
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Compiler.pir
Expand Up @@ -72,6 +72,30 @@ Markdown::HTML::Compiler implements a compiler for MAST nodes.
.return ($S0)
.end

.sub 'detab' :anon
.param string str
$P0 = split "\n", str
$P1 = new 'ResizableStringArray'
L1:
unless $P0 goto L2
$S0 = shift $P0
$S1 = substr $S0, 0, 1
unless $S1 == "\t" goto L3
$S0 = substr $S0, 1
goto L4
L3:
$S1 = substr $S0, 0, 4
unless $S1 == ' ' goto L4
$S0 = substr $S0, 4
L4:
push $P1, $S0
goto L1
L2:
str = join "\n", $P1
.return (str)
.end


=item html_children(node)

Return generated HTML for all of its children.
Expand Down Expand Up @@ -166,6 +190,24 @@ Return generated HTML for all of its children.
.return (code)
.end

=item html(Markdown::CodeBlock node)

=cut

.sub 'html' :method :multi(_,['Markdown';'CodeBlock'])
.param pmc node
$S1 = node.'text'()
$S1 = detab($S1)
$S1 = escape_code($S1)
.local pmc code
new code, 'CodeString'
$S0 = "<pre><code>"
$S0 .= $S1
$S0 .= "</code></pre>\n\n"
set code, $S0
.return (code)
.end

=item html(Markdown::BlockQuote node)

=cut
Expand Down
1 change: 1 addition & 0 deletions src/Node.pir
Expand Up @@ -19,6 +19,7 @@ for Markdown.

p6meta.'new_class'('Markdown::BlockQuote', 'parent'=>base)
p6meta.'new_class'('Markdown::Code', 'parent'=>base)
p6meta.'new_class'('Markdown::CodeBlock', 'parent'=>base)
p6meta.'new_class'('Markdown::Document', 'parent'=>base)
p6meta.'new_class'('Markdown::Emphasis', 'parent'=>base)
p6meta.'new_class'('Markdown::Email', 'parent'=>base)
Expand Down
4 changes: 4 additions & 0 deletions src/parser/actions.pm
Expand Up @@ -103,6 +103,10 @@ method RawLine($/) {
make Markdown::Line.new( :text( $/[0] ) );
}

method Verbatim($/) {
make Markdown::CodeBlock.new( :text( $/.text() ) );
}

method HorizontalRule($/) {
make Markdown::HorizontalRule.new();
}
Expand Down
14 changes: 14 additions & 0 deletions src/parser/grammar.pg
Expand Up @@ -19,6 +19,7 @@ token TOP {
token Block {
<.BlankLine>* [
| <BlockQuote> {*} #= BlockQuote
| <Verbatim> {*} #= Verbatim
| <Reference> {*} #= Reference
| <HorizontalRule> {*} #= HorizontalRule
| <Heading> {*} #= Heading
Expand Down Expand Up @@ -84,6 +85,19 @@ token BlockQuoteRaw {

token _Chevron { '>' }

token NonblankIndentedLine {
<!BlankLine> <.IndentedLine>
}

token VerbatimChunk {
<.BlankLine>* <.NonblankIndentedLine>+
}

token Verbatim {
<.VerbatimChunk>+
{*}
}

token HorizontalRule {
<.NonindentSpace> [
| '*' <.Sp> '*' <.Sp> '*' [ <.Sp> '*' ]*
Expand Down
76 changes: 76 additions & 0 deletions t/15-codeblock.t
@@ -0,0 +1,76 @@
#! perl
# Copyright (C) 2009, Parrot Foundation.
# $Id$

=head1 Markdown code block
=head2 Synopsis
% perl t/15-codeblock.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', 'CodeBlock 1' );
This is a normal paragraph:
This is a code block.
CODE
<p>This is a normal paragraph:</p>

<pre><code>This is a code block.
</code></pre>

OUT

language_output_is( 'markdown', <<'CODE', <<'OUT', 'CodeBlock 2' );
Here is an example of AppleScript:
tell application "Foo"
bell
end tell
CODE
<p>Here is an example of AppleScript:</p>

<pre><code>tell application "Foo"
bell
end tell
</code></pre>

OUT

language_output_is( 'markdown', <<'CODE', <<'OUT', 'CodeBlock HTML' );
HTML fragment:
<div class="footer">
&copy; 2004 Foo Corporation
</div>
CODE
<p>HTML fragment:</p>

<pre><code>&lt;div class="footer"&gt;
&amp;copy; 2004 Foo Corporation
&lt;/div&gt;
</code></pre>

OUT

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

0 comments on commit 1fbf6a8

Please sign in to comment.