Add base class for syntax handlers
Some checks failed
DokuWiki Default Tasks / all (push) Has been cancelled
Some checks failed
DokuWiki Default Tasks / all (push) Has been cancelled
This commit is contained in:
@@ -1,113 +1,55 @@
|
||||
<?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: Image gallery syntax.
|
||||
*
|
||||
* Renders a thumbnail gallery of images matching a glob pattern.
|
||||
*/
|
||||
class syntax_plugin_luxtools_images extends SyntaxPlugin
|
||||
class syntax_plugin_luxtools_images extends syntax_plugin_luxtools_abstract
|
||||
{
|
||||
/** @inheritdoc */
|
||||
public function getType()
|
||||
protected function getSyntaxKeyword(): string
|
||||
{
|
||||
return 'substition';
|
||||
return 'images';
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function getPType()
|
||||
protected function getDefaultParams(): array
|
||||
{
|
||||
return 'block';
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function getSort()
|
||||
{
|
||||
return 222;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function connectTo($mode)
|
||||
{
|
||||
$this->Lexer->addSpecialPattern('\{\{images>.+?\}\}', $mode, 'plugin_luxtools_images');
|
||||
}
|
||||
|
||||
/** @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('{{images>'), -2);
|
||||
[$path, $flags] = explode('&', $match, 2);
|
||||
|
||||
// load default config options
|
||||
$flags = $this->getConf('defaults') . '&' . $flags;
|
||||
$flags = explode('&', $flags);
|
||||
|
||||
$params = [
|
||||
'sort' => 'name',
|
||||
'order' => 'asc',
|
||||
'recursive' => 0,
|
||||
'titlefile' => '_title.txt',
|
||||
'cache' => 0,
|
||||
'randlinks' => 0,
|
||||
// Images syntax doesn't use some of the common params
|
||||
return [
|
||||
'style' => null,
|
||||
'tableheader' => null,
|
||||
'showsize' => null,
|
||||
'showdate' => null,
|
||||
'listsep' => null,
|
||||
];
|
||||
|
||||
foreach ($flags as $flag) {
|
||||
[$name, $value] = sexplode('=', $flag, 2, '');
|
||||
$params[trim($name)] = trim(trim($value), '"'); // quotes can be use to keep whitespace
|
||||
}
|
||||
|
||||
// separate path and pattern
|
||||
$path = Path::cleanPath($path, false);
|
||||
$parts = explode('/', $path);
|
||||
$pattern = array_pop($parts);
|
||||
$base = implode('/', $parts) . '/';
|
||||
|
||||
return [$base, $pattern, $params];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create output
|
||||
*/
|
||||
public function render($format, Doku_Renderer $renderer, $data)
|
||||
/** @inheritdoc */
|
||||
protected function processPath(string $path): array
|
||||
{
|
||||
[$base, $pattern, $params] = $data;
|
||||
[$base, $pattern] = $this->separatePathAndPattern($path);
|
||||
return ['base' => $base, 'pattern' => $pattern];
|
||||
}
|
||||
|
||||
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($base);
|
||||
} catch (Exception $e) {
|
||||
$renderer->cdata('[n/a: ' . $this->getLang('error_outsidejail') . ']');
|
||||
/** @inheritdoc */
|
||||
protected function doRender(string $format, \Doku_Renderer $renderer, array $pathData, array $params): bool
|
||||
{
|
||||
$pathInfo = $this->getPathInfoSafe($pathData['base'], $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);
|
||||
$result = $crawler->crawl(
|
||||
$pathInfo['root'],
|
||||
$pathInfo['local'],
|
||||
$pattern,
|
||||
$pathData['pattern'],
|
||||
$params['recursive'],
|
||||
$params['titlefile']
|
||||
);
|
||||
@@ -115,9 +57,8 @@ class syntax_plugin_luxtools_images extends SyntaxPlugin
|
||||
$items = $this->flattenResultTree($result);
|
||||
$items = $this->filterImages($items);
|
||||
|
||||
// if we got nothing back, display a message
|
||||
if ($items == []) {
|
||||
$renderer->cdata('[n/a: ' . $this->getLang('error_nomatch') . ']');
|
||||
$this->renderError($renderer, 'error_nomatch');
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user