Skip to content

Commit

Permalink
fixup! figcaption: Adds figcaption
Browse files Browse the repository at this point in the history
  • Loading branch information
matthijskooijman committed Oct 6, 2020
1 parent a26df84 commit 2f15bc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions data/themes/default/hypha.css
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ img {
height: auto;
}

#main figure .title + .attribution:before {
content: " - ";
}

#main img.left {
float: left;
margin: 0 13px 10px 0;
Expand Down
23 changes: 14 additions & 9 deletions system/core/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ function wikify_link($node) {
function add_captions_to_all_images($element) {
/** @var \DOMWrap\NodeList $img */
// process images that reside within "main"
foreach ($element->findXPath('//*[@id="main"]//img[@title] | //*[@id="main"]//img[@data-attribution]') as $img) {
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);
Expand All @@ -613,19 +613,24 @@ function add_captions_to_all_images($element) {
}

function add_caption_to_image($img) {
$figure = new DOMWrap\Element('figure');
$img->parentNode->insertBefore($figure, $img);
$figure->append($img);
$doc = $img->document();

$img->wrap('<figure>');
$caption = $doc->createElement('figcaption');
$img->after($caption);

$title = $img->getAttribute('title');
$text = $title ? $title : '';
$caption = new DOMWrap\Element('figcaption', $text);
$figure->append($caption);
if ($title) {
$span = $doc->createElement('span', $title);
$span->addClass('title');
$caption->append($span);
}

$attribution = $img->getAttribute('data-attribution');
if ($attribution) {
$small = new DOMWrap\Element('small', $attribution);
$figure->append($small);
$small = $doc->createElement('small', $attribution);
$small->addClass('attribution');
$caption->append($small);
}
}

Expand Down

0 comments on commit 2f15bc0

Please sign in to comment.