Skip to content

Commit

Permalink
Merge pull request #2390 from splitbrain/footnote_metafix
Browse files Browse the repository at this point in the history
Fix for Issue#1250 Footnotes break metadata abstract saving
  • Loading branch information
splitbrain committed May 17, 2018
2 parents d6cf40c + 2ef8deb commit c966fcf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
47 changes: 47 additions & 0 deletions _test/tests/inc/parser/renderer_metadata.test.php
@@ -0,0 +1,47 @@
<?php

/**
* Class renderer_xhtml_test
*/
class renderer_metadata_test extends DokuWikiTest {
/** @var Doku_Renderer_xhtml */
protected $R;

/**
* Called for each test
*
* @throws Exception
*/
function setUp() {
parent::setUp();
$this->R = new Doku_Renderer_metadata();
}

function tearDown() {
unset($this->R);
}


function test_footnote_and_abstract() {
// avoid issues with the filectime() & filemtime in document_start() & document_end()
$now = time();
$this->R->persistent['date']['created'] = $now;
$this->R->persistent['date']['modified'] = $now;

$this->R->document_start();

$this->R->cdata("abstract: ");

$this->R->footnote_open();
$this->R->cdata(str_pad("footnote: ", Doku_Renderer_metadata::ABSTRACT_MAX, "lotsa junk "));
$this->R->footnote_close();

$this->R->cdata("abstract end.");

$this->R->document_end();

$expected = 'abstract: abstract end.';
$this->assertEquals($expected, $this->R->meta['description']['abstract']);
}

}
19 changes: 14 additions & 5 deletions inc/parser/metadata.php
Expand Up @@ -47,6 +47,9 @@ class Doku_Renderer_metadata extends Doku_Renderer {
/** @var string keeps the first image reference */
protected $firstimage = '';

/** @var bool whether or not data is being captured for the abstract, public to be accessible by plugins */
public $capturing = true;

/** @var bool determines if enough data for the abstract was collected, yet */
public $capture = true;

Expand Down Expand Up @@ -123,7 +126,7 @@ function document_end() {
* @param $text
*/
function cdata($text) {
if(!$this->capture) return;
if(!$this->capture || !$this->capturing) return;

$this->doc .= $text;

Expand Down Expand Up @@ -211,23 +214,29 @@ function hr() {
*/
function footnote_open() {
if($this->capture) {
// move current content to store and record footnote
// move current content to store
// this is required to ensure safe behaviour of plugins accessed within footnotes
$this->store = $this->doc;
$this->doc = '';

// disable capturing
$this->capturing = false;
}
}

/**
* Callback for footnote end syntax
*
* All rendered content is moved to the $footnotes array and the old
* content is restored from $store again
* All content rendered whilst within footnote syntax mode is discarded,
* the previously rendered content is restored and capturing is re-enabled.
*
* @author Andreas Gohr
*/
function footnote_close() {
if($this->capture) {
// restore old content
// re-enable capturing
$this->capturing = true;
// restore previously rendered content
$this->doc = $this->store;
$this->store = '';
}
Expand Down

0 comments on commit c966fcf

Please sign in to comment.