/** * 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; }
/** * @param \nochso\WriteMe\Document $doc * * @return \Nette\Utils\Finder */ private function getFiles(Document $doc) { $findFiles = $this->options->getValue('api.file'); $fromFolders = $this->options->getValue('api.from'); $folderExclude = $this->options->getValue('api.folder-exclude'); $docPath = $doc->getFilepath(); // Make folder paths relative to the folder of the WRITEME file in case CWD differs. if ($docPath !== null) { $fromFolders = $this->makeFoldersRelativeToFile($docPath, $fromFolders); $folderExclude = $this->makeFoldersRelativeToFile($docPath, $folderExclude); } $files = Finder::findFiles($findFiles)->from($fromFolders)->exclude($folderExclude); return $files; }
/** * @param \nochso\WriteMe\Placeholder\Call $call * * @return \nochso\WriteMe\Markdown\HeaderContent[] */ private function getHeaderContents(Call $call) { $changelogPath = $this->findChangelog($call->getDocument()); $changelog = Document::fromFile($changelogPath); $parser = new HeaderParser(); $headerContentList = $parser->extractHeaderContents($changelog); $maxChanges = $this->options->getValue('changelog.max-changes'); $releaseLevel = $this->options->getValue('changelog.release-level'); $changes = 0; /** @var HeaderContent[] $displayHeaders */ $displayHeaders = []; /** @var HeaderContent $headerContent */ foreach ($headerContentList->getHeaders() as $headerContent) { // This header marks a release if ($headerContent->getLevel() === $releaseLevel) { $changes++; // Stop if we reached the max amount of changes. if ($changes > $maxChanges) { break; } $displayHeaders[] = $headerContent; } // Keep adding sub-HeaderContent if we're within a release if ($changes > 0 && $headerContent->getLevel() > $releaseLevel) { $displayHeaders[] = $headerContent; } } return $displayHeaders; }
/** * 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(); }
/** * Prepare options by merging default options with frontmatter. * * @param \nochso\WriteMe\Document $document */ public function prepare(Document $document) { $this->options = $this->getDefaultOptionList(); $this->options->prepare($document->getFrontmatter()); }
/** * @dataProvider saveTargetExceptionProvider * * @param \nochso\WriteMe\Document $document */ public function testSaveTarget_WhenNoTargetDetected_MustThrow(Document $document) { $this->expectException('RuntimeException'); $document->saveTarget(); }