apply title-casing (additive; doesn't lowercase any existing capital letters)

This commit is contained in:
mappu 2018-09-30 19:22:48 +13:00
parent 1aaa02e3a6
commit fea7059076

View File

@ -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);