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...).
This commit is contained in:
Daniel Rudolf 2017-10-14 23:12:16 +02:00
parent 151908fbad
commit d8a649e6f7
No known key found for this signature in database
GPG key ID: A061F02CD8DE4538
2 changed files with 17 additions and 25 deletions

View file

@ -1199,12 +1199,12 @@ class Pico
{ {
if ($this->metaHeaders === null) { if ($this->metaHeaders === null) {
$this->metaHeaders = array( $this->metaHeaders = array(
'title' => 'Title', 'Title' => 'title',
'description' => 'Description', 'Description' => 'description',
'author' => 'Author', 'Author' => 'author',
'date' => 'Date', 'Date' => 'date',
'robots' => 'Robots', 'Robots' => 'robots',
'template' => 'Template' 'Template' => 'template'
); );
$this->triggerEvent('onMetaHeaders', array(&$this->metaHeaders)); $this->triggerEvent('onMetaHeaders', array(&$this->metaHeaders));
@ -1254,27 +1254,19 @@ class Pico
$pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n" $pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
. "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s"; . "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
if (preg_match($pattern, $rawContent, $rawMetaMatches) && isset($rawMetaMatches[3])) { 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) { foreach ($headers as $name => $key) {
// the parser may return a string for non-YAML 1-liners if (isset($meta[$name])) {
// 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])) {
// rename field (e.g. remove whitespaces) // rename field (e.g. remove whitespaces)
if ($fieldId != $fieldName) { if ($key != $name) {
$meta[$fieldId] = $meta[$fieldName]; $meta[$key] = $meta[$name];
unset($meta[$fieldName]); unset($meta[$name]);
} }
} elseif (!isset($meta[$fieldId])) { } elseif (!isset($meta[$key])) {
// guarantee array key existance // guarantee array key existance
$meta[$fieldId] = ''; $meta[$key] = '';
} }
} }

View file

@ -403,8 +403,8 @@ class DummyPlugin extends AbstractPicoPlugin
* *
* @see Pico::getMetaHeaders() * @see Pico::getMetaHeaders()
* @param string[] &$headers list of known meta header * @param string[] &$headers list of known meta header
* fields; the array value specifies the YAML key to search for, the * fields; the array key specifies the YAML key to search for, the
* array key is later used to access the found value * array value is later used to access the found value
* @return void * @return void
*/ */
public function onMetaHeaders(array &$headers) public function onMetaHeaders(array &$headers)