Add sixel support

This commit is contained in:
Yuki 2025-05-30 17:04:20 -04:00
parent 0982ded5a1
commit 3fd9a57ec5
5 changed files with 74 additions and 4 deletions

3
conf/default.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$conf['render_sixel'] = 0;

3
conf/metadata.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$meta['render_sixel'] = array('onoff');

3
lang/en/settings.php Normal file
View File

@ -0,0 +1,3 @@
<?php
$lang['render_sixel'] = "[Experimental] Enables sixel image rendering. Requires the Imagick PHP extension.";

View File

@ -1,7 +1,7 @@
base ansi
author JP Savard
email yuki@a39.ca
date 2025-05-27
date 2025-05-30
name ANSI/VT100 text renderer
desc Renders pages as text with ANSI/VT100 escape codes.
url https://a39.dev/a39/dokuwiki-plugin-ansi

View File

@ -1,5 +1,7 @@
<?php
use dokuwiki\ChangeLog\MediaChangeLog;
use dokuwiki\File\MediaResolver;
use dokuwiki\File\PageResolver;
/**
@ -511,14 +513,66 @@ class renderer_plugin_ansi extends Doku_Renderer
public function internalmedia($src, $title = null, $align = null, $width = null,
$height = null, $cache = null, $linking = null, $return = false)
{
$this->doc .= "{{" . $src . "}}";
global $ID;
if (strpos($src, '#') !== false) {
[$src, $hash] = sexplode('#', $src, 2);
}
$src = (new MediaResolver($ID))->resolveId($src, $this->date_at, true);
$exists = media_exists($src);
$noLink = false;
$render = $linking != 'linkonly';
$render = $render && $exists;
$render = $render && class_exists("Imagick");
$render = $render && $this->getConf("render_sixel");
[$ext, $mime] = mimetype($src, false);
if (str_starts_with($mime, 'image') && $render)
{
$url = mediaFN($src, $this->_getLastMediaRevisionAt($src));
$image = new Imagick($url);
// todo: resize
if($width || $height) $image->thumbnailImage($width, $height);
$image->setImageFormat('sixel');
$ret = $image->getImageBlob();
}
else
{
$url = ml($src,
[
'id' => $ID,
'cache' => $cache,
'rev' => $this->_getLastMediaRevisionAt($src)
],
true
);
if(is_array($title)) $title = $title['title'];
if($title) $title = "\xF0\x9F\x96\xBC\xEF\xB8\x8F ".$title;
else $title = "\xF0\x9F\x96\xBC\xEF\xB8\x8F";
$ret = self::CSI.($exists ? "35" : "31") . ";4m" .$this->_formatLink($src, $this->_getFullLink($url), $title) . self::CSI."24;39m";
}
if($return) return $ret;
else $this->doc .= $ret;
}
/** @inheritdoc */
public function externalmedia($src, $title = null, $align = null, $width = null,
$height = null, $cache = null, $linking = null, $return = false)
{
$this->doc .= "{{" . $src . "}}";
if($title) $title = "\xF0\x9F\x96\xBC\xEF\xB8\x8F ".$title;
else $title = "\xF0\x9F\x96\xBC\xEF\xB8\x8F";
$ret = self::CSI."95;4m" . $this->_formatLink($src, $src, $title) . self::CSI."24;39m";
if($return) return $ret;
else $this->doc .= $ret;
}
public function _media($src, $title = null, $align = null, $width = null,
$height = null, $cache = null, $linking = null, $return = false)
{
}
/** @inheritdoc */
@ -647,4 +701,11 @@ class renderer_plugin_ansi extends Doku_Renderer
{
return self::OSC."8;;" . $url . self::ST . $name . ($id == $name ? "" : " [" . $id . "]") . self::OSC."8;;" . self::ST;
}
protected function _getLastMediaRevisionAt($media_id)
{
if (!$this->date_at || media_isexternal($media_id)) return '';
$changelog = new MediaChangeLog($media_id);
return $changelog->getLastRevisionAt($this->date_at);
}
}