From 70ff0a106fa6701b5aef051b65ff5e7cbc2c7a4f Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Sun, 26 Mar 2023 23:29:17 -0700 Subject: [PATCH] Per-theme config, default attributes, and bump BS ver --- src/AntCMS/AntCMS.php | 20 ++++++++++++++++++- src/AntCMS/AntConfig.php | 4 ++++ src/AntCMS/AntMarkdown.php | 17 +++++++++++++++- src/Plugins/Admin/AdminPlugin.php | 5 +++++ src/Themes/Bootstrap/Config.yaml | 14 +++++++++++++ .../Bootstrap/Templates/default.html.twig | 9 +++------ 6 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/Themes/Bootstrap/Config.yaml diff --git a/src/AntCMS/AntCMS.php b/src/AntCMS/AntCMS.php index e757a6b..7072d62 100644 --- a/src/AntCMS/AntCMS.php +++ b/src/AntCMS/AntCMS.php @@ -25,6 +25,7 @@ class AntCMS { $start_time = microtime(true); $content = $this->getPage($page); + $themeConfig = Self::getThemeConfig(); if (!$content || !is_array($content)) { $this->renderException("404"); @@ -38,8 +39,9 @@ class AntCMS 'AntCMSAuthor' => $content['author'], 'AntCMSKeywords' => $content['keywords'], 'AntCMSBody' => AntMarkdown::renderMarkdown($content['content']), - 'DisplayAuthor' => true, + 'ThemeConfig' => $themeConfig['config'] ?? [], ]; + $pageTemplate = $this->antTwig->renderWithTiwg($pageTemplate, $params); $end_time = microtime(true); @@ -240,4 +242,20 @@ class AntCMS header("Location: $url"); exit; } + + public static function getThemeConfig(string|null $theme = null) + { + $theme = $theme ?? AntConfig::currentConfig('activeTheme'); + + if (!is_dir(antThemePath . '/' . $theme)) { + $theme = 'Default'; + } + + $configPath = AntTools::repairFilePath(antThemePath . '/' . $theme . '/' . 'Config.yaml'); + if (file_exists($configPath)) { + $config = AntYaml::parseFile($configPath); + } + + return $config ?? []; + } } diff --git a/src/AntCMS/AntConfig.php b/src/AntCMS/AntConfig.php index 134e3ae..d011957 100644 --- a/src/AntCMS/AntConfig.php +++ b/src/AntCMS/AntConfig.php @@ -14,6 +14,7 @@ class AntConfig 'enableCache', 'debug', 'baseURL', + 'embed', ]; /** @@ -31,6 +32,9 @@ class AntConfig 'enableCache' => true, 'debug' => true, 'baseURL' => $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']), + 'embed' => [ + 'allowed_domains' => ['youtube.com', 'twitter.com', 'github.com', 'vimeo.com', 'flickr.com', 'instagram.com', 'facebook.com'], + ] ]; Self::saveConfig($defaultOptions); diff --git a/src/AntCMS/AntMarkdown.php b/src/AntCMS/AntMarkdown.php index b426c15..9be7eca 100644 --- a/src/AntCMS/AntMarkdown.php +++ b/src/AntCMS/AntMarkdown.php @@ -3,6 +3,8 @@ namespace AntCMS; use AntCMS\AntCache; +use AntCMS\AntConfig; +use AntCMS\AntCMS; use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\Autolink\AutolinkExtension; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; @@ -15,6 +17,7 @@ use ElGigi\CommonMarkEmoji\EmojiExtension; use League\CommonMark\Extension\Embed\Bridge\OscaroteroEmbedAdapter; use League\CommonMark\Extension\Embed\EmbedExtension; use SimonVomEyser\CommonMarkExtension\LazyImageExtension; +use League\CommonMark\Extension\DefaultAttributes\DefaultAttributesExtension; class AntMarkdown { @@ -25,6 +28,7 @@ class AntMarkdown { $antCache = new AntCache(); $cacheKey = $antCache->createCacheKey($md, 'markdown'); + $config = AntConfig::currentConfig(); if ($antCache->isCached($cacheKey)) { $cachedContent = $antCache->getCache($cacheKey); @@ -34,13 +38,23 @@ class AntMarkdown } } + $defaultAttributes = []; + $themeConfig = AntCMS::getThemeConfig(); + foreach ($themeConfig['defaultAttributes'] as $class => $attributes) { + $reflectionClass = new \ReflectionClass($class); + $fqcn = $reflectionClass->getName(); + $defaultAttributes[$fqcn] = $attributes; + } + $mdConfig = [ 'embed' => [ 'adapter' => new OscaroteroEmbedAdapter(), - 'allowed_domains' => ['youtube.com', 'twitter.com', 'github.com', 'vimeo.com', 'flickr.com', 'instagram.com', 'facebook.com'], + 'allowed_domains' => $config['embed']['allowed_domains'] ?? [], 'fallback' => 'link', ], + 'default_attributes' => $defaultAttributes, ]; + $environment = new Environment($mdConfig); $environment->addExtension(new CommonMarkCoreExtension()); @@ -52,6 +66,7 @@ class AntMarkdown $environment->addExtension(new EmojiExtension()); $environment->addExtension(new EmbedExtension()); $environment->addExtension(new LazyImageExtension()); + $environment->addExtension(new DefaultAttributesExtension()); $markdownConverter = new MarkdownConverter($environment); diff --git a/src/Plugins/Admin/AdminPlugin.php b/src/Plugins/Admin/AdminPlugin.php index 7de5b19..ff0aca7 100644 --- a/src/Plugins/Admin/AdminPlugin.php +++ b/src/Plugins/Admin/AdminPlugin.php @@ -108,9 +108,14 @@ class AdminPlugin extends AntPlugin if (is_bool($subvalue)) { $currentConfig[$key][$subkey] = ($subvalue) ? 'true' : 'false'; } + if (is_array($subvalue)) { + $currentConfig[$key][$subkey] = implode(', ', $subvalue); + } } } else if (is_bool($value)) { $currentConfig[$key] = ($value) ? 'true' : 'false'; + } else if (is_array($value)) { + $currentConfig[$key] = implode(', ', $value); } } diff --git a/src/Themes/Bootstrap/Config.yaml b/src/Themes/Bootstrap/Config.yaml new file mode 100644 index 0000000..b817b32 --- /dev/null +++ b/src/Themes/Bootstrap/Config.yaml @@ -0,0 +1,14 @@ +config: + showAuthor: false + +defaultAttributes: + \League\CommonMark\Extension\Table\Table: + class: table table-hover + \League\CommonMark\Extension\CommonMark\Node\Inline\Image: + class: img-fluid + \League\CommonMark\Extension\CommonMark\Node\Block\BlockQuote: + class: blockquote + \League\CommonMark\Extension\CommonMark\Node\Block\ListBlock: + class: + \League\CommonMark\Extension\CommonMark\Node\Block\ListItem: + class: diff --git a/src/Themes/Bootstrap/Templates/default.html.twig b/src/Themes/Bootstrap/Templates/default.html.twig index 3130499..2221bec 100644 --- a/src/Themes/Bootstrap/Templates/default.html.twig +++ b/src/Themes/Bootstrap/Templates/default.html.twig @@ -8,11 +8,8 @@ - - + + {{ AntCMSTitle }} @@ -39,7 +36,7 @@
- {% if DisplayAuthor and false %} + {% if AntCMSAuthor is defined and ThemeConfig.showAuthor %}

This content was written by {{ AntCMSAuthor }}

{% endif %} {{ AntCMSBody | raw }}