/** * Reduce prior sections, if a new section has been found. * * If a new section has been found all sections with a higher depth level * can be closed, and all items fitting into sections may be aggregated by * the respective sections as well. * * @param ezcDocumentRstNode $node * @return void */ protected function reduceSection(ezcDocumentRstNode $node) { // Collected node for prior section $collected = array(); $lastSectionDepth = -1; // Include all paragraphs, tables, lists and sections with a higher // nesting depth while ($child = $this->documentStack->shift()) { /* DEBUG echo " -> Try node: " . ezcDocumentRstNode::getTokenName( $child->type ) . ".\n"; // /DEBUG */ if (!in_array($child->type, $this->blockNodes, true)) { $this->triggerError(E_PARSE, "Unexpected node: " . ezcDocumentRstNode::getTokenName($child->type) . ".", null, $child->token->line, $child->token->position); } if ($child->type === ezcDocumentRstNode::SECTION) { if ($child->depth <= $node->depth && $node->depth !== 0) { // If the found section has a same or higher level, just // put everything back on the stack $child->nodes = array_merge($child->nodes, $collected); $this->documentStack->unshift($child); /* DEBUG echo " -> Leave on stack.\n"; $this->dumpStack(); // /DEBUG */ return $node; } // Reduce document, if reached if ($child->depth == 0 && $node->depth == 0) { /* DEBUG echo " -> Aggregate in root document node.\n"; // /DEBUG */ $child->nodes = array_merge($child->nodes, $collected); return $child; } // Check for title depth incosistency if ($lastSectionDepth - $child->depth > 1) { $this->triggerError(E_PARSE, "Title depth inconsitency.", null, $child->token->line, $child->token->position); } if ($lastSectionDepth === -1 || $lastSectionDepth > $child->depth) { // If the section level is higher then in our new node and // lower the the last node, reduce sections. /* DEBUG echo " -> Reduce section {$child->depth}.\n"; // /DEBUG */ $child->nodes = array_merge($child->nodes, $collected); $collected = array(); } // Sections on an equal level are just appended, for all // sections we remember the last depth. $lastSectionDepth = $child->depth; } /* DEBUG echo " -> Found another child...\n"; // /DEBUG */ array_unshift($collected, $child); } // No reduction found, put things back on stack. /* DEBUG echo " -> Put everything back on stack.\n"; // /DEBUG */ $this->documentStack->prepend(array_reverse($collected)); return $node; }