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


# Filter_3_06_make_tags_wellformed.pl
# 2010-04-06
# Wolfgang Schmidle


# text input

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


# read default parameters

my $filter = "mpdl-workflow-settings.pl";
my $result = `perl $filter`;

my $despecs = ($result =~ m! despecs=([^ ]+) ! ? $1 : "" );


# read <parameters>

my %unknown = ();

my $inParameters = 0;

foreach (@text) {
	last if m!</metadata>!;	
	if (m!<parameters>!) { $inParameters = 1; }
	if ($inParameters) {
		if (m![\t ,>]despecs *= *([^ ,\n<]+)!) { $despecs = $1; }
	}
}


# go through the text

foreach (@text) {

	# reserved characters in XML
	
	s!&!&amp;!g;
	s!&amp;amp;!&amp;!g;

	# correct syntax for attributes
	
	s!<([a-z]+) it>!<$1 style="it">!g;
	s!<([a-z]+) fr>!<$1 style="fr">!g;
	s!<pb ([^=>]+)>!<pb o="$1">!g; # <pb 25>[0037] --> <pb o="25">[0037]
	
	s!<([a-z]+) ita>!<$1 xml:lang="it">!g;  # (note the change from ita to it)
	s!<([a-z]+) fra>!<$1 xml:lang="fr">!g;  # (note the change from fra to fr)
	
	s!<n ([^=>]+)>!<n symbol="$1"/>!g;
	s!<mgl ([^=>]+)>!<mgl symbol="$1">!g; # was passiert bei <mgl it a> ?
	s!<mgr ([^=>]+)>!<mgr symbol="$1">!g; # was passiert bei <mgl it a> ?
	s!<fn ([^=>]+)>!<fn symbol="$1">!g; # was passiert bei <mgl it a> ?
	
	# add / in empty elements

	s!<pb>!<pb/>!g; # <pb o="25">[0037] --> <pb o="25"/>[0037]
	s!<pb ([^>/]+)>!<pb $1/>!g;
	s!<hd>!<hd/>!g;
	s!<fig>!<fig/>!g if $despecs eq "1.1.2";
	s!<gap>!<gap/>!g;

	# change the names of some elements
	
	 # part 1: element names that have to be changed in oder to make the XML wellformed
	s!<\^>!<super>!g;  # <super> will become <emph style="super"> later on
	s!</\^>!</super>!g;
	s!<_>!<sub>!g;  # <sub> will become <emph style="sub"> later on
	s!</_>!</sub>!g;
	s!<(\d\d\d)>!<unknown code="$1"/>!g;
	s!<\?>!<unsure/>!g;
	s!<\!>!<wrong/>!g;
	
	# part 2: elements whose names differ in the DESpecs and the ECHO schema
	s!<unknown !<de:unknown !g;
	
	s!<hd/>!<handwritten/>!g;

	s!<h>!<head>!g;
	s!<h ([^>]+)>!<head $1>!g;
	s!</h>!</head>!g;

	s!<ind([ >])!<div type="index" level="0" n="0"$1!g;
	s!</ind>!</div>!g;
	s!<toc([ >])!<div type="toc" level="0" n="0"$1!g;
	s!</toc>!</div>!g;

	s!<tb([ >])!<div type="table" level="0" n="0"$1!g; # tables cannot be handled as xhtml here yet
	s!</tb>!</div>!g;

	s!<fig>!<figure>!;
	s!</fig>!</figure>!;
	s!<fig/>!<figure/>!;
	s!<cap([ >])!<caption$1!;
	s!</cap>!</caption>!;
	s!<desc([ >])!<description$1!;
	s!</desc>!</description>!;
	s!<var([ >])!<variables$1!;
	s!</var>!</variables>!;   # (assuming it's inside <figure> and not a variable)
}


# text output

print @text;

# TO DO:
# Attribute: # in <p>, Zahl in <col>, 
# irgendwas ohne Leerzeichen in <mgl>, <mgr>, <fn>,<ac>, <n>,
# irgendwas in <r>

# verschachtelte <emph>

# z.B: <fig> --> <fig/> --> <figure/> wirklich in mehreren getrennten Schritten?
# stattdessen nach Modulen geordnet?

# in späteren Texten muss man <fig> und <fig/> unterscheiden

# überlegen: Elemente umbenennen eleganter als xsl-Skript?

# Attribute in Listen statt alle Fälle explizit durchzugehen? 
# dann: unterscheide zwischen it (steht in den DESpecs) und ita (steht nicht in den DESpecs)

# Mehrere Attribute: <h it ita>, <mgl it a> 

# Wenn ein tag in den raw text eingefügt wurde, wie zum Beispiel <num value="1758">CIↃIↃCCLVIII</num>,
# soll das Skript das nicht ändern. Erkennungszeichen ist wohl, dass in <num value="1758"> ein "=" ist.
# (Allerdings würde dieses Beispiel sowieso nicht verändert werden.)

