﻿#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;

use open qw(:std :utf8);
# binmode STDIN, ':utf8';
# binmode STDOUT, ':utf8';
# binmode STDERR, ':utf8';
 
use integer;


# Song_lb-anzeigen.pl
# 2010-11-22
# Wolfgang Schmidle

# what does it do?
# input
# output


my $username = "wschmidle";



my $width = 21;


# text input

my @text;
while(<>) { push @text, $_; }


# go through the text

my $inP = 0;
my $pID = "";
my $containsReg = 0;
my $containsPlace = 0;
my $inSm = 0;
my $smallText = "";
my $strangeS = 0;
my $i = 0;
my $s = "";
my $absatzPb = "";
my $pb = "";
my $pbN = 0;
my $o = "";
my $zeile1 = "";
my $zeile2 = "";
my $lineNo = 0;

my $rohtext = "";

sub druckeMitZusatz {
	my $zusatz = shift;
	if ($zusatz) { $zusatz = "        ". $zusatz; }

	while ((length $zeile1) > $width) { 
		$lineNo++;
		$zeile1 =~ s!(.{$width})!!;
		$rohtext .= "$lineNo　$1\n"; 
		$zeile2 =~ s!(.{$width})!!;
		my $teil2 = $1;
		unless ($teil2 =~ m!^　+$!) {
			$rohtext .= "\x{2007}　$teil2\n";
		}
	}
	if ($zeile1) { 
		$lineNo++;
		$rohtext .= "$lineNo　$zeile1$zusatz\n";
		$zeile1 = "";
		unless ($zeile2 =~ m!^　+$!) {
			$rohtext .= "\x{2007}　$zeile2\n";
			$zeile2 = "";
		}
	}
}

sub drucke {
	druckeMitZusatz("");
}

sub addSmallTextToZeile {
	if ((length $smallText) % 2 == 1) { $smallText .= "。"; }
	my $realLength = (length $smallText) / 2;
	
	while (((length $zeile1) % $width) + $realLength > $width) {
		my $lineFillLength = $width - ((length $zeile1) % $width);
		$smallText =~ s!(.{$lineFillLength})(.{$lineFillLength})!!;
		$zeile1 .= $1;
		$zeile2 .= $2;
		$realLength -= $lineFillLength;
	}
	# beachte: pb in small
	
	$smallText =~ s!(.{$realLength})!!;
	$zeile1 .= $1;
	$zeile2 .= $smallText;
	$smallText = "";
}

sub addText {
	my $new = shift;

	if ($inSm) {
		$smallText .= $new; 
	} else {
		$zeile1 .= $new;
		$zeile2 .= "　" x (length $new);
	}		
}

sub newLineStartsWith {
	my $new = shift;

	$smallText = "";
	$zeile1 = "";
	$zeile2 = "";
	addText($new);
}

my $normalWidth = $width;
my $containsLb = 0;

