-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathDirective.php
94 lines (83 loc) · 2.5 KB
/
Directive.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
<?php
namespace Gregwar\RST;
/**
* A directive is like a function you can call or apply to a block
* Il looks like:
*
* .. function:: main
* :arg1: value
* :arg2: otherValue
*
* Some block !
*
* The directive can define variables, create special nodes or change
* the node that directly follows it
*/
abstract class Directive
{
/**
* Get the directive name
*/
abstract public function getName();
/**
* This is the function called by the parser to process the directive, it can be overloaded
* to do anything with the document, like tweaking nodes or change the environment
*
* The node that directly follows the directive is also passed to it
*
* @param $parser the calling parser
* @param $node the node that follows the directive
* @param $variable the variable name of the directive
* @param $data the data of the directive (following ::)
* @param $options the array of options for this directive
*/
public function process(Parser $parser, $node, $variable, $data, array $options)
{
$document = $parser->getDocument();
$processNode = $this->processNode($parser, $variable, $data, $options);
if ($processNode) {
if ($variable) {
$environment = $parser->getEnvironment();
$environment->setVariable($variable, $processNode);
} else {
$document->addNode($processNode);
}
}
if ($node) {
$document->addNode($node);
}
}
/**
* This can be overloaded to write a directive that just create one node for the
* document, which is common
*
* The arguments are the same that process
*/
public function processNode(Parser $parser, $variable, $data, array $options)
{
$this->processAction($parser, $variable, $data, $options);
return null;
}
/**
* This can be overloaded to write a directive that just do an action without changing
* the nodes of the document
*
* The arguments are the same that process
*/
public function processAction(Parser $parser, $variabe, $data, array $options)
{
}
/**
* Called at the end of the parsing to finalize the document (add something or tweak nodes)
*/
public function finalize(Document &$document)
{
}
/**
* Should the following block be passed as a CodeNode?
*/
public function wantCode()
{
return false;
}
}