-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess-md.pl
executable file
·50 lines (46 loc) · 1.35 KB
/
process-md.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env perl
BEGIN { $^I = ""; }
sub fix_hash {
my ($a) = @_;
$a =~ s/_//g;
$a =~ tr/[A-Z ]/[a-z\-]/;
return $a;
};
sub fix_anchor {
my ($a) = @_;
$a =~ s{(#[-.\w\d ]*)}{fix_hash($1)}e;
return $a;
};
sub fix_page {
my ($a) = @_;
$a =~ tr/ /-/;
unless ($a =~ m{^https?://}) {
unless ($a =~ /\.md/) {
if (/#/) {
$a =~ s/(.)#/$1.md#/;
} else {
$a =~ s/(.)$/$1.md/;
}
}
$a =~ s{^([^.][^:]*)(:)}{./$1$2};
}
return fix_anchor($a);
};
LINE: while (defined($_ = readline ARGV)) {
s/\{\{/{% raw %}{{{% endraw %}/g unless /\% raw \%.*\% endraw \%/;
$backticks++ if (/\`\`\`/);
next if $backticks % 2;
s{\]\(\.\./}{](./}g;
s{(\[[^\]]*\])(\(#[^)]*\))}{"$1".fix_anchor($2)}ge;
s{(?<!\\)\[([^\]]*)\]\(((?:(?!://)[^)])*)\)}{"[$1](".fix_page($2).")"}ge;
s{\[\[([^\]|]*?)\|((?:(?!://)[^)])*?)\]\]}{"[$1](".fix_page($2).")"}ge;
s{\[\[([^\]|]*?)\|([^\]]*?)\]\]}{[$1]($2)}g;
s{\[\[([^\]|]*?)\]\]}{"[$1](".fix_page($1).")"}ge;
s{\]\(https://github.com/check-spelling/check-spelling/wiki/([^\)]+)\)}{"\]\(".fix_page($1).")"}ge;
s{https://github\.com/([^/\]\)]+/[^/\]\)]+)/blob/([^/\]\)]+/[^\)"#]*)($|\s|[\)"])}{https://raw.githubusercontent.com/$1/$2$3};
s{^[-*] \[x\](\s*\S.*)}{☑$1\n};
s{^[-*] \[ \](\s*\S.*)}{☐$1\n};
}
continue {
die "-p destination: $!\n" unless print $_;
}