Add template fallbacks. Closes #2

Also improved slightly the keyword generator, and added npm to dependabot scanning
This commit is contained in:
Belle Aerni 2023-01-07 13:35:14 -08:00
parent a36755bb80
commit b953821dd2
5 changed files with 46 additions and 17 deletions

View file

@ -11,6 +11,11 @@ updates:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/.github"
schedule:
interval: "daily"
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"

View file

@ -17,7 +17,7 @@ jobs:
php_version: "8.0"
- run: |
sudo apt-get install zip -y
zip -r AntCMS.zip src
zip -j AntCMS.zip src
- uses: ncipollo/release-action@v1
with:
artifacts: "AntCMS.zip"

View file

@ -43,12 +43,11 @@ class AntCMS
public function getPageLayout($theme = null)
{
$theme = $this->getThemeContent($theme);
$siteInfo = AntCMS::getSiteInfo();
$currentConfig = AntConfig::currentConfig();
$pageTemplate = $theme['default_layout'];
$pageTemplate = str_replace('<!--AntCMS-Navigation-->', AntPages::generateNavigation($theme['nav_layout']), $pageTemplate);
$pageTemplate = $this->getThemeTemplate('default_layout', $theme);
$pageTemplate = str_replace('<!--AntCMS-Navigation-->', AntPages::generateNavigation($this->getThemeTemplate('nav_layout', $theme)), $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteTitle-->', $siteInfo['siteTitle'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-SiteLink-->', '//' . $currentConfig['baseURL'], $pageTemplate);
@ -95,21 +94,23 @@ class AntCMS
}
}
public function getThemeContent($theme = null)
public function getThemeTemplate($layout = 'default_layout', $theme = null)
{
$currentConfig = AntConfig::currentConfig();
$theme = ($theme) ? $theme : $currentConfig['activeTheme'];
$themePath = antThemePath . '/' . $theme;
$templatePath = $themePath . '/' . 'Templates';
$themeContent['default_layout'] = file_get_contents($templatePath . '/default_layout.html');
$themeContent['nav_layout'] = file_get_contents($templatePath . '/nav_layout.html');
$theme = $theme ?? $currentConfig['activeTheme'];
$templatePath = antThemePath . '/' . $theme . '/' . 'Templates';
$defaultTemplates = antThemePath . '/Default/Templates';
if (!$themeContent['nav_layout']) {
$themeContent['default_layout'] = '';
$templates = AntTools::getFileList($templatePath, 'html');
if (in_array($layout . '.html', $templates)) {
$template = file_get_contents($templatePath . '/' . $layout . '.html');
} else {
$template = file_get_contents($defaultTemplates . '/' . $layout . '.html');
}
if (!$themeContent['default_layout']) {
$themeContent['default_layout'] = '
if ($layout == 'default_layout' && !$template) {
$template = '
<!DOCTYPE html>
<html>
<head>
@ -124,15 +125,17 @@ class AntCMS
</html>';
}
return $themeContent;
return $template;
}
public static function getPageHeaders($pageContent)
{
$AntKeywords = new AntKeywords();
// First get the AntCMS header and store it in the matches varible
preg_match('/--AntCMS--.*--AntCMS--/s', $pageContent, $matches);
// Remove the AntCMS section from the content
// Then remove it from the page content so it doesn't cause issues if we try to generate the keywords
$pageContent = preg_replace('/--AntCMS--.*--AntCMS--/s', '', $pageContent);
$pageHeaders = [];

View file

@ -26,16 +26,19 @@ class AntKeywords
}
// A bunch of characters we don't want to use for keyword generation
$stopWords = array(' a ', ' an ', ' and ', ' are ', ' as ', ' at ', ' be ', ' by ', ' for ', ' from ', ' has ', ' have ', ' he ', ' in ', ' is ', ' it ', ' its ', ' of ', ' on ', ' that ', ' the ', ' to ', ' was ', ' were ', ' will ', ' with ');
$stopWords = array(' a ', ' an ', ' and ', ' are ', ' as ', ' at ', ' be ', ' by ', ' for ', ' from ', ' has ', ' have ', ' in ', ' is ', ' it ', ' its ', ' of ', ' on ', ' that ', ' the ', ' to ', ' was ', ' were ', ' will ', ' with ');
$symbols = array('$', '€', '£', '¥', 'CHF', '₹', '+', '-', '×', '÷', '=', '>', '<', '.', ',', ';', ':', '!', '?', '"', '\'', '(', ')', '[', ']', '{', '}', '©', '™', '°', '§', '¶', '•', '_', '/');
$markdownSymbols = array('#', '##', '###', '####', '#####', '~~', '__', '**', '`', '``', '```', '*', '+', '>', '[', ']', '(', ')', '!', '&', '|');
$numbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$commonPronouns = array('he', 'him', 'his', 'she', 'her', 'hers', 'they', 'them', 'theirs');
//Strip the aforementioned characters away
$content = strtolower($content);
$content = str_replace($stopWords, ' ', $content);
$content = str_replace($symbols, ' ', $content);
$content = str_replace($markdownSymbols, ' ', $content);
$content = str_replace($numbers, ' ', $content);
$content = str_replace($commonPronouns, ' ', $content);
//Convert to an arrays
$words = explode(' ', $content);

18
src/AntCMS/AntTools.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace AntCMS;
class AntTools
{
public static function getFileList($dir, $extension = null, $returnPath = false)
{
$dir = new \RecursiveDirectoryIterator(antThemePath);
$iterator = new \RecursiveIteratorIterator($dir);
foreach ($iterator as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == $extension || $extension == null) {
$files[] = ($returnPath) ? $file->getPathname() : $file->getFilename();
}
}
return $files;
}
}