Skip to content

Commit

Permalink
Fixed doesn't show the heading when calling just a section.
Browse files Browse the repository at this point in the history
Complete rework of function _getSection().
  • Loading branch information
nerun committed Nov 27, 2022
1 parent dc999c1 commit 889ccc6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

2022-11-27 v0.6
* Fixed doesn't show the heading when calling just a section
* Complete rework of function _ _getSection()_

2022-11-25a v0.5.2
* Better fix for `{{template>page#section}}` doesn't exist with error message
* Updated error messages to look better
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Plugin for [DokuWiki](https://www.dokuwiki.org), available at [plugin: templater
Author : Jonathan Arkell
Updater : Daniel Dias Rodrigues
Current Version : 0.5.2 (2022-11-25a)
Current Version : 0.6 (2022-11-27)
10 changes: 10 additions & 0 deletions templater/debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Daniel Dias Rodrigues (aka Nerun) <danieldiasr@gmail.com>
*/
function printr($arr) {
echo "<pre>";
print_r($arr);
echo "</pre><br><br>";
}
?>
2 changes: 1 addition & 1 deletion templater/plugin.info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
base templater
author Jonathan Arkell (updated by Daniel Dias Rodrigues)
email jonnay@jonnay.net
date 2022-11-25a
date 2022-11-27
name Templater Plugin
desc Displays a wiki page (or a section thereof) within another, with user selectable replacements.
url https://www.dokuwiki.org/plugin:templater
68 changes: 37 additions & 31 deletions templater/syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* with bugfix from Ximin Luo <xl269@cam.ac.uk>
* Daniel Dias Rodrigues (aka Nerun) <danieldiasr@gmail.com>
* with one bugfix from jack126guy <halfgray7e@gmail.com>
* @version 0.5.2 (2022-11-25a)
* @version 0.6 (2022-11-27)
*/

define('BEGIN_REPLACE_DELIMITER', '@');
Expand All @@ -30,6 +30,7 @@
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once('debug.php');

/**
* All DokuWiki plugins to extend the parser/rendering mechanism
Expand All @@ -43,7 +44,7 @@ function getInfo() {
return array(
'author' => 'Jonathan Arkell (updated by Daniel Dias Rodrigues)',
'email' => 'jonnay@jonnay.net',
'date' => '2022-11-25a',
'date' => '2022-11-27',
'name' => 'Templater Plugin',
'desc' => 'Displays a wiki page (or a section thereof) within another, with user selectable replacements',
'url' => 'http://www.dokuwiki.org/plugin:templater',
Expand Down Expand Up @@ -108,7 +109,7 @@ function handle($match, $state, $pos, Doku_Handler $handler) {
if (array_key_exists(1, $wikipage)) {
$section = cleanID($wikipage[1]);
} else {
$section = "";
$section = null;
}

return array($wikipage[0], $replacers, $section);
Expand Down Expand Up @@ -169,22 +170,24 @@ function render($mode, Doku_Renderer $renderer, $data) {

// filter section if given
if ($data[2]) {
$getSection[] = $this->_getSection($data[2], $instr);
$instr = $getSection[0][0];
if(!is_null($getSection[0][1])) {
$renderer->doc .= sprintf($getSection[0][1], $data[2]);
$renderer->internalLink($data[0]);
$renderer->doc .= '.<br/><br/></div>';
$getSection = $this->_getSection($data[2], $instr);

$instr = $getSection[0];

if(!is_null($getSection[1])) {
$renderer->doc .= sprintf($getSection[1], $data[2]);
$renderer->internalLink($data[0]);
$renderer->doc .= '.<br/><br/></div>';
}
}

// correct relative internal links and media
$instr = $this->_correctRelNS($instr, $data[0]);

// doesn't show the heading for each template if {{template>page#section}} not {{template>page}}
if (array_key_exists(0, $instr[0][1])) {
if ($instr[0][1][0] == $data[2]) {
$instr[0][1][0] = "";
// doesn't show the heading for each template if {{template>page#section}}
if (sizeof($instr) > 0 && !isset($getSection[1])) {
if (array_key_exists(0, $instr[0][1]) && $instr[0][1][0] == $data[2]) {
$instr[0][1][0] = null;
}
}

Expand Down Expand Up @@ -214,37 +217,40 @@ function render($mode, Doku_Renderer $renderer, $data) {
* Get a section including its subsections
*/
function _getSection($title, $instructions) {
$i = (array) null;
$level = null;
$no_section = null;

foreach ($instructions as $instruction) {
if ($instruction[0] == 'header') {

// found the right header
if (cleanID($instruction[1][0]) == $title) {
$level = $instruction[1][1];
$i[] = $instruction;

// next header of the same level -> exit
} else if (isset($level)) {
if ($instruction[1][1] == $level) {
return $i;
} else {
if (isset($level) && isset($i)) {
if ($instruction[1][1] > $level) {
$i[] = $instruction;
// next header of the same level or higher -> exit
} else {
return array($i,null);
}
}
}

// add instructions from our section
} else if (isset($level)) {
$i[] = $instruction;
} else { // content between headers
if (isset($level) && isset($i)) {
$i[] = $instruction;
}
}
}

// Fix for when page#section doesn't exist
if(!isset($i)) {
$i[] = $instruction;
if(sizeof($i) == 0) {
$no_section = $this->getLang('no_such_section');
} else {
$no_section = null;
}

$array_i_nosection = array($i,$no_section);

return $array_i_nosection;
return array($i,$no_section);
}

/**
Expand Down

0 comments on commit 889ccc6

Please sign in to comment.