1 | <?php |
||
23 | class DocParser |
||
24 | { |
||
25 | /** |
||
26 | * @var Environment |
||
27 | */ |
||
28 | protected $environment; |
||
29 | |||
30 | /** |
||
31 | * @var InlineParserEngine |
||
32 | */ |
||
33 | private $inlineParserEngine; |
||
34 | |||
35 | /** |
||
36 | * @param Environment $environment |
||
37 | */ |
||
38 | 1944 | public function __construct(Environment $environment) |
|
43 | |||
44 | /** |
||
45 | * @return Environment |
||
46 | */ |
||
47 | 1944 | public function getEnvironment() |
|
51 | |||
52 | /** |
||
53 | * @param string $input |
||
54 | * |
||
55 | * @return string[] |
||
56 | */ |
||
57 | 1935 | private function preProcessInput($input) |
|
58 | { |
||
59 | 1935 | $lines = preg_split('/\r\n|\n|\r/', $input); |
|
60 | |||
61 | // Remove any newline which appears at the very end of the string. |
||
62 | // We've already split the document by newlines, so we can simply drop |
||
63 | // any empty element which appears on the end. |
||
64 | 1935 | if (end($lines) === '') { |
|
65 | 1926 | array_pop($lines); |
|
66 | 1284 | } |
|
67 | |||
68 | 1935 | return $lines; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param string $input |
||
73 | * |
||
74 | * @return Document |
||
75 | */ |
||
76 | 1935 | public function parse($input) |
|
77 | 2 | { |
|
78 | 1935 | $context = new Context(new Document(), $this->getEnvironment()); |
|
79 | |||
80 | 1935 | $lines = $this->preProcessInput($input); |
|
81 | 1935 | foreach ($lines as $line) { |
|
82 | 1932 | $context->setNextLine($line); |
|
83 | 1932 | $this->incorporateLine($context); |
|
84 | 1290 | } |
|
85 | |||
86 | 1935 | while ($context->getTip()) { |
|
87 | 1935 | $context->getTip()->finalize($context, count($lines)); |
|
88 | 1290 | } |
|
89 | |||
90 | 1935 | $this->processInlines($context, $context->getDocument()->walker()); |
|
91 | |||
92 | 1935 | $this->processDocument($context); |
|
93 | |||
94 | 1935 | return $context->getDocument(); |
|
95 | } |
||
96 | |||
97 | 1932 | private function incorporateLine(ContextInterface $context) |
|
135 | |||
136 | 1935 | private function processDocument(ContextInterface $context) |
|
137 | { |
||
138 | 1935 | foreach ($this->getEnvironment()->getDocumentProcessors() as $documentProcessor) { |
|
139 | $documentProcessor->processDocument($context->getDocument()); |
||
140 | 1290 | } |
|
141 | 1935 | } |
|
142 | |||
143 | 1935 | private function processInlines(ContextInterface $context, NodeWalker $walker) |
|
156 | |||
157 | /** |
||
158 | * Sets the container to the last open child (or its parent) |
||
159 | * |
||
160 | * @param ContextInterface $context |
||
161 | * @param Cursor $cursor |
||
162 | */ |
||
163 | 1932 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
182 | |||
183 | /** |
||
184 | * Parse blocks |
||
185 | * |
||
186 | * @param ContextInterface $context |
||
187 | * @param Cursor $cursor |
||
188 | */ |
||
189 | 1932 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
206 | |||
207 | /** |
||
208 | * @param ContextInterface $context |
||
209 | * @param Cursor $cursor |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 1932 | private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor) |
|
220 | |||
221 | /** |
||
222 | * @param ContextInterface $context |
||
223 | * @param Cursor $cursor |
||
224 | */ |
||
225 | 1932 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
243 | } |
||
244 |