/** * extractHeaderContents returns a HeaderList containing HeaderContent objects. * * @param \nochso\WriteMe\Document $document * * @return \nochso\WriteMe\Markdown\HeaderList * * @todo Refactor this as it mostly duplicates extractHeaders */ public function extractHeaderContents(Document $document) { $headerList = new HeaderList(); $lines = Multiline::create($document->getContent()); $prevLine = null; $isFenced = false; $currentHeaderContent = null; $isNewHeader = false; foreach ($lines as $key => $line) { if (preg_match('/^```(?!`)/', $line)) { $isFenced = !$isFenced; } if (!$isFenced) { $header = $this->extractHeader($line, $prevLine, $key); if ($header !== null) { $currentHeaderContent = HeaderContent::fromHeader($header); $headerList->add($currentHeaderContent); $isNewHeader = true; } } // Add content only if it's *after* the header line if ($currentHeaderContent !== null && !$isNewHeader) { $currentHeaderContent->addContent($line); } $prevLine = $line; $isNewHeader = false; } return $headerList; }
/** * extractFirstCall to a Placeholder method from a Document. * * @param \nochso\WriteMe\Document $document * @param int $priority * @param int $offset * * @return Call|null */ public static function extractFirstCall(Document $document, $priority = \nochso\WriteMe\Interfaces\Placeholder::PRIORITY_FIRST, $offset = 0) { $call = null; $content = $document->getContent(); if ($offset > 0) { $content = mb_substr($content, $offset); } if (preg_match(self::REGEX, $content, $matches, PREG_OFFSET_CAPTURE) === 1) { $call = new self(); $call->document = $document; $call->priority = $priority; $call->identifier = $matches[3][0]; $call->rawCall = $matches[0][0]; // Position of the match including additional offset from before $call->rawCallFirstPosition = $matches[0][1] + $offset; if (isset($matches[5]) && $matches[5][0] !== '') { $call->method = $matches[5][0]; } if (isset($matches[7])) { $call->extractParameters($matches[7][0]); } } return $call; }
private function showDiff(Document $document, $existingFilepath) { $before = file_get_contents($existingFilepath); $after = $document->getContent(); $diff = Diff\Diff::create($before, $after); $this->stdio->out('Differences to existing file:'); if (!count($diff->getDiffLines())) { $this->stdio->outln(' <<yellow>>None<<reset>>'); $this->stdio->outln(); return; } $this->stdio->outln(); $this->stdio->outln(); if ($this->stdio->getStdout()->isPosix()) { $t = new Diff\Format\Template\POSIX(); } else { $t = new Diff\Format\Template\Text(); } $this->stdio->outln($t->format($diff)); $this->stdio->outln(); }