From 531ea983fea6d86642c1f3efdc4becd8bd09390c Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Sun, 29 Nov 2015 20:37:35 +0100 Subject: [PATCH] Catch YAML parse errors --- lib/Pico.php | 17 ++++++++++++----- themes/default/index.twig | 9 +++++++++ themes/default/style.css | 14 +++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index 7f4b1c3b4..b1152170a 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -266,8 +266,8 @@ public function getThemesDir() * meta headers, processes Markdown, does Twig processing and returns * the rendered contents. * - * @return string rendered Pico contents - * @throws RuntimeException thrown when a not recoverable error occurs + * @return string rendered Pico contents + * @throws Exception thrown when a not recoverable error occurs */ public function run() { @@ -760,9 +760,13 @@ public function parseFileMeta($rawContent, array $headers) $pattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n" . "(.*?)(?:\r)?\n(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s"; if (preg_match($pattern, $rawContent, $rawMetaMatches)) { - $yamlParser = new \Symfony\Component\Yaml\Parser(); - $meta = $yamlParser->parse($rawMetaMatches[3]); - $meta = array_change_key_case($meta, CASE_LOWER); + try { + $yamlParser = new \Symfony\Component\Yaml\Parser(); + $meta = $yamlParser->parse($rawMetaMatches[3]); + $meta = array_change_key_case($meta, CASE_LOWER); + } catch (\Symfony\Component\Yaml\Exception\ParseException $e) { + $meta['YAML_ParseError'] = $e->getMessage(); + } foreach ($headers as $fieldId => $fieldName) { $fieldName = strtolower($fieldName); @@ -961,6 +965,9 @@ protected function readPages() $meta = &$this->meta; } + // fallback to page id if page title is empty + $meta['title'] = (!empty($meta['title'])) ? $meta['title'] : $id; + // build page data // title, description, author and date are assumed to be pretty basic data // everything else is accessible through $page['meta'] diff --git a/themes/default/index.twig b/themes/default/index.twig index 25d8b19ba..9543986dc 100644 --- a/themes/default/index.twig +++ b/themes/default/index.twig @@ -32,6 +32,15 @@
+ {% if meta.YAML_ParseError %} +
+
+

Invalid YAML Front Matter

+

{{ meta.YAML_ParseError }}

+
+
+ {% endif %} +
{{ content }}
diff --git a/themes/default/style.css b/themes/default/style.css index d26f65a5e..16ebf5d9f 100644 --- a/themes/default/style.css +++ b/themes/default/style.css @@ -119,7 +119,6 @@ a:hover, a:active { } h1, h2, h3, h4, h5, h6 { - color: #000; line-height: 1.2em; margin-bottom: 0.6em; } @@ -279,6 +278,19 @@ blockquote { .clearfix { *zoom: 1; } +.error { + margin: -80px 0 80px 0; + padding: 1em 0; + background-color: #F0DDDD; + color: #AA4444; +} +.error h2 { + font-size: 1.5em; + font-weight: bold; +} +.error p:last-child { + margin-bottom: 0; +} /* Media Queries /*---------------------------------------------*/