Skip to content

Commit

Permalink
Merge pull request #912 from phpDocumentor/bugfix/emphasize-lines
Browse files Browse the repository at this point in the history
[BUGFIX] Allow emphasize-lines with open end
  • Loading branch information
jaapio authored Mar 9, 2024
2 parents f9d8964 + 1f6fabc commit feb980d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Psr\Log\LoggerInterface;

use function preg_match;
use function sprintf;
use function strval;
use function trim;

Expand All @@ -30,8 +31,8 @@
*/
final class DefaultCodeNodeOptionMapper implements CodeNodeOptionMapper
{
/** @see https://regex101.com/r/I3KttH/1 */
public const LINE_NUMBER_RANGES_REGEX = '/^\d+(-\d+)?(?:,\s*\d+(-\d+)?)*$/';
/** @see https://regex101.com/r/czvfnV/2 */
public const LINE_NUMBER_RANGES_REGEX = '/^\d+(-\d*)?(?:,\s*\d+(-\d*)?)*$/';

public function __construct(
private readonly LoggerInterface $logger,
Expand Down Expand Up @@ -79,7 +80,10 @@ private function setEmphasizeLinesBasedOnOptions(array $options, CodeNode $node,
$emphasizeLines = (string) $options['emphasize-lines']->getValue();
if (!preg_match(self::LINE_NUMBER_RANGES_REGEX, $emphasizeLines)) {
// Input does not fit the pattern, log a warning
$this->logger->warning('Invalid value for option emphasize-lines. Expected format: \'1-5, 7, 33\'', $blockContext->getLoggerInformation());
$this->logger->warning(
sprintf('Invalid value for option emphasize-lines: "%s". Expected format: \'1-5, 7, 33\'', $emphasizeLines),
$blockContext->getLoggerInformation(),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ <h1>Title</h1>
print(&#039;This line is highlighted.&#039;)
print(&#039;This one is not...&#039;)
print(&#039;...but this one is.&#039;)</code></pre>
<pre><code class="language-yaml line-numbers" data-start="1" data-emphasize-lines="3-">Email:
formEditor:
predefinedDefaults:
defaultValue: &#039;&#039;
validators:
-
identifier: EmailAddress</code></pre>
</div>

<!-- content end -->
12 changes: 12 additions & 0 deletions tests/Integration/tests/code/code-block-emphasize/input/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ Title
print('This line is highlighted.')
print('This one is not...')
print('...but this one is.')
.. code-block:: yaml
:linenos:
:emphasize-lines: 3-
Email:
formEditor:
predefinedDefaults:
defaultValue: ''
validators:
-
identifier: EmailAddress

0 comments on commit feb980d

Please sign in to comment.