diff --git a/publi_bib.php b/publi_bib.php index fa38953..e1a9231 100644 --- a/publi_bib.php +++ b/publi_bib.php @@ -113,6 +113,48 @@ function expandJournalReferences($text) { } +function convertFirstNameToInitial($name) { + // Trim any whitespace + $name = trim($name); + + // Return empty string if name is empty + if (empty($name)) { + return ''; + } + + // Split the name into parts + $parts = preg_split('/\s+/', $name); + + // Process each part + $result = array_map(function($part) { + // Skip if part is empty + if (empty($part)) { + return ''; + } + + // If it's already an initial (single character followed by optional period) + if (preg_match('/^[A-Z]\.?$/', $part)) { + // Ensure it has a period + return rtrim($part, '.') . '.'; + } + + // If it's a hyphenated name, handle each part + if (strpos($part, '-') !== false) { + $hyphenParts = explode('-', $part); + $initials = array_map(function($p) { + return ucfirst($p[0]) . '.'; + }, $hyphenParts); + return implode('-', $initials); + } + + // Regular name: take first character, capitalize it, add period + return ucfirst($part[0]) . '.'; + }, $parts); + + // Join the parts back together + return implode(' ', array_filter($result)); +} + $m = setup_mustache(); @@ -124,6 +166,7 @@ $listener->addProcessor(new NamesProcessor()); $listener->addProcessor(static function (array $entry) { $entry['title'] = removeBoundingBraces($entry['title']); foreach ($entry['author'] as $i=>$v) { + $entry['author'][$i]['first'] = convertFirstNameToInitial($v['first']); $entry['author'][$i]['last'] = removeBoundingBraces($v['last']); } if (isset($entry['journal'])) {