Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Fixed matching directives ending in newline (#1414)
  Perf optimize `Line::isDirective()` (#1412)
  Move method call out of hot loop (#1413)
  • Loading branch information
OskarStark committed May 22, 2023
2 parents 89a672d + 5b6fa08 commit cd3d809
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Analyzer/RstAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ public function analyze(\SplFileInfo $file, array $rules): array
*/
foreach ($lines->toIterator() as $no => $line) {
\assert(\is_int($no));
$lineIsBlank = $line->isBlank();

foreach ($lineContentRules as $rule) {
if ($lines->isProcessedBy($no, $rule::class)) {
continue;
}

if (!$rule::runOnlyOnBlankline() && $line->isBlank()) {
if (!$rule::runOnlyOnBlankline() && $lineIsBlank) {
continue;
}

Expand Down
30 changes: 21 additions & 9 deletions src/Value/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,29 @@ public function isHeadline(): bool
return $this->headline;
}

/**
* @todo use regex here
*/
public function isDirective(): bool
{
if (null === $this->isDirective) {
$this->isDirective = (
str_starts_with(ltrim($this->raw->toString()), '.. ')
&& !str_starts_with(ltrim($this->raw->toString()), '.. _`')
&& str_contains($this->raw->toString(), '::')
) || $this->isDefaultDirective();
$string = ltrim($this->raw->toString());
$len = strlen($string);

if ($len >= 2
&& $string[0] === '.'
&& $string[1] === '.'
) {
if (
!str_starts_with($string, '.. _`')
&& str_contains($string, '::'))
{
return $this->isDirective = true;
}
}

if ($this->isDefaultDirective()) {
return $this->isDirective = true;
}

$this->isDirective = false;
}

return $this->isDirective;
Expand All @@ -94,7 +106,7 @@ public function isDirective(): bool
public function isDefaultDirective(): bool
{
if (null === $this->isDefaultDirective) {
$string = $this->raw->toString();
$string = rtrim($this->raw->toString());
$len = strlen($string);

if ($len < 2 || $string[$len - 1] !== ':' || $string[$len - 2] !== ':') {
Expand Down
1 change: 1 addition & 0 deletions tests/Rst/Value/LineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public static function isDefaultDirectiveProvider(): \Generator
{
yield [true, 'this is using the default directive::'];
yield [true, 'prefixed classes included in doc block comments (``/** ... */``). For example::'];
yield [true, "this is using the default directive::\n"];

yield [false, ''];
yield [false, '.. code-block:: php'];
Expand Down

0 comments on commit cd3d809

Please sign in to comment.