Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding redirection-title:: directive
- Loading branch information
Showing
8 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters