Skip to content

Commit

Permalink
figcaption: Adds figcaption
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laurensmartina committed Oct 6, 2020
1 parent e00160a commit c98967a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 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
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = '<a class="index" href="index/'.$hyphaLanguage.'">'.__('index').'</a>';
Expand Down
33 changes: 33 additions & 0 deletions system/core/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<figure>');
$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
Expand Down

0 comments on commit c98967a

Please sign in to comment.