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


# Filter_2_06_check_underscores.pl
# 2010-06-22
# Wolfgang Schmidle

# what does it do?
# input
# output

# note that this script does not call Filter_3_04_replace_underscores.pl, but replicates it


# text input

my @filters = (
	"Filter_3_00_Schummelskript.pl",
	"Filter_3_01_replace_unknown_characters.pl",
	"Filter_3_02_replace_replacements.pl",
	"Filter_3_03_replace_escape_sequences.pl"
);

my $file = shift @ARGV;

$_ = shift @filters;
my $command = "perl $_ \"$file\"";
foreach (@filters) { $command .= " | perl $_"; }

my $result = `$command`;

my @text = split "\n", $result;


# go through the text

my $inText = 0;
my $i = 0;

foreach (@text) {

	$i++;
	
	# ignore "_" in the preamble
	
	$inText = 1 if m!<pb[ >]!;
	next unless $inText;


	s!_([^_¤¥]+)_!_¤$1_¥!g;
	
	if (m!_[^¤¥]!) { 
		s![¤¥]!!g;
		warn "Line $i : unmatched _: \n$_\n"; 
		next;
	}
	if (m!_¤ !) { 
		s![¤¥]!!g;
		warn "Line $i : left _ is followed by space: \n$_\n";
		next;
	}
	if (m! _¥!) { 
		s![¤¥]!!g;
		warn "Line $i : right _ is preceded by space: \n$_\n"; 
		next;
	}

	s!_¤([^_]+)_¥!<it>$1</it>!g;

	if (m!_!) { warn "Line $i contains _:\n$_\n"; }
}

die "That's all.\n";


# TO DO:
# wirklich auch Schritte 3.01, 3.02, 3.03 aufrufen, oder ist das unnötig?

# Das Skript 3.04 bricht ab, wenn es selbst schon erkennt, dass es ein Problem gibt.
# Das ist dort sinnvoll, aber hier sollten alle Probleme aufgelistet werden, anstatt bei einem 
# Problem ganz abzubrechen.
