azatoth / idr2ldf.pl secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Script to convert inline defined references in wikitext to list defined references, are pretty brute force and might not work on all pages :)

View idr2ldf.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
#!/usr/bin/perl
 
use v5.10;
use File::Slurp;
use List::Util qw(max);
use strict;
 
my $data = read_file($ARGV[0]);
my $c=0;
my %l;
 
if( my @names = ($data =~ m/ref-(\d+)/g) ) {
$c = max(@names);
}
 
my ($body,$tail)=split/(?:==References==)/i, $data;
 
 
sub unnamed_ref {
my $ref=shift;
my $n="ref-".(++$c);
$l{$n}="<ref name=\"$n\">$ref</ref>";
"<ref name=\"$n\"/>";
}
sub named_ref{
my $ref=shift;
my $n=shift;$l{$n}="<ref name=\"$n\">$ref</ref>";
"<ref name=\"$n\"/>";
}
sub reflist{
my $p=shift;
"{{$p\|refs=\n".join("\n",values%l);
}
sub references{
"<references>\n".join("\n",values%l)."</references>";
}
 
$body=~s{<ref name="(\S*?)">(.*?)</ref>}{named_ref($2, $1)}sge;
$body=~s{<ref>(.*?)</ref>}{unnamed_ref($1)}sge;
 
$tail=~s<{{(Reflist.*?)\|refs=><reflist($1)>sge;
$tail=~s<{{(Reflist\|\d+em)}}><reflist($1)."}}">sge;
$tail=~s{(<references\s*/>)}{references()}sge;
 
print $body.$tail;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.