Pico::prepareFileContent PHP Method

prepareFileContent() public method

Applies some static preparations to the raw contents of a page, e.g. removing the meta header and replacing %base_url%
See also: Pico::parseFileContent()
See also: Pico::getFileContent()
public prepareFileContent ( string $rawContent, array $meta ) : string
$rawContent string raw contents of a page
$meta array meta data to use for %meta.*% replacement
return string contents prepared for parsing
    public function prepareFileContent($rawContent, array $meta)
    {
        // remove meta header
        $metaHeaderPattern = "/^(\\/(\\*)|---)[[:blank:]]*(?:\r)?\n" . "(?:(.*?)(?:\r)?\n)?(?(2)\\*\\/|---)[[:blank:]]*(?:(?:\r)?\n|\$)/s";
        $content = preg_replace($metaHeaderPattern, '', $rawContent, 1);
        // replace %site_title%
        $content = str_replace('%site_title%', $this->getConfig('site_title'), $content);
        // replace %base_url%
        if ($this->isUrlRewritingEnabled()) {
            // always use `%base_url%?sub/page` syntax for internal links
            // we'll replace the links accordingly, depending on enabled rewriting
            $content = str_replace('%base_url%?', $this->getBaseUrl(), $content);
        } else {
            // actually not necessary, but makes the URL look a little nicer
            $content = str_replace('%base_url%?', $this->getBaseUrl() . '?', $content);
        }
        $content = str_replace('%base_url%', rtrim($this->getBaseUrl(), '/'), $content);
        // replace %theme_url%
        $themeUrl = $this->getBaseUrl() . basename($this->getThemesDir()) . '/' . $this->getConfig('theme');
        $content = str_replace('%theme_url%', $themeUrl, $content);
        // replace %meta.*%
        if (!empty($meta)) {
            $metaKeys = $metaValues = array();
            foreach ($meta as $metaKey => $metaValue) {
                if (is_scalar($metaValue) || $metaValue === null) {
                    $metaKeys[] = '%meta.' . $metaKey . '%';
                    $metaValues[] = strval($metaValue);
                }
            }
            $content = str_replace($metaKeys, $metaValues, $content);
        }
        return $content;
    }