#! c:/perl/bin/perl # # Filename: endnote.pl # Author: Eric Pement - pemente [=at=] northpark.edu # Version: 1.3 # Date: Copyleft 2002 by Eric Pement. Revised 2005-06-18. # Purpose: To convert in-text notes and references to endnotes # Requires: Perl5; blank lines between paragraphs of input file # # Usage: perl [-s] endnote.pl [-options] source.txt >target.txt # Options: -start=7 # begin numbering at note #7 # -blank # do not delete blank lines after note blocks # # Sample: perl -s endnote.pl -start=19 docfile.txt >docfile_new.txt # # Output: Plain ASCII text. Can be modified for HTML. # See also: ENDNOTE.TXT, which further explains usage and application. # http://www.pement.org/perl/endnote_v13.txt # # Credits: The idea for this endnote system was borrowed from "wsNOTE" # by Eric Meyer, a 1988 MS-DOS utility for managing footnotes # in WordStar files. His program offered many extra features. use strict; use vars qw/$start $blank/; # use warnings; # use diagnostics; my $a; my $b; my $c = ""; if (defined $start and $start !~ /^\d+$/ ) { print "\aError!\nVariable 'start' was set as $start." . "It must be a numeric value!"; exit; } $a = $b = (defined $start ? $start - 1 : 0); LINE: while (<>) { chomp; if ( /^\[\[\s*$/ .. /^]]\s*$/ ) { # If between [[ and ]] markers .. next if /^\[\[\s*$/; # .. skip the [[ marker next if /^(\.\.|\?\?)/; # .. skip comment lines, ^.. or ^?? if ( $blank and /^]]\s*$/ ) { # Old behavior: on ']]', just kill next LINE; # the current line. } elsif (/^]]\s*$/) { # New default: On ']]' delete this <>; # line and the next one, too. next; } s/##(?=\.)/++$a/ge; # .. increment $a or '##.' in block $c .= $_ . "\n"; # .. store line in the accumulator. } else { # Else, not in the note block .. s/\[##]/"[" . ++$b . "]"/ge; # .. so increment $b or '[##]' print $_ . "\n"; # .. and print that line. } } if ( $a != $b ) { # error-checking. Are $a and $b equal? my $bdy = ($start ? $b - $start : $b ); my $nts = ($start ? $a - $start : $a ); print STDERR "\a\a"; print "\n\n\n================\n WARNING!\n================\n"; print "Body text numbers and Endnote numbers don't match!\n"; print "You have $bdy notes in the Body text and $nts notes\n"; print "in the Endnote section.\n"; print "The Endnote section will not be printed.\n\n\n"; } else { print "\n-----------\nENDNOTES:\n\n", $c , "[end of file]\n"; } #---end of script---