diff --git a/flextype/Flextype.php b/flextype/Flextype.php index 3b229e1f..925338f3 100755 --- a/flextype/Flextype.php +++ b/flextype/Flextype.php @@ -106,6 +106,9 @@ class Flextype // Init I18n I18n::init(); + // Init Shortcodes + Shortcodes::init(); + // Init Themes Themes::init(); diff --git a/flextype/Pages.php b/flextype/Pages.php index f17d12ed..0e98f271 100755 --- a/flextype/Pages.php +++ b/flextype/Pages.php @@ -3,7 +3,6 @@ use Arr; use Url; use Response; -use Shortcode; use Symfony\Component\Yaml\Yaml; /** @@ -109,7 +108,7 @@ class Pages $page = trim(file_get_contents($file)); $page = explode('---', $page, 3); - $frontmatter = Shortcodes::parse($page[1]); + $frontmatter = Shortcodes::driver()->process($page[1]); $result_page = Yaml::parse($frontmatter); // Get page url @@ -158,7 +157,7 @@ class Pages */ public static function parseContent(string $content) : string { - $content = Shortcodes::parse($content); + $content = Shortcodes::driver()->process($content); $content = Markdown::parse($content); return $content; diff --git a/flextype/Shortcodes.php b/flextype/Shortcodes.php index 746f3269..19d98136 100644 --- a/flextype/Shortcodes.php +++ b/flextype/Shortcodes.php @@ -1,5 +1,8 @@ addHandler('site_url', function() { + return Url::getBase(); + }); } /** - * Remove all registered shortcodes. - * - * - * Shortcode::clear(); - * + * Initialize Flextype Shortcodes * + * @access public + * @return object */ - public static function clear() + public static function init() { - static::$shortcode_tags = array(); - } - - /** - * Check if a shortcode has been registered. - * - * @param string $shortcode Shortcode tag. - */ - public static function exists(string $shortcode) - { - // Check shortcode - return array_key_exists($shortcode, static::$shortcode_tags); - } - - /** - * Parse a string, and replace any registered shortcodes within it with the result of the mapped callback. - * - * @param string $content Content - * @return string - */ - public static function parse(string $content) - { - if (! static::$shortcode_tags) { - return $content; - } - - $shortcodes = implode('|', array_map('preg_quote', array_keys(static::$shortcode_tags))); - $pattern = "/(.?)\{([$shortcodes]+)(.*?)(\/)?\}(?(4)|(?:(.+?)\{\/\s*\\2\s*\}))?(.?)/s"; - - return preg_replace_callback($pattern, 'static::_handle', $content); - } - - /** - * _handle() - */ - protected static function _handle($matches) - { - $prefix = $matches[1]; - $suffix = $matches[6]; - $shortcode = $matches[2]; - - // Allow for escaping shortcodes by enclosing them in {{shortcode}} - if ($prefix == '{' && $suffix == '}') { - return substr($matches[0], 1, -1); - } - - $attributes = array(); // Parse attributes into into this array. - - if (preg_match_all('/(\w+) *= *(?:([\'"])(.*?)\\2|([^ "\'>]+))/', $matches[3], $match, PREG_SET_ORDER)) { - foreach ($match as $attribute) { - if (! empty($attribute[4])) { - $attributes[strtolower($attribute[1])] = $attribute[4]; - } elseif (! empty($attribute[3])) { - $attributes[strtolower($attribute[1])] = $attribute[3]; - } - } - } - - // Check if this shortcode realy exists then call user function else return empty string - return (isset(static::$shortcode_tags[$shortcode])) ? $prefix . call_user_func(static::$shortcode_tags[$shortcode], $attributes, $matches[5], $shortcode) . $suffix : ''; + return !isset(self::$instance) and self::$instance = new Shortcodes(); } } diff --git a/flextype/boot/shortcodes.php b/flextype/boot/shortcodes.php deleted file mode 100644 index 89bf6521..00000000 --- a/flextype/boot/shortcodes.php +++ /dev/null @@ -1,18 +0,0 @@ - - * @link http://flextype.org - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -// Add {site_url} shortcode -Shortcodes::add('site_url', function () { - return Url::getBase(); -});