Pārlūkot izejas kodu

Catch YAML parse errors

Daniel Rudolf 9 gadi atpakaļ
vecāks
revīzija
5ea94d06e6
1 mainītis faili ar 14 papildinājumiem un 1 dzēšanām
  1. 14 1
      lib/Pico.php

+ 14 - 1
lib/Pico.php

@@ -753,6 +753,8 @@ class Pico
      * @param  string   $rawContent the raw file contents
      * @param  string[] $headers    known meta headers
      * @return array                parsed meta data
+     * @throws \Symfony\Component\Yaml\Exception\ParseException thrown when the
+     *     meta data is invalid
      */
     public function parseFileMeta($rawContent, array $headers)
     {
@@ -955,12 +957,23 @@ class Pico
             $url = $this->getPageUrl($id);
             if ($file != $this->requestFile) {
                 $rawContent = file_get_contents($file);
-                $meta = $this->parseFileMeta($rawContent, $this->getMetaHeaders());
+
+                $headers = $this->getMetaHeaders();
+                try {
+                    $meta = $this->parseFileMeta($rawContent, $headers);
+                } catch (\Symfony\Component\Yaml\Exception\ParseException $e) {
+                    $meta = array_fill_keys(array_keys($headers), '');
+                    $meta['time'] = $meta['date_formatted'] = '';
+                    $meta['YAML_ParseError'] = $e->getMessage();
+                }
             } else {
                 $rawContent = &$this->rawContent;
                 $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']