Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch YAML parse errors #293

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/Pico.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']
Expand Down
9 changes: 9 additions & 0 deletions themes/default/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
</header>

<section id="content">
{% if meta.YAML_ParseError %}
<div class="error">
<div class="inner">
<h2>Invalid YAML Front Matter</h2>
<p>{{ meta.YAML_ParseError }}</p>
</div>
</div>
{% endif %}

<div class="inner">
{{ content }}
</div>
Expand Down
14 changes: 13 additions & 1 deletion themes/default/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ a:hover, a:active {
}

h1, h2, h3, h4, h5, h6 {
color: #000;
line-height: 1.2em;
margin-bottom: 0.6em;
}
Expand Down Expand Up @@ -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
/*---------------------------------------------*/
Expand Down