From e6103791f528357197f8afb9ed222a9469cbd177 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Tue, 20 Jul 2021 18:39:34 +0200 Subject: Implement new *content functions* feature (readme) This commit implements a new feature called *content functions* that is similar but much more powerful than the already existing *content tags* which you may have already used (`{POST[1]}`, for example). You now can also add your own *content functions* to do some interesting things like embedding a YouTube video or other things to prevent typing repetitive lines of text or code in your entities content. Read the corresponding wiki page to learn more about this: https://github.com/Nerdmind/Blog/wiki/Content-functions --- core/functions.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'core/functions.php') diff --git a/core/functions.php b/core/functions.php index 97ff802..cf35aa9 100644 --- a/core/functions.php +++ b/core/functions.php @@ -9,6 +9,7 @@ use Template\Template as Template; use Template\Factory as TemplateFactory; use Parsers\ArgumentParser; +use Parsers\FunctionParser; use Parsers\EmoticonParser; use Parsers\MarkdownParser; @@ -190,7 +191,8 @@ function parseContentTags(string $text): string { $text = preg_replace($base_tag, \Application::getURL('$1'), $text); $text = preg_replace($file_tag, \Application::getFileURL('$1'), $text); - return $text; + $FunctionParser = new FunctionParser; + return $FunctionParser->transform($text); } #=============================================================================== @@ -318,6 +320,23 @@ function generateSlug($string, $separator = '-') { return trim($string, $separator); } +#=========================================================================== +# Callback for (CATEGORY|PAGE|POST|USER) content function +#=========================================================================== +function getEntityMarkdownLink($ns, $id, $text = NULL, $info = NULL): string { + if(!$Entity = Application::getRepository($ns)->find($id)) { + return sprintf('`{%s: *Reference error*}`', strtoupper($ns)); + } + + $title = htmlspecialchars($Entity->get('name') ?? $Entity->get('fullname')); + $href = Application::getEntityURL($Entity); + $text = $text ?: "»{$title}«"; + $info = $info ?: sprintf('%s »%s«', + Application::getLanguage()->text(strtolower($ns)), $title); + + return sprintf('[%s](%s "%s")', $text, $href, $info); +} + #=============================================================================== # Function for use in templates to get data of a category #=============================================================================== @@ -369,3 +388,45 @@ function USER(int $id): array { return []; } + +#=========================================================================== +# Get base URL (optionally extended by $extend) +#=========================================================================== +FunctionParser::register('BASE_URL', function($extend = '') { + return Application::getURL($extend); +}); + +#=========================================================================== +# Get file URL (optionally extended by $extend) +#=========================================================================== +FunctionParser::register('FILE_URL', function($extend = '') { + return Application::getFileURL($extend); +}); + +#=========================================================================== +# Get Markdown formatted *category* link +#=========================================================================== +FunctionParser::register('CATEGORY', function($id, $text = NULL, $title = NULL) { + return getEntityMarkdownLink('Category', $id, $text, $title); +}); + +#=========================================================================== +# Get Markdown formatted *page* link +#=========================================================================== +FunctionParser::register('PAGE', function($id, $text = NULL, $title = NULL) { + return getEntityMarkdownLink('Page', $id, $text, $title); +}); + +#=========================================================================== +# Get Markdown formatted *post* link +#=========================================================================== +FunctionParser::register('POST', function($id, $text = NULL, $title = NULL) { + return getEntityMarkdownLink('Post', $id, $text, $title); +}); + +#=========================================================================== +# Get Markdown formatted *user* link +#=========================================================================== +FunctionParser::register('USER', function($id, $text = NULL, $title = NULL) { + return getEntityMarkdownLink('User', $id, $text, $title); +}); -- cgit v1.2.3