foreach (@text) {

	$i++;
	
	if (m! file="X06\.01\.(\d\d\d[AB])"!) { 
		$pb = $1;
		m! n="(\d+)"!;
		$pbN = $1;
		$o = (m! o="([^"]+)"! ? ", $1" : "" );
		unless ($inP) { 
			$lineNo = 0;
			$rohtext .= "[$pb, $pbN$o]\n"; 
			next;
		}
	}

	if (s!<head[^>]*>(.+)</head>!$1!) {
	
		$lineNo++;
		s!<reg[^>]+>([^<]+)</reg>!$1!g;
		s!<smlb/>!!g;
		s!<emph style="sm">([^<]+)</emph>!($1)!g;
		s/ *<!--.+?-->//;
		$rohtext .= "\n\n$lineNo　$_\n";
		next;
	}
	
	if (m!<anchor !) {
		next;
	}

	if (m!<p[ >]!) { 
		$inP = 1; 

		$pID = (m!<p xml:id="([A-Z0-9]+)"! ? $1 : "");
		unless ($pID) {
			if ($text[$i] =~ m!<s xml:id="([A-Z0-9]+)"!) { 
				$pID = "[$1]";
			}
		}

		$containsReg = 0; 
		$containsPlace = 0; 
		$strangeS = 0;
		$zeile1 = "";
		$zeile2 = "";
		$inSm = 0;
		$absatzPb = $pb; 
		$rohtext .= "\n";
		if ($pID) { $rohtext .= "　　　　$pID\n"; } 

		my $j = $i;
		$containsLb = 0;
		while (! ($text[$j] =~ m!</p>!)) { 
			if ($text[$j] =~ m!<lb/>!) { $containsLb = 1; }
			$j++;
		}
		if ($containsLb) { $width = 100; }

		next; 
	}
	
	if (m!</p>!) { $
		inP = 0; 
		
		drucke;
		
#		if ($containsReg) {
#			$rohtext .= "(p mit reg)\n\n";
#		}
# 		if ($containsPlace) {
# 			$rohtext .= "(p mit place)\n\n";
# 		}
		if ($strangeS) {
			$rohtext .= "(seltsam geformter Absatz)\n\n";
		}

		$width = $normalWidth;

		next;
	}
	
	next unless $inP;


	# Hack für small text, solange <small> nicht erlaubt ist
	
	if (m!<small[ >]!) {
		$inSm = 1;
		$smallText = "";
		next;
	}
	if (m!<s>\(</s>!) {
		next;
	}
	if (m!<s>\)</s>!) {
		next;
	}
	if (m!</small>!) {
		$inSm = 0;
		addSmallTextToZeile;
		next;
	}

	next if (m!^\t+</s>!);
	
	# entferne Kommentarzeilen
	
	next if m/\t<!--/;
	
	# entferne Kommentare am Ende
	
	s/ *<!--.+?-->//;  # unnötig? auf die schnelle reingefrickelt
	
	
	# entferne tags

	if (s!<reg[^>]+>([^<]+)</reg>!$1!g) { $containsReg = 1; }
	if (s!<place[^>]+>([^<]+|[^<]*<lb/>[^<]*)</place>!$1!g) { $containsPlace = 1; }
	if (s!<place[^>]+>([^<]+|[^<]*<pb [^>]+>[^<]*)</place>!$1!g) { $containsPlace = 1; }
	if (s!<place[^>]+>!!g) { $containsPlace = 1; }
	if (s!</place>!!g) { $containsPlace = 1; }
	if (s!\(([^)]+)\)!$1!g) { $containsPlace = 1; }

	s!\[(.)[vVrR]{1,2}\]!$1!g; # entferne [ V] etc.
	s!<smlb/>!!g;
	

	if (m!<s[ >]!) {
		unless (m!</s>!) { s!\n!</s>\n!; }
	}
	if (m!</s>!) {
		unless (m!<s[ >]!) { s!^(\t+)!$1<s>!; }
	}
	


	# doofe Ausnahme 1: z.B.  <s xml:id="N401213">頃​刻​而​燥，

	if (m!<s[^>]*>([^<]+)\n!) {
		$s = $1;
		$s =~ s![。！？，、：；《》“”\x{200B}]!!g;
		$s =~ s![., ]!!g; 
		addText($s);
		next;
	}

	# doofe Ausnahme 1a: z.B.  頃​刻​而​燥，</s>

	if (m!\t+([^<\t]+)</s>\n!) {
		$s = $1;
		$s =~ s![。！？，、：；《》“”\x{200B}]!!g;
		$s =~ s![., ]!!g; 
		addText($s);
		next;
	}

	# doofe Ausnahme 2: z.B.  <s xml:id="N40079A">南​方​必​刈​<pb file="X06.01.020B" n="42"/>

	if (m!<s[^>]*>([^<]+)<pb file="X06\.01\.(\d\d\d[AB])" n="(\d+)"(?: o="[^"]+" o-norm="[^"]+")?/>\n!) {
		$s = $1;
		$pb = $2;
		$pbN = $3;
		$o = (m! o="([^"]+)"! ? ", $1" : "" );

		$s =~ s![。！？，、：；《》“”\x{200B}]!!g;
		$s =~ s![., ]!!g; 
		addText($s);

		druckeMitZusatz("<pb>");
		
		$rohtext .= "[$pb, $pbN$o]\n";

		$lineNo = 0;
		newLineStartsWith("");
		next;
	}
	
	# doofe Ausnahme 3: z.B.  <pb file="X06.01.022B" n="46"/>稻，</s>

	if (m!^\t+<pb file="X06\.01\.(\d\d\d[AB])" n="(\d+)"(?: o="[^"]+" o-norm="[^"]+")?/>([^<]+)</s>!) {
		$pb = $1;
		$pbN = $2;
		$s = $3;
		$o = (m! o="([^"]+)"! ? ", $1" : "" );

		$s =~ s![。！？，、：；《》“”\x{200B}]!!g;
		$s =~ s![., ]!!g; 

		druckeMitZusatz("<pb>");
		
		$rohtext .= "[$pb, $pbN$o]\n";

		$lineNo = 0;
		newLineStartsWith($s);
		next;
	}

	# case 1: <pb> on a separate line
	
	if (m!^\t+<pb file="X06\.01\.(\d\d\d[AB])" n="(\d+)"(?: o="[^"]+" o-norm="[^"]+")?/>!) {
	
		$pb = $1;
		$pbN = $2;
		$o = (m! o="([^"]+)"! ? ", $1" : "" );

		if ($inSm) { addSmallTextToZeile; }

		druckeMitZusatz("<pb>");
		
		$rohtext .= "[$pb, $pbN$o]\n";

		$lineNo = 0;
		newLineStartsWith("");
		next;
	}
	
	# <lb/> on a separate line
	
	if (m!^\t+<lb/>$!) {

		druckeMitZusatz("<lb>");
		
		newLineStartsWith("");
		next;
	}
	

	# Figures
	
	if (m!<figure!) {
		drucke;
		next;
	}
	if (m!<image!) { 
		next;
	}
	if (m!<caption>(.+?)</caption>!) {
		my $caption = $1;
		$caption =~ s!<lb/>!\n      !g;
		$rohtext .= "(cap: $caption)\n";
		next;
	}
	if (m!<description>(.+?)</description>!) {
		my $description = $1;
		$description =~ s!<lb/>!\n    !g;
		$rohtext .= "(d: $description)\n";
		next;
	}
	next if m!</figure>!;
	

	# aber keine weiteren Ausnahmen! 

	unless (m!<s[^>]*>(.+?)</s>!) { $strangeS = 1; }
	next if $strangeS;
	
	$s = $1;
	$s =~ s![。！？，、：；《》“”\x{200B}]!!g;
	
	# case 2: <s> with <pb>
	
	if  ($s =~ m!(.+)<pb file="X06\.01\.(\d\d\d[AB])"[^>]+>(.*)!) {
		
		my $vor = $1;
		$pb = $2;
		my $nach = $3;
		
		$s =~ m! n="(\d+)"!;
		$pbN = $1;
		$o = ($s =~ m! o="([^"]+)"! ? ", $1" : "" );
		
		$vor =~ s![., ]!!g; 

		addText($vor);		
		if ($inSm) { addSmallTextToZeile; }

		druckeMitZusatz("<pb>");
		
		$rohtext .= "[$pb, $pbN$o]\n";

		$lineNo = 0;
		newLineStartsWith($nach);
		next;
	}

	# case 3: <s> with <lb>
	
	if  ($s =~ m!(.+)<lb/>(.*)!) {
		
#		$containsLb = 1;

		my $vor = $1;
		my $nach = $2;
		
		$vor =~ s![., ]!!g; 

		addText($vor);
		if ($inSm) { addSmallTextToZeile; }
		
		druckeMitZusatz("<lb>");

		$nach =~ s![., ]!!g; 
		
		newLineStartsWith($nach);
		next;
	}
	
	# case 4: <s> without <pb> or <lb>
	
	$s =~ s![., ]!!g; 

	addText($s);
}

open (EVALUATE, ">/Users/$username/Desktop/pseudo-rohtext.txt") or die "Can't write the raw text!\n";
print EVALUATE $rohtext;
close(EVALUATE);

die "That's all.\n";


# TO DO:
# figures
# <reg>
# small text in Klammern; keine ultra-korrekte Anzeige; einfach korrekt weiterzählen: Länge (n+1)/2
# verwende <lb/>, wenn es da ist

# allgemein: ein neues o-norm darf das Skript nicht brechen
