User:Cedars/inote.pl

The following Perl script is a hack for converting Wikipedia citations from the inote convention to the ref/note convention. The result is fed to standard output. The script handles both single-argument and double-argument inote tags. The scripts' work is quite obvious as citations are systematically labelled AR1, AR2 and so on unless a second argument is provided for the inote tag. Both inote tags and inote-explain tags should be completely removed from the output. The script is designed to assist the human editing of Wikipedia articles, not replace it. Please feel free to make changes to this page if you feel they would improve the script. If you have comments on the script please feel free to post them on the talk page.


 * 1) ! /usr/bin/perl -w


 * 1) inote.pl
 * 2) Converts Wikipedia citations from the inote convention to the ref/note convention.
 * 3) Result is fed to standard output.
 * 4) Public Domain 2005
 * 1) Public Domain 2005
 * 1) Public Domain 2005

if ($#ARGV != 0 || !($ARGV[0] && -f $ARGV[0])) { print "\ninote.pl\n\n"; print "Converts Wikipedia citations from the inote convention to the ref/note\nconvention. "; print "Result is fed to standard output.\n\n"; print "Usage:\tinote.pl target\n"; print "\ttarget - the text file to convert\n"; die("\n"); }
 * 1) Check that our arguments make sense, otherwise print usage

$path = $ARGV[0];
 * 1) Set the path file to reflect the target

open(FILE, $path); @input = ; close(FILE); $input_len = $#input + 1;
 * 1) Open the file

$output_len = 0;
 * 1) Create output

$notes[0] = "==Notes==\n"; $notes_len = 1; $AR_count = 1;
 * 1) Create initial notes part

$terminate = 0; for ($i = 0; !$terminate && $i < $input_len; $i++) { # Get the current line $curline = $input[$i]; # We have found the heading after the references so end the loop if ($references && $curline =~ /^\=\=[^\=]/) { $terminate_index = $i; $terminate = 1; }	else { # Check for inote tag and pull contents if ($curline =~ /\{\{inote\|([^\}^\|]*)\|?([^\}]*)\}\}/) {
 * 1) Go through all lines in the file

# Switch style depending on arguments if ($2 eq "") { # Revise input $input[$i] =~ s/{\{inote\|([^\}^\|]*)\|?([^\}]*)\}\}/\{\{ref\|AR$AR_count\}\}/; # Add to notes $notes[$notes_len] = "# ".$1."\n"; $notes_len++; $AR_count++; }			else { # Revise input $input[$i] =~ s/{\{inote\|([^\}^\|]*)\|?([^\}]*)\}\}/\{\{ref\|$2\}\}/; # Add to notes $notes[$notes_len] = "# ".$1."\n"; $notes_len++; }			# Delay the move (we might have multiple notes) $i--; }		else { # Add line unaltered to output $output[$output_len] = $curline; $output[$output_len] =~ s/\{\{explain-inote\}\}\n?//; $output_len++; }	}	# Note if we have found the references heading if ($curline =~ /^\=\=.*References.*\=\=/) { $references = 1; } }

if ($terminate) {
 * 1) If terminate is true we found the place to put our notes, otherwise just add them to the end

# Add in notes for ($i = 0; $i < $notes_len; $i++) { $output[$output_len] = $notes[$i]; $output_len++; }	# Add in a blank line $output[$output_len] = "\n"; $output_len++; # Fill out the rest for ($i = $terminate_index; $i < $input_len; $i++) { $output[$output_len] = $input[$i]; $output[$output_len] =~ s/\{\{explain-inote\}\}\n?//; $output_len++; } } else {

# Add in a blank line $output[$output_len] = "\n"; $output_len++; # Add in notes for ($i = 0; $i < $notes_len; $i++) { $output[$output_len] = $notes[$i]; $output_len++; }	# Add in a blank line $output[$output_len] = "\n"; $output_len++; }

for ($i = 0; $i < $output_len; $i++) { print $output[$i]; }
 * 1) Print end result