#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
use integer;

$| = 1;

require $ENV{'ECHO_SCRIPTS_DIR'} . "/share/common_functions.pl";
consoleStatus('Inserting semantic units.');

# Filter_4_04_insert_s.pl
# version 2010-07-05
# Wolfgang Schmidle


my $indent = "  ";
my $satzzeichen = "[.:;?]";
my $xml_entities = "\&(amp|lt|gt|quot|apos)\;"; # escape xml entities
my $unicodepoint = "\&#[0-9A-Fa-f]{1,4}\;";         # escape Unicodepoints

my $filtername = "Filter_4_04_insert_s.pl";

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

# go through the text

my $inP = 0;

foreach (@text) {

	if (/<p[ >]/) { $inP = 1; }
	next unless $inP;
	
	# if (m!<s xml:space=\"preserve\">!) { die "$filtername: Es gibt bereits <s xml:space=\"preserve\"> im Text: $_\n"; }
	# if (m!<s[^>]*>!) { die "$filtername: Es gibt bereits <s xml:space=\"preserve\"\> im Text: $_\n"; }
	# if (m!<s[^ui>]*>!) { printlnInERROR("$filtername: Es gibt bereits <s xml:space=\"preserve\"\> im Text: $_\n"); }
        # if whitespace comes after first s, we might be sure that there are already s tags inside
        if (m!<s\s.*?>!) { printlnInERROR("$filtername: There is at least one semantic unit (<s xml:space=\"preserve\"\>) already marked up: $_\n"); }

	# Fehler:
	# immer noch </s> nach &amp;
	# <lb/> am Anfang und nicht am Ende: Modell der Eipo-Version übernehmen!
	#   dann: Liste von Dingen, die der Satzende-Marker überspringen soll, z.B. Komma, <lb/>, Klammern, etc.
	#   und zwar alle diese Dinge, egal wieviele und in welcher Reihenfolge. Danach ¥¥ --> ¥
	# (deniq; sollte schon ein <reg> haben)
	# Komma am Anfang:  vis (Illustri ſsimi &amp; *<lb/>Saptentiſſimi DD.)*, ac 

	# markiere Satzzeichen, die kein Satzende sind
	1 while s!(<[^>]*$satzzeichen)([^¥>][^>]*>)!$1¥$2!; # Satzzeichen innerhalb von < >, z.B. in <de:unknown code="010"/>
	# s!($xml_entities[^¥])!$1¥!g; # &amp;
	s!($xml_entities)!$1¥!g; # &amp;
	s!($unicodepoint)!$1¥!g; # &amp;

	# -  main
	s!<p>!<p>\n$indent<s xml:¥space=\"preserve\">!;
	s!(<p [^>]+>)!$1\n$indent<s xml:¥space=\"preserve\">!;
	
	s!($satzzeichen)([^¥])!$1</s><s xml:space=\"preserve\">$2!g;

	# # -  corrections
	# s!($satzzeichen)</s><s xml:¥space=\"preserve\">_!$1_</s><s xml:¥space=\"preserve\">!g;  # (the underscore _ should be made into italics!)
	s!</s><s xml:¥space=\"preserve\">\)!)</s><s xml:¥space=\"preserve\">!g; # move closing parenthesis into the previous sentence
	s!</s><s xml:¥space=\"preserve\"> ! </s><s xml:¥space=\"preserve\">!g;
	
	s!</s><s xml:¥space=\"preserve\"><lb/>!<lb/></s><s xml:¥space=\"preserve\">!;
	
	# entferne das Hilfszeichen wieder
	s!¥!!g;
	
	# vor </s> auch am Zeilenende ein space, außer beim letzten </s> direkt vor <p/>
	
	# -  final corrections
	#s!($satzzeichen)</p>!$1</s></p>!;  # überflüssig??
	s#(?<!</s>)</p>#</s></p>#;
	s!<s xml:space=\"preserve\"></p>!</p>!;

	s!</p>!\n</p>!;
	s!</s><s xml:space=\"preserve\">!</s>\n$indent<s xml:space=\"preserve\">!g;
	
	if (m!</p>!) { $inP = 0; }
}


# geänderten Text ausgeben

# print @text;
printInOutputTextFile(@text);
consoleStatus('Finished.');

# TO DO:
# nach welchem Kriterium werden in notes <s> eingefügt oder nicht?
# Vorschlag: einzeilige note, oder wenn das Ergebnis ein einziger <s> wäre: kein <s>
# da kann man sich aber doch fragen, warum ist das in <p> anders, der bekommt immer <s>,
# auch wenn es nur ein einziges ist. Also vielleicht:
# einzeilige Marginalie: kein <s>. Wäre geradliniger.

# statt Parametern auch vielleicht erstmal mehrere <s>-Skripte?

# Fehler
# kein Satzende bei -que in allen Variationen, zum Beispiel q́; 
# <s>EA Virtutis est vis (Illustri ſsimi &amp; </s><s><lb/>Saptentiſſimi DD.)</s>

# das Skript sollte die Sprache berücksichtigen für den gesamten Text / jeweiligen Absatz

# wenn es noch keine <reg> im Text gibt, sollte q; wie in deniq; kein Satzende bewirken
