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


# Filter_4_05_emph.pl
# 2010-04-14
# Wolfgang Schmidle

# what does it do?
# input
# output


# small and capital letters, taken from the <reg> script
my $klein = "a-zſæœèòäöüßó" . "ãẽĩõũꝰę";
my $gross = "A-ZÄÖÜÆŒ";
my $letter = $klein . $gross;


# text input

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


# go through the text

my @emph = ( 
	"it", # <it> is created from _ _; it doesn't occur in the raw text
	"bf", 
	"sc", 
	"sub",   #  <_> has been changed to <sub> in Filter_2_3_wellformed_xml
	"super",  #  <^> has been changed to <super> in Filter_2_3_wellformed_xml
	"ul", 
	"ol", 
	"red", 
	"sp",
	"sc"
);

my $nothing = "¥"; # ¥ is guaranteed not to occur in the text

my %neutral = ( 
	"it" => " ,-",
	"bf" => " ", 
	"sc" => " ,", 
	"sub" => " ",
	"super" => " ",
	"ul" => "$nothing", 
	"ol" => "$nothing", 
	"red" => " ", 
	"sp" => " ",
	"sc" => " "
);

my $i = 0;

foreach (@text) {
	
	$i++;

	# special case: small caps

	s!([$gross]+)<sc>(.+?)</sc>!<sc>$1\L$2\E</sc>!g;
	

	# turn tags into emph-styles

	foreach my $tag (@emph) {
		s!<$tag>(.+?)</$tag>!<emph style="$tag">$1</emph>!g;
	}

	# combine consecutive tags of the same type
	
	foreach my $tag (@emph) {
		s!</$tag>([$neutral{$tag}]+)<$tag>!$1!g;
	}
	
	# combine consecutive tags of different types

	# move <emph> as high as possible in the hierarchy: besser mit xslt?

}


# text output

print @text;


# TO DO:
# die Umwandlung in <emph> ist noch etwas brute force, insbesondere Verschachtelungen gehen schief
# combine consecutive tags of different types
# move <emph> as high as possible in the hierarchy: besser mit xslt?

# <it> in Absätzen in italics wird wieder upright

