implement list mode

This new syntax allows to specify a list of images including title and
description.

implements #7
implements #159
This commit is contained in:
Andreas Gohr
2023-08-23 15:57:04 +02:00
parent b3c199ca0d
commit 826d25a2f9
7 changed files with 90 additions and 10 deletions

View File

@ -4,8 +4,6 @@ namespace dokuwiki\plugin\gallery\classes;
abstract class AbstractGallery
{
/** @var Image[] */
protected $images = [];
/** @var Options */
@ -14,7 +12,7 @@ abstract class AbstractGallery
/**
* Initialize the Gallery
*
* @param string $src The source from where to get the images
* @param mixed $src The source from where to get the images
* @param Options $options Gallery configuration
*/
public function __construct($src, Options $options)

View File

@ -9,7 +9,10 @@ class FeedGallery extends AbstractGallery
protected $feedHost;
protected $feedPath;
/** @inheritdoc */
/**
* @inheritdoc
* @param string $url
*/
public function __construct($url, Options $options)
{
parent::__construct($url, $options);

40
classes/ListGallery.php Normal file
View File

@ -0,0 +1,40 @@
<?php
namespace dokuwiki\plugin\gallery\classes;
/**
* A gallery created from a list of images
*/
class ListGallery extends AbstractGallery
{
/**
* @inheritdoc
* @param string[] $src
*/
public function __construct($src, Options $options)
{
parent::__construct($src, $options);
foreach ($src as $item) {
[$img, $meta] = sexplode(' ', $item, 2);
[$title, $desc] = sexplode('-', $meta, 2);
$img = trim($img);
$title = trim($title);
$desc = trim($desc);
if (!$this->hasImageExtension($img)) continue;
try {
$image = new Image($img);
} catch (\Exception $e) {
// not found
continue;
}
if ($title) $image->setTitle($title);
if ($desc) $image->setDescription($desc);
$this->images[] = $image;
}
}
}

View File

@ -45,7 +45,7 @@ class Options
public function __construct()
{
// load options from config
$plugin = plugin_load('syntax', 'gallery');
$plugin = plugin_load('syntax', 'gallery_main');
$this->thumbnailWidth = $plugin->getConf('thumbnail_width');
$this->thumbnailHeight = $plugin->getConf('thumbnail_height');
$this->lightboxWidth = $plugin->getConf('image_width');

View File

@ -51,7 +51,7 @@ class XHTMLFormatter extends BasicFormatter
{
if (count($pages) <= 1) return;
$plugin = plugin_load('syntax', 'gallery');
$plugin = plugin_load('syntax', 'gallery_main');
$this->renderer->doc .= '<div class="gallery-page-selector">';
$this->renderer->doc .= '<span>' . $plugin->getLang('pages') . ' </span>';
@ -157,7 +157,7 @@ class XHTMLFormatter extends BasicFormatter
$p = [
'class' => 'gallery-caption',
];
$html .= '<p' . buildAttributes($p) . '>' . hsc($image->getDescription()) . '</p>';
$html .= '<div ' . buildAttributes($p) . '>' . hsc($image->getDescription()) . '</div>';
}
if ($this->options->showname) {
$a = [

36
syntax/list.php Normal file
View File

@ -0,0 +1,36 @@
<?php
use dokuwiki\plugin\gallery\classes\Options;
/**
* DokuWiki Plugin gallery (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <andi@splitbrain.org>
*/
class syntax_plugin_gallery_list extends syntax_plugin_gallery_main
{
/** @inheritDoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('<gallery.*?>.+?</gallery>', $mode, 'plugin_gallery_list');
}
/** @inheritDoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
$match = substr($match, 8, -10); //strip markup from start and end
[$params, $list] = sexplode('>', $match, 2);
$options = new Options();
$options->parseParameters($params);
$list = explode("\n", $list);
$list = array_map('trim', $list);
$list = array_filter($list);
return [$list, $options];
}
}

View File

@ -3,6 +3,7 @@
use dokuwiki\File\PageResolver;
use dokuwiki\plugin\gallery\classes\BasicFormatter;
use dokuwiki\plugin\gallery\classes\FeedGallery;
use dokuwiki\plugin\gallery\classes\ListGallery;
use dokuwiki\plugin\gallery\classes\XHTMLFormatter;
use dokuwiki\plugin\gallery\classes\NamespaceGallery;
use dokuwiki\plugin\gallery\classes\Options;
@ -15,7 +16,7 @@ use dokuwiki\plugin\gallery\classes\Options;
* @author Joe Lapp <joe.lapp@pobox.com>
* @author Dave Doyle <davedoyle.canadalawbook.ca>
*/
class syntax_plugin_gallery extends DokuWiki_Syntax_Plugin
class syntax_plugin_gallery_main extends DokuWiki_Syntax_Plugin
{
/** @inheritdoc */
@ -39,7 +40,7 @@ class syntax_plugin_gallery extends DokuWiki_Syntax_Plugin
/** @inheritdoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\{\{gallery>[^}]*\}\}', $mode, 'plugin_gallery');
$this->Lexer->addSpecialPattern('\{\{gallery>[^}]*\}\}', $mode, 'plugin_gallery_main');
}
/** @inheritdoc */
@ -80,7 +81,9 @@ class syntax_plugin_gallery extends DokuWiki_Syntax_Plugin
{
[$src, $options] = $data;
if (preg_match('/^https?:\/\//i', $src)) {
if (is_array($src)) {
$gallery = new ListGallery($src, $options);
} elseif (preg_match('/^https?:\/\//i', $src)) {
$gallery = new FeedGallery($src, $options);
} else {
$gallery = new NamespaceGallery($src, $options);