Created
June 18, 2009 00:28
-
-
Save arodland/131621 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/perl | |
sub rewrite_link { | |
my ($orig) = shift; | |
if ($orig =~ /(.*)\.chm::(.*)/) { | |
my ($chmname, $path) = ($1, $2); | |
$chmname = lc $chmname; | |
$path =~ s#/#:#g; | |
$path =~ s/\.html//; | |
return "4c_help:$chmname:$path"; | |
} else { | |
return $orig; # Don't know what it is, don't touch it. | |
} | |
} | |
while (<>) { | |
my $out = ""; | |
while (1) { | |
if (/\G([^[]+)/gc) { # Look for a sequence of text that can't be a link and copy it verbatim | |
$out .= $1; | |
} elsif (/\G\[\[(.+?)(\||\])/gc) { # Look for the beginning of a link. | |
# Take any text starting with "[[" and ending at "|" or "]". | |
$out .= "[[" . rewrite_link($1) . $2; | |
} elsif (/\G\[/gc) { # Found a "[" that isn't starting a link, copy it to output | |
$out .= "["; | |
} elsif (/\G\Z/gc) { # Found end of line | |
last; | |
} else { | |
die "Can't happen"; | |
} | |
} | |
print $out; | |
} | |
# Usage: perl -pi.bak wikilink-rewrite.pl [files...] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment