sample implementation of frontmatter in pages

This commit is contained in:
Andy Miller
2014-09-19 17:51:29 -06:00
parent 0ab39a9b85
commit 103369cc64
3 changed files with 40 additions and 1 deletions

View File

@@ -33,3 +33,6 @@ form:
@data-options: '\Grav\Common\Page\Pages::types'
validate:
required: true

View File

@@ -59,6 +59,25 @@ class Markdown extends General
return $content['markdown'];
}
/**
* Get/set frontmatter content.
*
* @param string $var
*
* @return string
*/
public function frontmatter($var = null)
{
$content = $this->content();
if ($var !== null) {
$content['frontmatter'] = (string) $var;
$this->content($content);
}
return $content['frontmatter'];
}
/**
* Check contents and make sure it is in correct format.
*
@@ -104,13 +123,18 @@ class Markdown extends General
protected function decode($var)
{
$content = array();
$content['header'] = array();
$content['frontmatter'] = array();
// Normalize line endings to Unix style.
$var = preg_replace("/(\r\n|\r)/", "\n", $var);
// Parse header.
preg_match("/---\n(.+?)\n---(\n\n|$)/uism", $var, $m);
$content['header'] = isset($m[1]) ? YamlParser::parse(preg_replace("/\n\t/", "\n ", $m[1])) : array();
if (isset($m[1])) {
$content['frontmatter'] = preg_replace("/\n\t/", "\n ", $m[1]);
$content['header'] = YamlParser::parse($content['frontmatter']);
}
// Strip header to get content.
$content['markdown'] = trim(preg_replace("/---\n(.+?)\n---(\n\n|$)/uism", '', $var));

View File

@@ -54,6 +54,7 @@ class Page
protected $modified;
protected $id;
protected $header;
protected $frontmatter;
protected $content;
protected $raw_content;
protected $pagination;
@@ -140,6 +141,14 @@ class Page
return $file ? $file->raw() : '';
}
public function frontmatter($var = null) {
if ($var) {
$this->frontmatter = $var;
}
return $this->frontmatter;
}
/**
* Gets and Sets the header based on the YAML configuration at the top of the .md file
*
@@ -164,6 +173,7 @@ class Page
$file = $this->file();
if ($file) {
$this->raw_content = $file->markdown();
$this->frontmatter = $file->frontmatter();
$this->header = (object) $file->header();
$var = true;
@@ -254,6 +264,8 @@ class Page
return Utils::truncateHTML($content, $size);
}
/**
* Gets and Sets the content based on content portion of the .md file
*