From c98967acb1135b25ebfa6fc3220fad9a87afacd3 Mon Sep 17 00:00:00 2001 From: Laurens Martina Date: Mon, 5 Oct 2020 20:04:36 +0200 Subject: [PATCH] figcaption: Adds figcaption Wraps images with title or data-attribution attribute in a figure element. Places the title in a figcaption element. Places the data-attribution in a small element. --- data/themes/default/hypha.css | 4 ++++ index.php | 1 + system/core/pages.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/data/themes/default/hypha.css b/data/themes/default/hypha.css index 835f90ca..52740165 100644 --- a/data/themes/default/hypha.css +++ b/data/themes/default/hypha.css @@ -525,6 +525,10 @@ img { height: auto; } +#main figure .title + .attribution:before { + content: " - "; +} + #main img.left { float: left; margin: 0 13px 10px 0; diff --git a/index.php b/index.php index 0d9afe1a..b2e40dfa 100644 --- a/index.php +++ b/index.php @@ -125,6 +125,7 @@ if ($hyphaPage) processCommandResult($hyphaPage->process($O_O->getRequest())); registerPostProcessingFunction('dewikify'); + registerPostProcessingFunction('add_captions_to_all_images'); // add hypha commands and navigation $_cmds[] = ''.__('index').''; diff --git a/system/core/pages.php b/system/core/pages.php index 5da80d22..79829fb6 100644 --- a/system/core/pages.php +++ b/system/core/pages.php @@ -601,6 +601,39 @@ function wikify_link($node) { $node->text(''); } + function add_captions_to_all_images($element) { + /** @var \DOMWrap\NodeList $img */ + // process images that reside within "main" + foreach ($element->findXPath('//*[@id="main"]//img[@title or @data-attribution]') as $img) { + // do not process images that reside within the wymeditor + if ($img->parents('.wymeditor')->count() === 0) { + add_caption_to_image($img); + } + } + } + + function add_caption_to_image($img) { + $doc = $img->document(); + + $img->wrap('
'); + $caption = $doc->createElement('figcaption'); + $img->after($caption); + + $title = $img->getAttribute('title'); + if ($title) { + $span = $doc->createElement('span', $title); + $span->addClass('title'); + $caption->append($span); + } + + $attribution = $img->getAttribute('data-attribution'); + if ($attribution) { + $small = $doc->createElement('small', $attribution); + $small->addClass('attribution'); + $caption->append($small); + } + } + /* Function: versionSelector generate html select element with available revisions for given page