From d4c65fa5231abe3b3aa84c34a91f87717aa5e63f Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Sun, 5 Feb 2017 21:49:54 +0100 Subject: [PATCH] Allow configuring Parsedown --- config/config.yml.template | 13 +++++++--- lib/Pico.php | 52 ++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/config/config.yml.template b/config/config.yml.template index aed7698..c40dc4c 100644 --- a/config/config.yml.template +++ b/config/config.yml.template @@ -3,7 +3,7 @@ # site_title: Pico # The title of your website base_url: ~ # Pico will try to guess its base URL, if this fails, override it here - # Example: http://example.com/pico/ + # Example: http://example.com/pico/ rewrite_url: ~ # A boolean (true or false) indicating whether URL rewriting is forced timezone: UTC # Your PHP installation might require you to manually specify a timezone @@ -12,7 +12,7 @@ timezone: UTC # Your PHP installation might require you to # theme: default # The name of your custom theme theme_url: ~ # Pico will try to guess the URL to the themes dir of your installation - # If this fails, override it here. Example: http://example.com/pico/themes/ + # If this fails, override it here. Example: http://example.com/pico/themes/ theme_config: widescreen: false # Default theme: Use more horicontal space (i.e. make the site container wider) twig_config: @@ -24,11 +24,18 @@ twig_config: # Content # date_format: %D %T # Pico's default date format - # List of available variables: http://php.net/manual/en/function.strftime.php + # See http://php.net/manual/en/function.strftime.php for more info pages_order_by: alpha # Change how Pico sorts pages ("alpha" for alphabetical order, or "date") pages_order: asc # Sort pages in ascending ("asc") or descending ("desc") order content_dir: content/ # The path to Pico's content directory content_ext: .md # The file extension of your Markdown files +content_config: + extra: true # Use the Parsedown Extra parser to support extended markup + # See https://michelf.ca/projects/php-markdown/extra/ for more info + breaks: false # A boolean indicating whether breaks in the markup should be reflected in the + # parsed contents of the page + escape: false # Escape HTML markup in your content files + auto_urls: true # Automatically link URLs found in your markup ## # Plugins diff --git a/lib/Pico.php b/lib/Pico.php index 786608b..477b079 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -695,15 +695,16 @@ class Pico 'site_title' => 'Pico', 'base_url' => '', 'rewrite_url' => null, + 'timezone' => null, 'theme' => 'default', 'theme_url' => null, + 'twig_config' => null, 'date_format' => '%D %T', - 'twig_config' => array('cache' => false, 'autoescape' => false, 'debug' => false), 'pages_order_by' => 'alpha', 'pages_order' => 'asc', 'content_dir' => null, 'content_ext' => '.md', - 'timezone' => null + 'content_config' => null ); if (!$this->config['base_url']) { @@ -716,6 +717,29 @@ class Pico $this->config['rewrite_url'] = $this->isUrlRewritingEnabled(); } + if (!$this->config['timezone']) { + // explicitly set a default timezone to prevent a E_NOTICE + // when no timezone is set; the `date_default_timezone_get()` + // function always returns a timezone, at least UTC + $this->config['timezone'] = @date_default_timezone_get(); + } + date_default_timezone_set($this->config['timezone']); + + if (!$this->config['theme_url']) { + $this->config['theme_url'] = $this->getBaseThemeUrl(); + } elseif (preg_match('#^[A-Za-z][A-Za-z0-9+\-.]*://#', $this->config['theme_url'])) { + $this->config['theme_url'] = rtrim($this->config['theme_url'], '/') . '/'; + } else { + $this->config['theme_url'] = $this->getBaseUrl() . rtrim($this->config['theme_url'], '/') . '/'; + } + + $defaultTwigConfig = array('cache' => false, 'autoescape' => false, 'debug' => false); + if (!is_array($this->config['twig_config'])) { + $this->config['twig_config'] = $defaultTwigConfig; + } else { + $this->config['twig_config'] += $defaultTwigConfig; + } + if (!$this->config['content_dir']) { // try to guess the content directory if (is_dir($this->getRootDir() . 'content')) { @@ -727,21 +751,12 @@ class Pico $this->config['content_dir'] = $this->getAbsolutePath($this->config['content_dir']); } - if (!$this->config['theme_url']) { - $this->config['theme_url'] = $this->getBaseThemeUrl(); - } elseif (preg_match('#^[A-Za-z][A-Za-z0-9+\-.]*://#', $this->config['theme_url'])) { - $this->config['theme_url'] = rtrim($this->config['theme_url'], '/') . '/'; + $defaultContentConfig = array('extra' => true, 'breaks' => false, 'escape' => false, 'auto_urls' => true); + if (!is_array($this->config['content_config'])) { + $this->config['content_config'] = $defaultContentConfig; } else { - $this->config['theme_url'] = $this->getBaseUrl() . rtrim($this->config['theme_url'], '/') . '/'; + $this->config['content_config'] += $defaultContentConfig; } - - if (!$this->config['timezone']) { - // explicitly set a default timezone to prevent a E_NOTICE - // when no timezone is set; the `date_default_timezone_get()` - // function always returns a timezone, at least UTC - $this->config['timezone'] = @date_default_timezone_get(); - } - date_default_timezone_set($this->config['timezone']); } /** @@ -1139,7 +1154,12 @@ class Pico public function getParsedown() { if ($this->parsedown === null) { - $this->parsedown = new ParsedownExtra(); + $className = $this->config['content_config']['extra'] ? 'ParsedownExtra' : 'Parsedown'; + $this->parsedown = new $className(); + + $this->parsedown->setBreaksEnabled((bool) $this->config['content_config']['breaks']); + $this->parsedown->setMarkupEscaped((bool) $this->config['content_config']['escape']); + $this->parsedown->setUrlsLinked((bool) $this->config['content_config']['auto_urls']); } return $this->parsedown;