From 560432ad3b68a757311cb6f6929002e35f75804c Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Sat, 23 Nov 2024 21:16:31 +0100 Subject: [PATCH] Update website --- publi_bib.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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'])) {