/** * Finalize the block; mark it closed for modification * * @param int $lineNumber * @param InlineParser $inlineParser * @param ReferenceMap $refMap */ public function finalize($lineNumber, CommonMark_InlineParser $inlineParser, CommonMark_Reference_ReferenceMap $refMap) { if (!$this->open) { return; } $this->open = false; if ($lineNumber > $this->startLine) { $this->endLine = $lineNumber - 1; } else { $this->endLine = $lineNumber; } switch ($this->getType()) { case self::TYPE_PARAGRAPH: $this->stringContent = preg_replace('/^ */m', '', implode("\n", $this->strings->toArray())); // Try parsing the beginning as link reference definitions: while ($this->stringContent[0] === '[' && ($pos = $inlineParser->parseReference($this->stringContent, $refMap))) { $this->stringContent = substr($this->stringContent, $pos); if ($this->isStringContentBlank()) { //RegexHelper::getInstance()->isBlank($this->stringContent)) { $this->type = self::TYPE_REFERENCE_DEF; break; } } break; case self::TYPE_ATX_HEADER: case self::TYPE_SETEXT_HEADER: case self::TYPE_HTML_BLOCK: $this->stringContent = implode("\n", $this->strings->toArray()); break; case self::TYPE_INDENTED_CODE: $reversed = array_reverse($this->strings->toArray(), true); foreach ($reversed as $index => $line) { if ($line == '' || $line === "\n" || preg_match('/^(\\n *)$/', $line)) { unset($reversed[$index]); } else { break; } } $fixed = array_reverse($reversed); $tmp = implode("\n", $fixed); if (substr($tmp, -1) !== "\n") { $tmp .= "\n"; } $this->stringContent = $tmp; break; case self::TYPE_FENCED_CODE: // first line becomes info string $this->setExtra('info', CommonMark_Util_RegexHelper::unescape(trim($this->strings->first()))); if ($this->strings->count() == 1) { $this->stringContent = ''; } else { $this->stringContent = implode("\n", $this->strings->slice(1)) . "\n"; } break; case self::TYPE_LIST: $this->setExtra('tight', true); // tight by default $numItems = $this->children->count(); $i = 0; while ($i < $numItems) { /** @var BlockElement $item */ $item = $this->children->get($i); // check for non-final list item ending with blank line: $lastItem = $i == $numItems - 1; if ($item->endsWithBlankLine() && !$lastItem) { $this->setExtra('tight', false); break; } // Recurse into children of list item, to see if there are // spaces between any of them: $numSubItems = $item->getChildren()->count(); $j = 0; while ($j < $numSubItems) { $subItem = $item->getChildren()->get($j); $lastSubItem = $j == $numSubItems - 1; if ($subItem->endsWithBlankLine() && !($lastItem && $lastSubItem)) { $this->setExtra('tight', false); break; } $j++; } $i++; } break; default: break; } }