Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code and comments mixed

Introduced a slight hack. Oh well, seems the lesser of two evils.
  • Loading branch information...
commit b0416b126b192347f624f7702fcf768772d89996 1 parent 567371b
@masak masak authored
Showing with 32 additions and 6 deletions.
  1. +18 −6 lib/Perl6/Literate.pm
  2. +14 −0 t/03-code-comments-mixed.t
View
24 lib/Perl6/Literate.pm
@@ -1,5 +1,7 @@
module Perl6::Literate;
+regex empty { ^ \s* $ }
+
sub convert($text) {
# There are six modes, 'start', 'code', 'comment', 'empty line after code'
# and 'empty line after comment'. The latter two are abbreviated '_code'
@@ -7,43 +9,43 @@ sub convert($text) {
# should treat all four modes somehow.
my $mode = 'start';
- return [~] gather {
+ my @p = gather {
for $text.comb(/\N*\n|\N+\n?/).kv -> $n, $line {
given $line {
when /^ '>'/ {
given $mode {
when 'code'|'_code'|'start'|'_start' {
- take ' ' ~ $line.substr(1);
+ # all clear, break here
}
when '_comment' {
take "=end Comment\n";
- take $line;
}
when 'comment' {
die "Must have empty line before code paragraph "
~ "on line $n";
}
}
+ take ' ' ~ $line.substr(1);
$mode = 'code';
}
- when /^ \s* $/ {
+ when /<empty>/ {
take $line;
$mode.=subst(/^_?/, '_');
}
default {
given $mode {
when 'comment'|'_comment' {
- take $line;
+ # all clear, break here
}
when 'start'|'_start'|'_code' {
take "=begin Comment\n";
- take $line;
}
when 'code' {
die "Must have empty line after code paragraph "
~ "on line $n";
}
}
+ take $line;
$mode = 'comment'
}
}
@@ -53,4 +55,14 @@ sub convert($text) {
take "\n=end Comment\n";
}
}
+
+ # This is slightly hackish, but still much preferrable to buffering
+ # lines and other unholy tricks.
+ for reverse 0..(@p - 2) -> $n {
+ if @p[$n] ~~ /<empty>/ && @p[$n+1] ~~ /'=end Comment'\n/ {
+ @p[$n], @p[$n+1] = @p[$n+1], @p[$n];
+ }
+ }
+
+ return [~] @p;
}
View
14 t/03-code-comments-mixed.t
@@ -0,0 +1,14 @@
+use v6;
+use Test;
+
+use Perl6::Literate;
+
+is Perl6::Literate::convert(q[[[Here is some text.
+
+> say 'OH HAI';]]]),
+ q[[[=begin Comment
+Here is some text.
+=end Comment
+
+ say 'OH HAI';]]],
+ 'comment and then code';
Please sign in to comment.
Something went wrong with that request. Please try again.