Skip to content

Commit

Permalink
Add -sidebar option
Browse files Browse the repository at this point in the history
to fix issue #147
  • Loading branch information
gturri committed May 5, 2023
1 parent 0825c16 commit d768e3e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
16 changes: 16 additions & 0 deletions _tests/src/test/java/nspages/Test_nsPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ public void explicitDefaultPath(){
assertSameLinks(currentNsLinks());
}

@Test
// This test does not really represent an actual use case since this
// option is meant to be used in a sidebar (where the current ns is not
// the same as the one of the page where the nspages plugin is used)
// but this still makes sure that this case is not obviously broken
public void sidebarOptionUsesCurrentNamespace(){
generatePage("autrens:start", "<nspages -sidebar>");
assertSameLinks(currentNsLinks());
}

@Test
public void sidebarOptionDoesNotAcceptAnExplicitNs(){
generatePage("autrens:start", "<nspages -sidebar some_name>");
assertThat(getDriver().getPageSource(), JUnitMatchers.containsString("With the -sidebar option you cannot specify a namespace"));
}

@Test
public void unsafePath(){
generatePage("autrens:start", "<nspages ..:..>");
Expand Down
1 change: 1 addition & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
$lang['continued'] = ' cont.';
$lang['nopages'] = 'No pages in this namespace.';
$lang['nosubns'] = 'No subnamespaces.';
$lang['sidebarOrNs'] = 'With the -sidebar option you cannot specify a namespace. Found: ';
1 change: 1 addition & 0 deletions lang/fr/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
$lang['continued'] = ' (suite)';
$lang['nopages'] = 'Pas de pages dans cette catégorie.';
$lang['nosubns'] = 'Pas de sous-catégories.';
$lang['sidebarOrNs'] = 'Avec l\'option -sidebar il n\'est pas possible de spécifier de catégorie. Trouvé: ';
9 changes: 6 additions & 3 deletions namespaceFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ function __construct($path){
$this->sanitizeNs();
}

private function computeWantedNs($path){
private function computeWantedNs($wantedNS){
global $ID;
$result = '';
$wantedNS = trim($path);
if($wantedNS == '') {
$wantedNS = $this->getCurrentNamespace();
}
Expand Down Expand Up @@ -76,6 +75,10 @@ function isNsSafe(){
}

function getWantedDirectory(){
return utf8_encodeFN(str_replace(':', '/', $this->wantedNs));
return $this->namespaceToDirectory($this->wantedNs);
}

static function namespaceToDirectory($ns){
return utf8_encodeFN(str_replace(':', '/', $ns));
}
}
14 changes: 11 additions & 3 deletions printers/printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ function printTOC($tab, $type, $text, $hideno){
abstract function _print($tab, $type);

function printUnusableNamespace($wantedNS){
$this->renderer->section_open(1);
$this->renderer->cdata($this->plugin->getLang('doesntexist').$wantedNS);
$this->renderer->section_close();
$this->printError($this->plugin->getLang('doesntexist').$wantedNS);
}

function printErrorSidebarDoestAcceptNamespace($wantedNS){
$this->printError($this->plugin->getLang('sidebarOrNs').$wantedNS);
}

private function printError($errorMessage){
$this->renderer->section_open(1);
$this->renderer->cdata($errorMessage);
$this->renderer->section_close();
}

private function _printHeader(&$tab, $type, $text, $hideno) {
Expand Down
35 changes: 30 additions & 5 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function handle($match, $state, $pos, Doku_Handler $handler) {
optionParser::checkOption($match, "(modification)?Dates?OnPictures?", $return['modificationDateOnPictures'], true);
optionParser::checkOption($match, "displayModificationDates?", $return["displayModificationDate"], true);
optionParser::checkOption($match, "includeItemsInTOC", $return["includeItemsInTOC"], true);
optionParser::checkOption($match, "sidebar", $return["sidebar"], true);
optionParser::checkRecurse($match, $return['maxDepth']);
optionParser::checkNbColumns($match, $return['nbCol']);
optionParser::checkSimpleStringArgument($match, $return['textPages'], $this, 'textPages');
Expand All @@ -91,10 +92,16 @@ function handle($match, $state, $pos, Doku_Handler $handler) {
optionParser::checkSimpleStringArgument($match, $return['defaultPicture'], $this, 'defaultPicture');

//Now, only the wanted namespace remains in $match
$nsFinder = new namespaceFinder($match);
$return['wantedNS'] = $nsFinder->getWantedNs();
$return['safe'] = $nsFinder->isNsSafe();
$return['wantedDir'] = $nsFinder->getWantedDirectory();
$match = trim($match);
if ($return["sidebar"]) {
// Don't bother resolving or sanitizing now: it will be done at render-time in this mode
$return['wantedNS'] = $match;
} else {
$nsFinder = new namespaceFinder($match);
$return['wantedNS'] = $nsFinder->getWantedNs();
$return['safe'] = $nsFinder->isNsSafe();
$return['wantedDir'] = $nsFinder->getWantedDirectory();
}

return $return;
}
Expand All @@ -119,7 +126,7 @@ private function _getDefaultOptions(){
'modificationDateOnPictures' => false,
'displayModificationDate' => false,
'sortByCreationDate' => false, 'defaultPicture' => null, 'tree' => false,
'includeItemsInTOC' => false
'includeItemsInTOC' => false, 'sidebar' => false,
);
}

Expand All @@ -137,6 +144,24 @@ function render($mode, Doku_Renderer $renderer, $data) {
$this->_denullifyPictureOptions($data);
$printer = $this->_selectPrinter($mode, $renderer, $data);

if ($data['sidebar']) {
if ($data['wantedNS'] !== '') {
$printer->printErrorSidebarDoestAcceptNamespace($data['wantedNS']);
return TRUE;
}
if ($mode === "metadata") {
// In this case $INFO is null so there is not much we can do,
// but anyway in "sidebar" mode we're not really going to generate metadata of the current page
// so it rather makes sense to exiting without printing anything.
return TRUE;
}
global $INFO;
$data['wantedNS'] = $INFO['namespace'];
$data['safe'] = true;
$data['wantedDir'] = namespaceFinder::namespaceToDirectory($data['wantedNS']);
}


if( ! $this->_isNamespaceUsable($data)){
$printer->printUnusableNamespace($data['wantedNS']);
return TRUE;
Expand Down

0 comments on commit d768e3e

Please sign in to comment.