From e6b5dd70b2bca478639a55c2386a99cde4e944ee Mon Sep 17 00:00:00 2001 From: Muhammad Yousuf Fazal Date: Wed, 25 Oct 2023 14:33:09 +0500 Subject: [PATCH] Normalization of Variable Signed-off-by: Muhammad Yousuf Fazal --- src/Domain/ContentRenderer/CodeRenderer.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Domain/ContentRenderer/CodeRenderer.php b/src/Domain/ContentRenderer/CodeRenderer.php index 2cd909b..5e4d46f 100644 --- a/src/Domain/ContentRenderer/CodeRenderer.php +++ b/src/Domain/ContentRenderer/CodeRenderer.php @@ -58,7 +58,7 @@ private function getWrapperAttributes( string $contentUrl ): array { $attributes['class'] = $this->getWrapperClasses(); if ( !empty( $this->showSpecificLines ) ) { - $attributes['data-show-lines'] = $this->showSpecificLines; + $attributes['data-show-lines'] = $this->lineNormalizer( $this->showSpecificLines ); } $attributes['data-toolbar-order'] = 'copy-to-clipboard'; @@ -71,4 +71,21 @@ private function getWrapperAttributes( string $contentUrl ): array { return $attributes; } + /** + * @return string + */ + private function lineNormalizer( string $lines ) { + $exploded = explode( ',', preg_replace( '/\s+/', '', $lines ) ); + + $ranges = array_filter( $exploded, function( $value ) { + if ( preg_match( '/^\d+$/', $value ) || preg_match( '/^(\d+)-(\d+)$/', $value ) ) { + return true; + } + else { + return false; + } + }); + + return implode( ',', $ranges ); + } }