Created
August 18, 2011 12:52
-
-
Save masak/1154000 to your computer and use it in GitHub Desktop.
Get all snippets of Perl 6 code from Rosetta Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
my $OUTPUT_DIR = 'output'; | |
mkdir $OUTPUT_DIR; | |
my $table-of-contents = qx[wget -q -O- http://rosettacode.org/wiki/Category:Perl_6]; | |
my $interested = False; | |
for $table-of-contents.lines { | |
$interested = True if /'pages are in this category, out of'/; | |
$interested = False if /'</table>'/; | |
if $interested && /'<li><a href="/wiki/' (.*?) '" title="' .*? '">' (.*?) '<' / { | |
my $article_name = $0; | |
my $name = $1.lc; | |
$name.=subst(' ', '_', :g); | |
$name.=subst('+', '_plus_', :g); | |
$name.=subst('/', '__', :g); | |
$name.=subst(/'('|')'/, '', :g); | |
$name.=subst(q['], '', :g); | |
$name.=subst('_-_', '_', :g); | |
$name.=subst('_-', '-', :g); | |
$name.=subst('"', '', :g); | |
$name.=subst('brain****', 'brainfuck', :g); | |
$name.=subst('$', 'USD', :g); | |
$name.=subst(',', '', :g); | |
die "Unknown characters in $name" | |
unless $name ~~ /^ [\w | '-']+ $/; | |
my $url = "http://rosettacode.org/mw/index.php?title=$article_name\\&action=edit"; | |
my $article = qqx[wget -q -O- $url]; | |
my $PERL6_PREFIX = '<lang perl6>'; | |
my $PERL6_SUFFIX = '</lang>'; | |
my $capturing = False; | |
my $content = ""; | |
my $file_index = 1; | |
for $article.lines { | |
$capturing = True if /$PERL6_PREFIX/; | |
if $capturing { | |
$content ~= $_; | |
} | |
if $capturing && /$PERL6_SUFFIX/ { | |
$capturing = False; | |
$content.=subst($PERL6_PREFIX, ''); | |
$content.=subst($PERL6_SUFFIX, ''); | |
given open "$OUTPUT_DIR/$name-{$file_index++}", :w { | |
.say: $content; | |
.close; | |
} | |
$content = ""; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment