feat: reworked the template structure
This commit is contained in:
parent
c607d57221
commit
c9808f90f8
24 changed files with 216 additions and 80 deletions
|
@ -6,8 +6,6 @@ class Post implements ArrayAccess {
|
|||
private $content_html;
|
||||
private $content_markdown;
|
||||
|
||||
private $site_defaults;
|
||||
|
||||
public $data;
|
||||
|
||||
public $html_data;
|
||||
|
@ -66,14 +64,12 @@ class Post implements ArrayAccess {
|
|||
|
||||
$this->content_html = null;
|
||||
$this->content_markdown = null;
|
||||
|
||||
$this->site_defaults = $site_defaults;
|
||||
|
||||
if(!isset($post_data) or !isset($post_data['id'])) {
|
||||
$post_data = $this->_generate_404($post_data);
|
||||
}
|
||||
|
||||
$data = $post_data;
|
||||
$data = array_merge($site_defaults, $post_data);
|
||||
|
||||
if($data['path'] == '') {
|
||||
$data['path'] = '/';
|
||||
|
@ -127,11 +123,7 @@ class Post implements ArrayAccess {
|
|||
return $this->data[$name];
|
||||
}
|
||||
|
||||
if(is_null($this->site_defaults)) {
|
||||
throw new RuntimeException("Post site defaults have not been set properly!");
|
||||
}
|
||||
|
||||
return $this->site_defaults[$name] ?? null;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function offsetGet($offset) : mixed {
|
||||
|
@ -141,9 +133,6 @@ class Post implements ArrayAccess {
|
|||
if(isset($this->data[$offset])) {
|
||||
return true;
|
||||
}
|
||||
if(isset($this->site_defaults[$offset])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !is_null($this->offsetGet($offset));
|
||||
}
|
||||
|
@ -179,7 +168,7 @@ class Post implements ArrayAccess {
|
|||
}
|
||||
|
||||
public function to_array($options = []) {
|
||||
$out_data = array_merge($this->site_defaults, $this->data);
|
||||
$out_data = $this->data;
|
||||
|
||||
if(isset($options['markdown'])) {
|
||||
$out_data['markdown'] = $this->get_markdown();
|
||||
|
|
|
@ -61,6 +61,8 @@ $db_connection->execute_query("DELETE FROM posts;");
|
|||
$sql_adapter = new MySQLHandler($db_connection, $SERVER_HOST);
|
||||
$adapter = new PostHandler($sql_adapter);
|
||||
|
||||
$adapter->site_defaults = [];
|
||||
|
||||
$sql_adapter->debugging = true;
|
||||
|
||||
function test_accounce($title) {
|
||||
|
@ -127,15 +129,23 @@ test_accounce("Setting post markdown...");
|
|||
$sql_adapter->set_postdata([
|
||||
'path' => '/testing/markdowntest',
|
||||
'markdown' => 'Inline markdown test should work...',
|
||||
'title' => "A Markdown Test"
|
||||
'title' => "A Markdown Test",
|
||||
'brief' => "The dragons explore markdown, sort of properly... Maybe.",
|
||||
'tags' => ['one', 'two', 'three', 'sexee']
|
||||
]);
|
||||
$post = $sql_adapter->get_postdata('/testing/markdowntest');
|
||||
var_dump($sql_adapter->get_post_markdown($post['id']));
|
||||
|
||||
$sql_adapter->set_post_markdown($post['id'],
|
||||
'
|
||||
This is one hell of a cute test!
|
||||
|
||||
This is one hell of a cute test!
|
||||
|
||||
> Just checking in...
|
||||
|
||||
{{
|
||||
template: fragments/blog/card.html
|
||||
}}
|
||||
'
|
||||
);
|
||||
var_dump($sql_adapter->get_post_markdown($post['id']));
|
||||
|
|
|
@ -1,22 +1,97 @@
|
|||
<?php
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Highlight\Highlighter;
|
||||
|
||||
class Dergdown extends ParsedownExtra
|
||||
{
|
||||
protected $highlighter;
|
||||
|
||||
protected $dergInsertRenderer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->highlighter = new Highlighter();
|
||||
$this->highlighter = null;
|
||||
$this->BlockTypes['{'] []= 'DergInsert';
|
||||
|
||||
$this->dergInsertRenderer = null;
|
||||
}
|
||||
|
||||
public function setDergRenderer($dergRenderer) {
|
||||
$this->dergInsertRenderer = $dergRenderer;
|
||||
}
|
||||
|
||||
protected function blockDergInsert($Line, $currentBlock) {
|
||||
if (preg_match('/^{{\s?(.*)$/', $Line['body'], $match)) {
|
||||
return array(
|
||||
'text' => $match[1] ?? ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function blockDergInsertContinue($Line, $Block) {
|
||||
if(isset($Block['complete'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(preg_match('/(.*)}}/', $Line['body'], $match)) {
|
||||
$Block['text'] .= "\n" . $match[1];
|
||||
$Block['complete'] = true;
|
||||
return $Block;
|
||||
}
|
||||
|
||||
$Block['text'] .= "\n" . $Line['body'];
|
||||
|
||||
return $Block;
|
||||
}
|
||||
|
||||
protected function blockDergInsertComplete($Block) {
|
||||
try {
|
||||
$parsed_data = Yaml::parse($Block['text']);
|
||||
}
|
||||
catch (Exception $ex) {
|
||||
return array(
|
||||
'markup' => '
|
||||
<div class="derg-insert-error">
|
||||
<h3> Error in a dergen template! </h3>
|
||||
YAML could not be parsed properly: <br>
|
||||
<code>
|
||||
' . $ex->getMessage() . '</code> </div>'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
if(!isset($this->dergInsertRenderer)) {
|
||||
throw new Exception("No Dergen Renderer was set!");
|
||||
}
|
||||
|
||||
$render_output = $this->dergInsertRenderer->dergRender($parsed_data);
|
||||
} catch (Exception $ex) {
|
||||
return array(
|
||||
'markup' => '
|
||||
<div class="derg-insert-error">
|
||||
<h3> Error in a dergen template! </h3>
|
||||
Rendering engine threw an error: <br>
|
||||
<code>
|
||||
' . $ex->getMessage() . '</code> </div>'
|
||||
);
|
||||
}
|
||||
|
||||
return array(
|
||||
'markup' => $render_output
|
||||
);
|
||||
}
|
||||
|
||||
protected function blockFencedCodeComplete($block)
|
||||
{
|
||||
{
|
||||
if (! isset($block['element']['text']['attributes'])) {
|
||||
return $block;
|
||||
}
|
||||
|
||||
if(!isset($this->highlighter)) {
|
||||
$this->highlighter = new Highlighter();
|
||||
}
|
||||
|
||||
$code = $block['element']['text']['text'];
|
||||
$languageClass = $block['element']['text']['attributes']['class'];
|
||||
$language = explode('-', $languageClass);
|
||||
|
|
|
@ -21,6 +21,8 @@ if(isset($REQUEST_QUERY['page'])) {
|
|||
$ajax_args['fa'] = $FONT_AWESOME_ARRAY;
|
||||
$ajax_args['page'] ??= $SITE_CONFIG['site_defaults'];
|
||||
|
||||
echo $twig->render('/ajax/' . $AJAX_REQUEST_TEMPLATE, $ajax_args);
|
||||
$ajax_args['post'] ??= $ajax_args['page'];
|
||||
|
||||
echo $twig->render($AJAX_REQUEST_TEMPLATE, $ajax_args);
|
||||
|
||||
?>
|
|
@ -29,14 +29,27 @@ $sql_adapter = new MySQLHandler($db_connection, $SERVER_HOST);
|
|||
$adapter = new PostHandler($sql_adapter);
|
||||
|
||||
require_once 'dergdown.php';
|
||||
require_once 'setup/derg_insert.php';
|
||||
|
||||
function dergdown_to_html($text) {
|
||||
function dergdown_to_html($post) {
|
||||
$DergInsert = new DergInsertRenderer($post);
|
||||
$Parsedown = new Dergdown();
|
||||
$Parsedown->setDergRenderer($DergInsert);
|
||||
|
||||
return $Parsedown->text($text);
|
||||
$markdown = $post->markdown;
|
||||
|
||||
if($markdown == '') {
|
||||
$markdown = '
|
||||
{{
|
||||
template: fragments/directory/inline.html
|
||||
}}
|
||||
';
|
||||
}
|
||||
|
||||
return $Parsedown->text($markdown);
|
||||
}
|
||||
function post_to_html($post) {
|
||||
return dergdown_to_html($post->markdown);
|
||||
return dergdown_to_html($post);
|
||||
}
|
||||
$adapter->markdown_engine = "post_to_html";
|
||||
|
||||
|
|
43
www/src/setup/derg_insert.php
Normal file
43
www/src/setup/derg_insert.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
class DergInsertRenderer {
|
||||
protected $twig;
|
||||
protected $post;
|
||||
protected $postAdapter;
|
||||
|
||||
public function __construct($post) {
|
||||
global $twig;
|
||||
global $adapter;
|
||||
|
||||
$this->twig = $twig;
|
||||
$this->post = $post;
|
||||
$this->postAdapter = $adapter;
|
||||
}
|
||||
|
||||
public function dergRender($renderConfig) {
|
||||
global $FONT_AWESOME_ARRAY;
|
||||
|
||||
if(!isset($renderConfig['template'])) {
|
||||
throw new Exception("No template type given!");
|
||||
}
|
||||
|
||||
$template = $renderConfig['template'];
|
||||
|
||||
$args = [
|
||||
'post' => $this->post,
|
||||
'page' => $this->post,
|
||||
'fa' => $FONT_AWESOME_ARRAY
|
||||
];
|
||||
|
||||
if(isset($renderConfig['post'])) {
|
||||
$args['post'] = $this->postAdapter->get_post($renderConfig['post']);
|
||||
}
|
||||
if(isset($renderConfig['page'])) {
|
||||
$args['page'] = $this->postAdapter->get_post($renderConfig['page']);
|
||||
}
|
||||
|
||||
return $this->twig->render($template, $args);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue