User:Cedars/inote.pl

From Wikipedia, the free encyclopedia

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.

#! /usr/bin/perl -w

# inote.pl
#
# Converts Wikipedia citations from the inote convention to the ref/note convention.
# Result is fed to standard output.
#
# Public Domain 2005

# Check that our arguments make sense, otherwise print usage
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");
}

# Set the path file to reflect the target
$path = $ARGV[0];

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

# Create output
$output_len = 0;

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

# Go through all lines in the file
$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\|([^\}^\|]*)\|?([^\}]*)\}\}/) {

                        # Switch style depending on arguments                   
                        if ($2 eq "") {
                                
                                # Revise input
                                $input[$i] =~ s/{\{inote\|([^\}^\|]*)\|?([^\}]*)\}\}/\{\{ref\|AR$AR_count\}\}/;
                                
                                # Add to notes
                                $notes[$notes_len] = "# {{note|AR".$AR_count."}} ".$1."\n"; 
                                $notes_len++;
                                $AR_count++;
                                
                        }
                        else {
                        
                                # Revise input
                                $input[$i] =~ s/{\{inote\|([^\}^\|]*)\|?([^\}]*)\}\}/\{\{ref\|$2\}\}/;
                                
                                # Add to notes
                                $notes[$notes_len] = "# {{note|".$2."}} ".$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 is true we found the place to put our notes, otherwise just add them to the end
if ($terminate) {

        # 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++;
}

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