From dd14929505e3fd0bc1710ed757b501d3ec1f6ca1 Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Tue, 4 Dec 2018 15:47:37 +0100 Subject: new template system --- app/class/config.php | 8 +-- app/class/modelart.php | 12 ++++ app/class/modelrender.php | 135 ++++++++++++++++++++++--------------------- app/view/templates/admin.php | 2 +- app/view/templates/read.php | 4 +- 5 files changed, 89 insertions(+), 72 deletions(-) diff --git a/app/class/config.php b/app/class/config.php index 38ccf5f..d6904df 100644 --- a/app/class/config.php +++ b/app/class/config.php @@ -4,10 +4,10 @@ abstract class Config { - protected static $arttable = 'artstore'; + protected static $arttable = 'mystore'; protected static $domain; protected static $admin; - protected static $editor; + protected static $editor = 'editor'; protected static $invite; protected static $read; protected static $color4; @@ -15,8 +15,8 @@ abstract class Config protected static $basepath = ''; protected static $route404; protected static $existnot = 'This page does not exist yet'; - protected static $defaultbody = ''; - protected static $defaultart = 'cul'; + protected static $defaultbody = '%HEADER%'. PHP_EOL .PHP_EOL . '%NAV%'. PHP_EOL .PHP_EOL . '%ASIDE%'. PHP_EOL .PHP_EOL . '%SECTION%'. PHP_EOL .PHP_EOL . '%FOOTER%'; + protected static $defaultart = ''; protected static $showeditmenu = true; protected static $editsymbol = 'pen'; diff --git a/app/class/modelart.php b/app/class/modelart.php index 3fa2d48..e9f6ec3 100644 --- a/app/class/modelart.php +++ b/app/class/modelart.php @@ -52,6 +52,18 @@ class Modelart extends Modeldb } } + public function getartelement($id, $element) + { + if(in_array($element, Model::TEXT_ELEMENTS)) { + $art = $this->get($id); + if($art !== false) { + return $art->$element(); + } else { + return ''; + } + } + } + public function delete(Art2 $art) { $this->artstore->delete($art->id()); diff --git a/app/class/modelrender.php b/app/class/modelrender.php index 829c132..43e40a9 100644 --- a/app/class/modelrender.php +++ b/app/class/modelrender.php @@ -36,39 +36,13 @@ class Modelrender extends Modelart public function renderbody(Art2 $art) { $this->art = $art; - $body = $this->getbody($this->readbody(), $this->getelements()); + $body = $this->getbody($this->readbody()); $parsebody = $this->parser($body); return $parsebody; } - public function getelements() - { - $elements = []; - foreach (self::TEXT_ELEMENTS as $element) { - if (isset($this->art->template('array')[$element])) { - $templateid = $this->art->template('array')[$element]; - $tempalteart = $this->get($templateid); - if($tempalteart) { - $text = $tempalteart->$element() . PHP_EOL . $this->art->$element(); - } else { - $text = $this->art->$element(); - } - } else { - $text = $this->art->$element(); - } - $text = $this->article($text); - $text = $this->autotaglistupdate($text); - $text = $this->markdown($text); - - $elements[$element] = PHP_EOL . '<' . $element . '>' . PHP_EOL . $text . PHP_EOL . '' . PHP_EOL; - - } - - return $elements; - } - public function readbody() { if (isset($this->art->template('array')['body'])) { @@ -81,16 +55,44 @@ class Modelrender extends Modelart return $body; } - public function getbody(string $body, array $elements) + public function getbody(string $body) { - $body = preg_replace_callback('~\%(SECTION|ASIDE|NAV|HEADER|FOOTER)\%~', function ($match) use ($elements) { - return $elements[strtolower($match[1])]; + $rend = $this; + $body = preg_replace_callback('~\%(SECTION|ASIDE|NAV|HEADER|FOOTER)(:(((\.|)?([\w-_]+|\!))*))?\%~', function ($match) use ($rend) { + $element = strtolower($match[1]); + $getelement = ''; + if (isset($match[3]) && !empty($match[3])) { + $templatelist = str_replace('!', $this->art->id(), explode('.', $match[3])); + foreach ($templatelist as $template) { + if ($template === $rend->art->id()) { + $templateelement = $rend->art->$element(); + } else { + $templateelement = $rend->getartelement($template, $element); + } + $getelement .= $templateelement; + } + } else { + $templatelist = [$rend->art->id()]; + $getelement = $rend->art->$element(); + } + $class = implode(' ', $templatelist); + $getelement = $rend->elementparser($getelement); + $getelement = PHP_EOL . '<' . $element . ' class="' . $class . '">' . $getelement . ''; + return $getelement; }, $body); return $body; } - - + public function elementparser($element) + { + $element = $this->article($element); + $element = $this->autotaglistupdate($element); + $element = $this->markdown($element); + + return $element; + } + + public function write() { file_put_contents(Model::RENDER_DIR . $this->art->id() . '.css', $this->art->css()); @@ -162,8 +164,8 @@ class Modelrender extends Modelart public function parser(string $text) { - $text = str_replace('uart($matches[1]) . '"" title="' . Config::existnot() . '" class="internal"'; } else { $linkfrom[] = $matchart->id(); - return 'href="' . $rend->uart($matches[1]) . $matches[2]. '" title="' . $matchart->description() . '" class="internal"'; + return 'href="' . $rend->uart($matches[1]) . $matches[2] . '" title="' . $matchart->description() . '" class="internal"'; } }, $text @@ -221,8 +223,8 @@ class Modelrender extends Modelart if (!$matchart) { return '' . $matches[1] . ''; } else { - $linkfrom[] = $matchart->id(); - return '' . $matchart->title() . ''; + $linkfrom[] = $matchart->id(); + return '' . $matchart->title() . ''; } }, $text @@ -234,16 +236,17 @@ class Modelrender extends Modelart public function headerid($text) { $sum = []; - $text = preg_replace_callback('/(.+)<\/h[1-6]>/mU', - function ($matches) use (&$sum) { - $cleanid = idclean($matches[4]); - $sum[$cleanid][$matches[1]] = $matches[4]; - return ''.$matches[4].''; - }, - $text - ); - $this->sum = $sum; - return $text; + $text = preg_replace_callback( + '/(.+)<\/h[1-6]>/mU', + function ($matches) use (&$sum) { + $cleanid = idclean($matches[4]); + $sum[$cleanid][$matches[1]] = $matches[4]; + return '' . $matches[4] . ''; + }, + $text + ); + $this->sum = $sum; + return $text; } public function markdown($text) @@ -264,14 +267,14 @@ class Modelrender extends Modelart public function article($text) { $pattern = '/(\R\R|^\R|^)[=]{3,}([\w-]*)\R\R(.*)(?=\R\R[=]{3,}[\w-]*\R)/sUm'; - $text = preg_replace_callback($pattern, function($matches) { - if(!empty($matches[2])) { - $id = ' id="'.$matches[2].'" '; + $text = preg_replace_callback($pattern, function ($matches) { + if (!empty($matches[2])) { + $id = ' id="' . $matches[2] . '" '; } else { $id = ' '; } return '
' . PHP_EOL . PHP_EOL . $matches[3] . PHP_EOL . PHP_EOL . '
' . PHP_EOL . PHP_EOL; - } , $text); + }, $text); $text = preg_replace('/\R\R[=]{3,}([\w-]*)\R/', '', $text); return $text; } @@ -326,30 +329,30 @@ class Modelrender extends Modelart public function autotaglistupdate($text) { $taglist = $this->autotaglist($text); - foreach ($taglist as $tag ) { + foreach ($taglist as $tag) { $li = []; - foreach ($this->artlist as $item ) { - if(in_array($tag, $item->tag('array'))) { + foreach ($this->artlist as $item) { + if (in_array($tag, $item->tag('array'))) { $li[] = $item; } - + } - $ul = '
    ' . PHP_EOL; + $ul = '
      ' . PHP_EOL; $this->artlistsort($li, 'date', -1); - foreach ($li as $item ) { - if($item->id() === $this->art->id()) { - $actual = ' actualpage'; - } else { - $actual = ''; - } - $ul .= '
    • '.$item->title().'
    • ' . PHP_EOL; + foreach ($li as $item) { + if ($item->id() === $this->art->id()) { + $actual = ' actualpage'; + } else { + $actual = ''; } + $ul .= '
    • ' . $item->title() . '
    • ' . PHP_EOL; + } $ul .= '
    ' . PHP_EOL; - - $text = str_replace('%%'.$tag.'%%', $ul, $text); - $li = array_map(function($item) { + $text = str_replace('%%' . $tag . '%%', $ul, $text); + + $li = array_map(function ($item) { return $item->id(); }, $li); $this->linkfrom = array_unique(array_merge($this->linkfrom, $li)); diff --git a/app/view/templates/admin.php b/app/view/templates/admin.php index 54bbedd..d913186 100644 --- a/app/view/templates/admin.php +++ b/app/view/templates/admin.php @@ -42,7 +42,7 @@

    Editing

    - + diff --git a/app/view/templates/read.php b/app/view/templates/read.php index 4e18cb3..42fba7b 100644 --- a/app/view/templates/read.php +++ b/app/view/templates/read.php @@ -51,7 +51,9 @@ $this->stop(); } } else { - echo '

    ' . $alertnotexist . '

    '; + if(!empty(Config::existnot())) { + echo '

    ' . Config::existnot() . '

    '; + } if ($user->iseditor()) { ?> ⭐ Create -- cgit v1.2.3