Add Pico::substituteFileContent() method

This commit is contained in:
Daniel Rudolf 2017-06-21 14:27:34 +02:00
parent df532e45bb
commit 55a0a5bc91
No known key found for this signature in database
GPG key ID: A061F02CD8DE4538

View file

@ -1290,22 +1290,40 @@ class Pico
/** /**
* Applies some static preparations to the raw contents of a page, * Applies some static preparations to the raw contents of a page,
* e.g. removing the meta header and replacing %base_url% * e.g. removing the meta header and replacing %...% placehodlers
* *
* This method calls the {@see Pico::substituteFileContent()} method.
*
* @see Pico::substituteFileContent()
* @see Pico::parseFileContent() * @see Pico::parseFileContent()
* @see Pico::getFileContent() * @see Pico::getFileContent()
* @param string $rawContent raw contents of a page * @param string $rawContent raw contents of a page
* @param array $meta meta data to use for %meta.*% replacement * @param array $meta meta data to use for %meta.*% replacement
* @return string contents prepared for parsing * @return string prepared Markdown contents
*/ */
public function prepareFileContent($rawContent, array $meta) public function prepareFileContent($rawContent, array $meta)
{ {
$variables = array();
// remove meta header // remove meta header
$metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n" $metaHeaderPattern = "/^(\/(\*)|---)[[:blank:]]*(?:\r)?\n"
. "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s"; . "(?:(.*?)(?:\r)?\n)?(?(2)\*\/|---)[[:blank:]]*(?:(?:\r)?\n|$)/s";
$content = preg_replace($metaHeaderPattern, '', $rawContent, 1); $markdown = preg_replace($metaHeaderPattern, '', $rawContent, 1);
// replace placeholders
$markdown = $this->substituteFileContent($markdown, $meta);
return $markdown;
}
/**
* Replaces all %...% placeholders in a page's contents
*
* @param string $markdown Markdown contents of a page
* @param array $meta meta data to use for %meta.*% replacement
* @return string substituted Markdown contents
*/
public function substituteFileContent($markdown, array $meta = array())
{
$variables = array();
// replace %version% // replace %version%
$variables['%version%'] = static::VERSION; $variables['%version%'] = static::VERSION;
@ -1336,27 +1354,28 @@ class Pico
} }
} }
$content = str_replace(array_keys($variables), $variables, $content); return str_replace(array_keys($variables), $variables, $markdown);
return $content;
} }
/** /**
* Parses the contents of a page using ParsedownExtra * Parses the contents of a page using ParsedownExtra
* *
* @see Pico::prepareFileContent() * @see Pico::prepareFileContent()
* @see Pico::substituteFileContent()
* @see Pico::getFileContent() * @see Pico::getFileContent()
* @param string $content raw contents of a page (Markdown) * @param string $markdown Markdown contents of a page
* @return string parsed contents (HTML) * @return string parsed contents (HTML)
*/ */
public function parseFileContent($content) public function parseFileContent($markdown)
{ {
return $this->getParsedown()->text($content); return $this->getParsedown()->text($markdown);
} }
/** /**
* Returns the cached contents of the requested page * Returns the cached contents of the requested page
* *
* @see Pico::prepareFileContent() * @see Pico::prepareFileContent()
* @see Pico::substituteFileContent()
* @see Pico::parseFileContent() * @see Pico::parseFileContent()
* @return string|null parsed contents * @return string|null parsed contents
*/ */