From d8a649e6f7f86f49898604b88aa043cb049739f4 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Sat, 14 Oct 2017 23:12:16 +0200 Subject: [PATCH] Don't lower meta data unsolicited and flip meta headers array Don't lower unregistered meta headers on the first level unsolicited (e.g. `SomeNotRegisteredKey: foobar` in the YAML Frontmatter should result in `['SomeNotRegisteredKey']`, not `['somenotregisteredkey']`). Furthermore, Pico no longer compares registered meta headers in a case-insensitive manner. However, you can now register multiple search strings that are used to find a registered meta header. This is achieved by flipping the meta headers array: Pico 2.0 uses the array key to search for a meta value and the array value to store the found meta value. Previously it was the other way round (what didn't make much sense...). --- lib/Pico.php | 38 +++++++++++++++----------------------- plugins/DummyPlugin.php | 4 ++-- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index 37c6a27..4a32354 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -1199,12 +1199,12 @@ class Pico { if ($this->metaHeaders === null) { $this->metaHeaders = array( - 'title' => 'Title', - 'description' => 'Description', - 'author' => 'Author', - 'date' => 'Date', - 'robots' => 'Robots', - 'template' => 'Template' + 'Title' => 'title', + 'Description' => 'description', + 'Author' => 'author', + 'Date' => 'date', + 'Robots' => 'robots', + 'Template' => 'template' ); $this->triggerEvent('onMetaHeaders', array(&$this->metaHeaders)); @@ -1254,27 +1254,19 @@ class Pico $pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n" . "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s"; if (preg_match($pattern, $rawContent, $rawMetaMatches) && isset($rawMetaMatches[3])) { - $meta = $this->getYamlParser()->parse($rawMetaMatches[3]); + $meta = $this->getYamlParser()->parse($rawMetaMatches[3]) ?: array(); + $meta = is_array($meta) ? $meta : array('title' => $meta); - if ($meta !== null) { - // the parser may return a string for non-YAML 1-liners - // assume that this string is the page title - $meta = is_array($meta) ? array_change_key_case($meta, CASE_LOWER) : array('title' => $meta); - } else { - $meta = array(); - } - - foreach ($headers as $fieldId => $fieldName) { - $fieldName = strtolower($fieldName); - if (isset($meta[$fieldName])) { + foreach ($headers as $name => $key) { + if (isset($meta[$name])) { // rename field (e.g. remove whitespaces) - if ($fieldId != $fieldName) { - $meta[$fieldId] = $meta[$fieldName]; - unset($meta[$fieldName]); + if ($key != $name) { + $meta[$key] = $meta[$name]; + unset($meta[$name]); } - } elseif (!isset($meta[$fieldId])) { + } elseif (!isset($meta[$key])) { // guarantee array key existance - $meta[$fieldId] = ''; + $meta[$key] = ''; } } diff --git a/plugins/DummyPlugin.php b/plugins/DummyPlugin.php index 00877bd..e53ad00 100644 --- a/plugins/DummyPlugin.php +++ b/plugins/DummyPlugin.php @@ -403,8 +403,8 @@ class DummyPlugin extends AbstractPicoPlugin * * @see Pico::getMetaHeaders() * @param string[] &$headers list of known meta header - * fields; the array value specifies the YAML key to search for, the - * array key is later used to access the found value + * fields; the array key specifies the YAML key to search for, the + * array value is later used to access the found value * @return void */ public function onMetaHeaders(array &$headers)