128 lines
3.4 KiB
PHP
128 lines
3.4 KiB
PHP
<?php
|
|
|
|
use dokuwiki\Extension\SyntaxPlugin;
|
|
use dokuwiki\plugin\luxtools\Crawler;
|
|
use dokuwiki\plugin\luxtools\Output;
|
|
use dokuwiki\plugin\luxtools\Path;
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
/** @inheritdoc */
|
|
public function getType()
|
|
{
|
|
return 'substition';
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
public function getPType()
|
|
{
|
|
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)
|
|
$path = Path::cleanPath($path, true);
|
|
|
|
return [$path, $params];
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
public function render($format, Doku_Renderer $renderer, $data)
|
|
{
|
|
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') . ']');
|
|
return true;
|
|
}
|
|
|
|
$crawler = new Crawler($this->getConf('extensions'));
|
|
$crawler->setSortBy($params['sort']);
|
|
$crawler->setSortReverse($params['order'] === 'desc');
|
|
|
|
$items = $crawler->listDirectory(
|
|
$pathInfo['root'],
|
|
$pathInfo['local'],
|
|
$params['titlefile']
|
|
);
|
|
|
|
if ($items == []) {
|
|
$renderer->cdata('[n/a: ' . $this->getLang('error_nomatch') . ']');
|
|
return true;
|
|
}
|
|
|
|
// Always render as table style
|
|
$params['style'] = 'table';
|
|
|
|
$output = new Output($renderer, $pathInfo['root'], $pathInfo['web'], $items);
|
|
$output->renderAsFlatTable($params);
|
|
return true;
|
|
}
|
|
}
|