From fea70590765e3b9f85694aa09f944f0399014c85 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 30 Sep 2018 19:22:48 +1300 Subject: [PATCH] apply title-casing (additive; doesn't lowercase any existing capital letters) --- shunt_tags | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/shunt_tags b/shunt_tags index d852963..9ba5425 100755 --- a/shunt_tags +++ b/shunt_tags @@ -229,6 +229,59 @@ function fixup($input) { // There's always a space before the tag $tmp = preg_replace('~([^ ])\[~', '\1 [', $tmp); + // Title-case for sections (delimited by -/~) + $fix_title_cases = function($str) { + + // First section: all words titlecase except the/and/or unless first word + $titlecase = function($str) { + $words = explode(" ", $str); + for($i = 0; $i < count($words); ++$i) { + if (strlen($words[$i]) == 0) { + continue; + } + + if ( + $i == 0 || + !in_array($words[$i], ['and', 'or', 'of', 'on', 'the', 'in', 'no', 'a']) + ) { + $words[$i][0] = strtoupper($words[$i][0]); + } + } + return implode(" ", $words); + }; + + // Second section: enforce first character uppercase if not already + $weaktitlecase = function($str) { + if (!ctype_upper($str[0])) { + $str[0] = strtoupper($str[0]); + } + return $str; + }; + + // Only process up to the first [ (if one exists) + $spos = strpos($str, '['); + $consider = substr($str, 0, $spos); + + $sections = preg_split('~( - | \~ )~', $consider, -1, PREG_SPLIT_DELIM_CAPTURE); + #var_dump($sections); + #die(); + for($i = 0; $i < count($sections); $i += 2) { + if (strlen($sections[$i]) == 0) { + continue; + } + + if ($i == 0) { + $sections[$i] = $titlecase($sections[$i]); + } else { + $sections[$i] = $weaktitlecase($sections[$i]); + } + } + return implode("", $sections).substr($str, $spos); + }; + $tmp = $fix_title_cases($tmp); + + // Special fixups: + // Fixup [Saiyan]BrollY, that's a sub group $tmp = str_replace('SaiyanBrollY', '[Saiyan]BrollY', $tmp);