Add base class for syntax handlers
Some checks failed
DokuWiki Default Tasks / all (push) Has been cancelled

This commit is contained in:
2026-01-06 09:02:39 +01:00
parent 0ad43bcf9c
commit 490a483df1
5 changed files with 273 additions and 258 deletions

View File

@@ -1,111 +1,41 @@
<?php
use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\plugin\luxtools\Crawler;
use dokuwiki\plugin\luxtools\Output;
use dokuwiki\plugin\luxtools\Path;
require_once(__DIR__ . '/AbstractSyntax.php');
/**
* LuxTools Plugin: Directory syntax.
*
* Lists the direct children (folders and files) of a given path.
* Always renders as a table.
*/
class syntax_plugin_luxtools_directory extends SyntaxPlugin
class syntax_plugin_luxtools_directory extends syntax_plugin_luxtools_abstract
{
/** @inheritdoc */
public function getType()
protected function getSyntaxKeyword(): string
{
return 'substition';
return 'directory';
}
/** @inheritdoc */
public function getPType()
protected function processPath(string $path): array
{
return 'block';
}
/** @inheritdoc */
public function getSort()
{
return 222;
}
/** @inheritdoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\{\{directory>.+?\}\}', $mode, 'plugin_luxtools_directory');
}
/** @inheritdoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
global $INPUT;
// do not allow the syntax in discussion plugin comments
if (!$this->getConf('allow_in_comments') && $INPUT->has('comment')) {
return false;
}
$match = substr($match, strlen('{{directory>'), -2);
[$path, $flags] = explode('&', $match, 2);
// load default config options
$flags = $this->getConf('defaults') . '&' . $flags;
$flags = explode('&', $flags);
$params = [
'sort' => 'name',
'order' => 'asc',
'style' => 'list',
'tableheader' => 0,
'recursive' => 0,
'titlefile' => '_title.txt',
'cache' => 0,
'randlinks' => 0,
'showsize' => 0,
'showdate' => 0,
'listsep' => ', ',
];
foreach ($flags as $flag) {
[$name, $value] = sexplode('=', $flag, 2, '');
$params[trim($name)] = trim(trim($value), '"'); // quotes can be used to keep whitespace
}
// directory path (no glob/pattern)
// Directory path (no glob/pattern)
$path = Path::cleanPath($path, true);
return [$path, $params];
return ['path' => $path];
}
/** @inheritdoc */
public function render($format, Doku_Renderer $renderer, $data)
protected function doRender(string $format, \Doku_Renderer $renderer, array $pathData, array $params): bool
{
if ($data === false) return false;
[$path, $params] = $data;
if ($format != 'xhtml' && $format != 'odt') {
return false;
}
// disable caching
if ($params['cache'] === 0) {
$renderer->nocache();
}
try {
$pathHelper = new Path($this->getConf('paths'));
$pathInfo = $pathHelper->getPathInfo($path);
} catch (Exception $e) {
$renderer->cdata('[n/a: ' . $this->getLang('error_outsidejail') . ']');
$pathInfo = $this->getPathInfoSafe($pathData['path'], $renderer);
if ($pathInfo === false) {
return true;
}
$crawler = new Crawler($this->getConf('extensions'));
$crawler->setSortBy($params['sort']);
$crawler->setSortReverse($params['order'] === 'desc');
$crawler = $this->createCrawler($params);
$items = $crawler->listDirectory(
$pathInfo['root'],
$pathInfo['local'],
@@ -113,7 +43,7 @@ class syntax_plugin_luxtools_directory extends SyntaxPlugin
);
if ($items == []) {
$renderer->cdata('[n/a: ' . $this->getLang('error_nomatch') . ']');
$this->renderError($renderer, 'error_nomatch');
return true;
}