Skip to content

Commit

Permalink
Adding redirection-title:: directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed May 13, 2014
1 parent e7fc0cf commit f2ff874
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 5 deletions.
32 changes: 32 additions & 0 deletions Directives/RedirectionTitle.php
@@ -0,0 +1,32 @@
<?php

namespace Gregwar\RST\Directives;

use Gregwar\RST\Nodes\TitleNode;
use Gregwar\RST\Span;
use Gregwar\RST\Parser;
use Gregwar\RST\Directive;

/**
* This sets a new target for a following title, this can be used to change
* its link
*/
class RedirectionTitle extends Directive
{
public function getName()
{
return 'redirection-title';
}

public function process(Parser $parser, $node, $variable, $data, array $options)
{
$document = $parser->getDocument();

if ($node) {
if ($node instanceof TitleNode) {
$node->setTarget($data);
}
$document->addNode($node);
}
}
}
9 changes: 6 additions & 3 deletions Document.php
Expand Up @@ -93,7 +93,8 @@ public function getTocs()
* array(
* array('Main title', array(
* array('Sub title', array()),
* array('Sub title 2', array()
* array('Sub title 2', array(),
* array(array('Redirection', 'target'), array(),
* )
* )
*/
Expand All @@ -105,11 +106,13 @@ public function getTitles()
foreach ($this->nodes as $node) {
if ($node instanceof TitleNode) {
$level = $node->getLevel();
$text = $node->getValue() . '';
$text = (string)$node->getValue();
$redirection = $node->getTarget();
$value = $redirection ? array($text, $redirection) : $text;

if (isset($levels[$level-1])) {
$parent = &$levels[$level-1];
$element = array($text, array());
$element = array($value, array());
$parent[] = $element;
$levels[$level] = &$parent[count($parent)-1][1];
}
Expand Down
10 changes: 9 additions & 1 deletion HTML/Nodes/TocNode.php
Expand Up @@ -17,7 +17,15 @@ protected function renderLevel($url, $titles, $level = 1, $path = array())
$path[$level-1] = $k+1;
list($title, $childs) = $entry;
$token = 'title.'.implode('.', $path);
$html .= '<li><a href="'.$url.'#'.$token.'">'.$title.'</a></li>';
$target = $url.'#'.$token;

if (is_array($title)) {
list($title, $target) = $title;
$info = $this->environment->resolve('doc', $target);
$target = $this->environment->relativeUrl($info['url']);
}

$html .= '<li><a href="'.$target.'">'.$title.'</a></li>';

if ($childs) {
$html .= '<ul>';
Expand Down
3 changes: 2 additions & 1 deletion Kernel.php
Expand Up @@ -43,7 +43,8 @@ public function getDirectives()
new Directives\CodeBlock,
new Directives\Replace,
new Directives\Toctree,
new Directives\Document
new Directives\Document,
new Directives\RedirectionTitle
);
}

Expand Down
65 changes: 65 additions & 0 deletions Nodes/RedirectionNode.php
@@ -0,0 +1,65 @@
<?php

namespace Gregwar\RST\Nodes;

abstract class ListNode extends Node
{
protected $lines = array();

/**
* Infos contains:
*
* - text: the line text
* - depth: the depth in the list level
* - prefix: the prefix char (*, - etc.)
* - ordered: true of false if the list is ordered
*/
public function addLine(array $line)
{
$this->lines[] = $line;
}

public function render()
{
$depth = -1;
$value = '';
$stack = array();

foreach ($this->lines as $line) {
$prefix = $line['prefix'];
$text = $line['text'];
$ordered = $line['ordered'];
$newDepth = $line['depth'];

if ($depth < $newDepth) {
$tags = $this->createList($ordered);
$value .= $tags[0];
$stack[] = array($newDepth, $tags[1]."\n");
$depth = $newDepth;
}

while ($depth > $newDepth) {
$top = $stack[count($stack)-1];

if ($top[0] > $newDepth) {
$value .= $top[1];
array_pop($stack);
$top = $stack[count($stack)-1];
$depth = $top[0];
}
}

$value .= $this->createElement($text, $prefix)."\n";
}

while ($stack) {
list($d, $closing) = array_pop($stack);
$value .= $closing;
}

return $value;
}

abstract protected function createElement($text, $prefix);
abstract protected function createList($ordered);
}
11 changes: 11 additions & 0 deletions Nodes/TitleNode.php
Expand Up @@ -6,6 +6,7 @@ abstract class TitleNode extends Node
{
protected $level;
protected $token;
protected $target = null;

public function __construct($value, $level, $token)
{
Expand All @@ -18,4 +19,14 @@ public function getLevel()
{
return $this->level;
}

public function setTarget($target)
{
$this->target = $target;
}

public function getTarget()
{
return $this->target;
}
}
12 changes: 12 additions & 0 deletions tests/BuilderTests.php
Expand Up @@ -95,6 +95,18 @@ public function testSubDirective()
$this->assertContains('<h2>There is a title here</h2>', $contents);
}

/**
* Test that redirection-title worked
*/
public function testRedirectionTitle()
{
$contents = file_get_contents($this->targetFile('magic-link.html'));
$this->assertNotContains('redirection', $contents);

$contents = file_get_contents($this->targetFile('index.html'));
$this->assertContains('"subdirective.html">See also', $contents);
}

public function setUp()
{
shell_exec('rm -rf '.$this->targetFile());
Expand Down
7 changes: 7 additions & 0 deletions tests/builder/input/another.rst
Expand Up @@ -5,3 +5,10 @@ Another page
============

Hello!

.. redirection-title:: subdirective

See also
--------

See also: :doc:`subdirective`

0 comments on commit f2ff874

Please sign in to comment.