Files
shaarli_plugins/workchronicles/workchronicles.php
SansGuidon c60a8bbed0 add(workchronicles): plugin for rendering comics
renders comics (for now only workchronicles) directly in Shaarli UI without opening external link
2025-08-07 11:22:27 +00:00

97 lines
3.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Plugin WorkChronicles Thumbnail with Debugging and Substack <figure> Fallback
*
* Pour chaque lien vers workchronicles (site principal ou substack),
* on récupère soit la balise og:image, soit la première <figure> contenant un <img>,
* et on laffiche inline juste sous le titre, avec une taille de 728×728.
*/
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
function workchronicles_url(string $url): string
{
echo "<pre style=\"color: lime;\">DEBUG: Checking URL {$url}</pre>";
$pattern = '~^https?://(?:www\.)?(?:workchronicles\.com|workchronicles\.substack\.com)/\S+~i';
if (preg_match($pattern, $url, $m)) {
echo "<pre style=\"color: lime;\">DEBUG: Matched workchronicles URL {$m[0]}</pre>";
return $m[0];
}
echo "<pre style=\"color: yellow;\">DEBUG: Not a workchronicles URL</pre>";
return '';
}
function hook_workchronicles_render_linklist(array $data): array
{
echo "<pre style=\"color: lime;\">DEBUG: hook_workchronicles_render_linklist start</pre>";
if (empty($data['links']) || !is_array($data['links'])) {
echo "<pre style=\"color: yellow;\">DEBUG: Aucun lien à traiter</pre>";
return $data;
}
foreach ($data['links'] as &$link) {
$url = $link['url'] ?? '';
echo "<pre style=\"color: lime;\">DEBUG: Processing link {$url}</pre>";
$page = workchronicles_url($url);
if ('' === $page) {
echo "<pre style=\"color: yellow;\">DEBUG: Skipping non-workchronicles</pre>";
continue;
}
echo "<pre style=\"color: lime;\">DEBUG: Fetching page content from {$page}</pre>";
list($headers, $content) = get_http_response($page);
if (empty($content)) {
echo "<pre style=\"color: red;\">DEBUG: Empty content</pre>";
continue;
}
echo "<pre style=\"color: lime;\">DEBUG: Page content length: " . strlen($content) . "</pre>";
$imgUrl = '';
if (preg_match('/<meta\s+property="og:image"\s+content="([^"]+)"/i', $content, $m)) {
$imgUrl = $m[1];
echo "<pre style=\"color: lime;\">DEBUG: Found og:image {$imgUrl}</pre>";
}
if ('' === $imgUrl && preg_match('#<figure[^>]*>.*?<img[^>]+src="([^"]+)"#is', $content, $m2)) {
$imgUrl = $m2[1];
echo "<pre style=\"color: lime;\">DEBUG: Fallback found <figure> img src {$imgUrl}</pre>";
}
if ('' !== $imgUrl) {
$html = '<div class="wc-thumbnail">';
$html .= '<img src="' . htmlspecialchars($imgUrl, ENT_QUOTES, 'UTF-8') . '" ';
$html .= 'alt="" style="width:728px;height:728px;margin:0.5em 0;object-fit:contain;" />';
$html .= '</div>';
if (!empty($link['description'])) {
$link['description'] = $html . "\n\n" . $link['description'];
} else {
$link['description'] = $html;
}
echo "<pre style=\"color: lime;\">DEBUG: Injected thumbnail into description</pre>";
} else {
echo "<pre style=\"color: yellow;\">DEBUG: No image found (og:image nor <figure> img)</pre>";
}
}
unset($link);
echo "<pre style=\"color: lime;\">DEBUG: hook_workchronicles_render_linklist end</pre>";
return $data;
}
function hook_workchronicles_render_includes(array $data): array
{
echo "<pre style=\"color: lime;\">DEBUG: hook_workchronicles_render_includes</pre>";
$css = '<style>';
$css .= '.wc-thumbnail img { border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.2); display: block; margin: 0.5em auto; }';
$css .= '</style>';
$data['includes'][] = $css;
echo "<pre style=\"color: lime;\">DEBUG: Injected CSS</pre>";
return $data;
}
function workchronicles_dummy_translation()
{
t('WorkChronicles thumbnail plugin');
}