-
Notifications
You must be signed in to change notification settings - Fork 22
/
Document.php
152 lines (124 loc) · 3.43 KB
/
Document.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Gregwar\RST;
use Gregwar\RST\Nodes\Node;
use Gregwar\RST\Nodes\TitleNode;
use Gregwar\RST\Nodes\TocNode;
use Gregwar\RST\Nodes\RawNode;
abstract class Document extends Node
{
protected $environment;
protected $headerNodes = array();
protected $nodes = array();
public function __construct(Environment $environment)
{
$this->environment = $environment;
}
public function getEnvironment()
{
return $this->environment;
}
public function renderDocument()
{
return $this->render();
}
/**
* Getting all nodes of the document that satisfies the given
* function. If the function is null, all the nodes are returned.
*/
public function getNodes($function = null)
{
$nodes = array();
if ($function == null) {
return $this->nodes;
}
foreach ($this->nodes as $node) {
if ($function($node)) {
$nodes[] = $node;
}
}
return $nodes;
}
/**
* Gets the main title of the document
*/
public function getTitle()
{
foreach ($this->nodes as $node) {
if ($node instanceof TitleNode && $node->getLevel() == 1) {
return $node->getValue().'';
}
}
return null;
}
/**
* Get the table of contents of the document
*/
public function getTocs()
{
$tocs = array();
$nodes = $this->getNodes(function($node) {
return $node instanceof TocNode;
});
foreach ($nodes as $toc) {
$files = $toc->getFiles();
foreach ($files as &$file) {
$file = $this->environment->canonicalUrl($file);
}
$tocs[] = $files;
}
return $tocs;
}
/**
* Gets the titles hierarchy in arrays, for instance :
*
* array(
* array('Main title', array(
* array('Sub title', array()),
* array('Sub title 2', array(),
* array(array('Redirection', 'target'), array(),
* )
* )
*/
public function getTitles()
{
$titles = array();
$levels = array(&$titles);
foreach ($this->nodes as $node) {
if ($node instanceof TitleNode) {
$level = $node->getLevel();
$text = (string)$node->getValue();
$redirection = $node->getTarget();
$value = $redirection ? array($text, $redirection) : $text;
if (isset($levels[$level-1])) {
$parent = &$levels[$level-1];
$element = array($value, array());
$parent[] = $element;
$levels[$level] = &$parent[count($parent)-1][1];
}
}
}
return $titles;
}
public function addNode($node)
{
if (is_string($node)) {
$node = new RawNode($node);
}
if (!$node instanceof Node) {
$this->getEnvironment()->getErrorManager('addNode($node): $node should be a string or a Node');
}
$this->nodes[] = $node;
}
public function prependNode(Node $node)
{
array_unshift($this->nodes, $node);
}
public function addHeaderNode(Node $node)
{
$this->headerNodes[] = $node;
}
public function __toString()
{
return $this->render();
}
}