Skip to content

Commit

Permalink
Removed `\LastDragon_ru\LaraASP\Documentator\Markdown\Document::getTi…
Browse files Browse the repository at this point in the history
…tle()`, `\LastDragon_ru\LaraASP\Documentator\Markdown\Document::getSummary()`, `\LastDragon_ru\LaraASP\Documentator\Markdown\Document::getBody()`. The new extractions will be used instead.
  • Loading branch information
LastDragon-ru committed Jan 23, 2025
1 parent aab18e6 commit df87396
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 400 deletions.
123 changes: 1 addition & 122 deletions packages/documentator/src/Markdown/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,28 @@

namespace LastDragon_ru\LaraASP\Documentator\Markdown;

use Closure;
use LastDragon_ru\LaraASP\Core\Path\FilePath;
use LastDragon_ru\LaraASP\Documentator\Editor\Coordinate;
use LastDragon_ru\LaraASP\Documentator\Editor\Editor;
use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Location;
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Extraction;
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Markdown;
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Mutation;
use LastDragon_ru\LaraASP\Documentator\Markdown\Data\Lines;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Node\Block\Document as DocumentNode;
use League\CommonMark\Node\Block\Paragraph;
use League\CommonMark\Node\Node;
use Override;
use Stringable;

use function array_key_first;
use function array_key_last;
use function array_values;
use function count;
use function implode;
use function is_int;
use function mb_ltrim;
use function mb_trim;
use function str_ends_with;
use function str_starts_with;

// todo(documentator): There is no way to convert AST back to Markdown yet
// https://github.com/thephpleague/commonmark/issues/419

class Document implements Stringable {
private ?Editor $editor = null;
private ?string $title = null;
private ?string $summary = null;
private ?Editor $editor = null;

public function __construct(
protected readonly Markdown $markdown,
Expand All @@ -51,50 +37,6 @@ public function isEmpty(): bool {
return !$this->node->hasChildren() && count($this->node->getReferenceMap()) === 0;
}

/**
* Returns the first `# Header` if present.
*/
public function getTitle(): ?string {
if ($this->title === null) {
$title = $this->getFirstNode(Heading::class, static fn ($n) => $n->getLevel() === 1);
$title = $this->getBlockText($title) ?? '';
$title = mb_trim(mb_ltrim("{$title}", '#'));
$this->title = $title;
}

return $this->title !== '' ? $this->title : null;
}

/**
* Returns the first paragraph if present.
*/
public function getSummary(): ?string {
if ($this->summary === null) {
$summary = $this->getSummaryNode();
$summary = $this->getBlockText($summary);
$summary = mb_trim("{$summary}");
$this->summary = $summary;
}

return $this->summary !== '' ? $this->summary : null;
}

/**
* Returns the rest of the document text after the summary.
*/
public function getBody(): ?string {
$summary = $this->getSummaryNode();
$start = $summary?->getEndLine();
$end = array_key_last($this->getLines());
$body = $start !== null && is_int($end)
? $this->getText(new Location($start + 1, $end))
: null;
$body = mb_trim((string) $body);
$body = $body !== '' ? $body : null;

return $body;
}

/**
* @param iterable<array-key, Coordinate> $location
*/
Expand Down Expand Up @@ -133,69 +75,6 @@ protected function getEditor(): Editor {
return $this->editor;
}

/**
* @template T of Node
*
* @param class-string<T> $class
* @param Closure(T): bool|null $filter
* @param Closure(Node): bool|null $skip
*
* @return ?T
*/
private function getFirstNode(string $class, ?Closure $filter = null, ?Closure $skip = null): ?Node {
$node = null;

foreach ($this->node->children() as $child) {
// Comment?
if (
$child instanceof HtmlBlock
&& str_starts_with($child->getLiteral(), '<!--')
&& str_ends_with($child->getLiteral(), '-->')
) {
continue;
}

// Skipped?
if ($skip !== null && $skip($child)) {
continue;
}

// Wanted?
if ($child instanceof $class) {
if ($filter === null || $filter($child)) {
$node = $child;
}

break;
}

// End
break;
}

return $node;
}

private function getBlockText(?AbstractBlock $node): ?string {
$startLine = $node?->getStartLine();
$endLine = $node?->getEndLine();
$location = $startLine !== null && $endLine !== null
? new Location($startLine, $endLine)
: null;
$text = $location !== null
? $this->getText($location)
: null;

return $text;
}

private function getSummaryNode(): ?Paragraph {
$skip = static fn ($node) => $node instanceof Heading && $node->getLevel() === 1;
$node = $this->getFirstNode(Paragraph::class, skip: $skip);

return $node;
}

#[Override]
public function __toString(): string {
$lines = $this->getLines();
Expand Down
Loading

0 comments on commit df87396

Please sign in to comment.