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


# Filter_3_04_replace_underscores.pl
# 2010-04-04
# Wolfgang Schmidle

# This script replaces _ _ by <it> </it>.
# input
# output

# note that the "die" cases normally have been taken care of  in step 2.06


# text input

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


# go through the text

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

foreach (@text) {
	$i++;

	# ignore "_" before the first <pb>
	
	$inText = 1 if m!<pb[ >]!;
	next unless $inText;
	
	s!<_>!<sub>!g;
	s!</_>!</sub>!g;

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

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


# text output

print @text;


# TO DO:
# s!<_>!<sub>!g; etc. ist etwas krude; geth es auch eleganter?

