#!/usr/bin/perl -w

####################################################################################################
##      TibPagination.pl 
##          by Than Grove   05/30/11
##         Tibetan and Himalayan Library (www.thlib.org)
##
##      This program adds digital page and line milestone markers to a Unicode text file based on THL's TEI-based markup.
##
#################### Variables  ####################################################################
#
## params are 1. the text file that is to be paginated ending in .txt
#
# output is same file name ending in .xml
#
use Encode;
use utf8;

$inFile = $ARGV[0];               ## The file to be converted that is read in
$outFile = $inFile;              ## The file to which the paginated text is to be written
$outFile =~ s/\.txt/\.xml/;
print "Outfile is: $outFile\n";
## A list of all punctuation characters in Unicode to be used in a Reg Ex.
$punct = "\x{0F00}\x{0F01}\x{0F02}\x{0F03}\x{0F04}\x{0F05}\x{0F06}\x{0F07}";
$punct .= "\x{0F08}\x{0F09}\x{0F0A}\x{0F0B}\x{0F0C}\x{0F0D}\x{0F0E}\x{0F0F}";
$punct .= "\x{0F10}\x{0F11}\x{0F12}\x{0F13}\x{0F14}\x{0020}\x{00A0}\x{000D}\n";        
#$punct = decode_utf8($punct);

open (READIN,"<:utf8", $inFile) or die "Cannot open the infile ($inFile): $!\n";
open (OUT,">:utf8", $outFile) or die "Cannot open the outFile ($outFile): $!\n";

$isPunct = "off";
$sylCt = 0;
$lineCt = 0;
$spl = 20; # Syllables per line
$lpp = 15; # Lines per page
$totalPgs = 1;
$dpmark = '<milestone unit="digpage"/>';
$dlmark = '<milestone unit="digline"/>';

print OUT '<?xml version="1.0" encoding="UTF-8"?>';
print OUT '<div>';
print OUT $dpmark;

$isBeg = "on";

print "Converting. ";

while(my $char = getc READIN) {
	if($punct =~ /$char/ ) {
		if($isPunct ne "on") { 
			$isPunct = "on"; 
			if($isBeg eq "on") {
				$isBeg = "off";
			} else {
				$sylCt++;
			}
		}
	} elsif ($isPunct eq "on") {
		$isPunct = "off";
		# Add the milestone markup when appropriate
		if($sylCt == $spl) {
			$lineCt ++;
			if($lineCt == $lpp) {
				print OUT $dpmark;
				$lineCt = 0;
				$totalPgs++;
				print ". "
			} else {
				print OUT $dlmark;
			}
			$sylCt = 0;
		}
	}
	print OUT $char;
}

print OUT '</div>';

close READIN;
close OUT;
print "\nFinished! Total pages =  $totalPgs \n";