Created
January 23, 2016 08:22
-
-
Save n13i/2ae03a8a5bbcaf9c9adf 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 | |
use strict; | |
use warnings; | |
use utf8; | |
binmode STDIN, ':encoding(utf8)'; | |
binmode STDOUT, ':encoding(utf8)'; | |
my $path = $ARGV[0] || ''; | |
while(<STDIN>) | |
{ | |
my $line = $_; | |
$line =~ s/^#title\((.+)\)$/<!-- --- title: $1 -->/; | |
$line =~ s/^#contents$/[[_TOC_]]/; | |
$line =~ s/%%/~~/g; | |
$line =~ s/^([#]+\s.+?)\s\[#[^\]]+\]$/$1/; | |
$line =~ s/(\[\[(.+?)\]\])/&replace_link($1, $2)/eg; | |
$line =~ s/&ref\((.+?)\);/&replace_ref($1)/eg; | |
print $line; | |
} | |
sub replace_link | |
{ | |
my $outer = shift; | |
my $inner = shift; | |
if($inner =~ /^(.+?)\:(http.+)$/) | |
{ | |
return sprintf('[%s](%s)', $1, $2); | |
} | |
if($inner =~ /^(.+?)>(.+)$/) | |
{ | |
return sprintf('[[%s|%s]]', $1, $2); | |
} | |
return $outer; | |
} | |
sub replace_ref | |
{ | |
my $filename = shift; | |
`echo "$path/$filename" >> refs.txt`; | |
if($filename =~ /\.(png|jpg)$/) | |
{ | |
return sprintf('[[%s/%s]]', $path, $filename); | |
} | |
return sprintf('[%s](%s/%s)', $filename, $path, $filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment