Mercurial > hg > batmud > ggrtf
changeset 1627:a3c3591f9a74
Clean up this script.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 04 May 2018 20:24:01 +0300 |
parents | f2a165403c69 |
children | ea96ce334a5c |
files | docs/normalizeml.pl |
diffstat | 1 files changed, 30 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/docs/normalizeml.pl Fri Nov 04 15:41:49 2016 +0200 +++ b/docs/normalizeml.pl Fri May 04 20:24:01 2018 +0300 @@ -4,9 +4,11 @@ # Programmed by Matti 'ccr' Hamalainen <ccr@tnsp.org> # (C) Copyright 2007,2009 Tecnic Software productions (TNSP) # +use utf8; use strict; use warnings; + sub dorep($$) { my $str = $_[0]; @@ -15,6 +17,7 @@ return $str; } + my %xmlentities = (); my $entMode = 0; my $entData; @@ -23,36 +26,51 @@ binmode(STDIN, ":utf8"); binmode(STDOUT, ":utf8"); -while (<STDIN>) { - if (/<!ENTITY ([A-Za-z][A-Za-z0-9_]+) +SYSTEM +\"([^\"]*)\">/) { +while (defined(my $line = <STDIN>)) +{ + if ($line =~ /<!ENTITY ([A-Za-z][A-Za-z0-9_]+) +SYSTEM +\"([^\"]*)\">/) + { # Handle external entities my $name = $1; my $extfname = $2; local($/, *INFILE); - open(INFILE, "<", $extfname) or die("Could not open entity file '$extfname'.\n"); + open(INFILE, "<", $extfname) or die("Could not open entity file '".$extfname."'.\n"); $xmlentities{$name} = <INFILE>; close(INFILE); - } elsif (/<!ENTITY ([A-Za-z][A-Za-z0-9_]+) \"(.*?)\">/) { + } + elsif ($line =~ /<!ENTITY ([A-Za-z][A-Za-z0-9_]+) \"(.*?)\">/) + { # One-line entities $xmlentities{$1} = $2; - } elsif (/<!ENTITY ([A-Za-z][A-Za-z0-9_]+) \"(.*)$/) { + } + elsif ($line =~ /<!ENTITY ([A-Za-z][A-Za-z0-9_]+) \"(.*)$/) + { # Multi-line entities $entName = $1; $entData = $2; $entMode = 1; - } elsif ($entMode == 1) { - if (/^(.*)\">/) { + } + elsif ($entMode == 1) + { + if ($line =~ /^(.*)\">/) + { $entData .= $1; $xmlentities{$entName} = $entData; $entMode = 0; - } else { + } + else + { $entData .= $_; } - } else { + } + else + { # Expand entities for five levels at most - my $str = $_; - for (my $depth = 1; $depth < 5; $depth++) { - while (my ($k, $v) = each(%xmlentities)) { + my $str = $line; + for (my $depth = 1; $depth < 5; $depth++) + { + while (my ($k, $v) = each(%xmlentities)) + { $str =~ s/&$k;/$v/g; $str =~ s/&$k\s+([A-Za-z0-9 ]+);/dorep($v,$1)/eg; }