(.*<\/span>))/gm;
+
+ tokenList.forEach( function( value, index ) {
+ if ( index === tokenCount - 1 ) {
+ value = value.replace (regex, '' );
+ }
+
+ if ( showLines.includes ( index + 1 ) ) {
+ tokenList[index] += '\n';
+ }
+ else {
+ tokenList[index] = '' + value + '';
+ }
+ });
+
+ return tokenList;
+ }
+
+ function lineNumberList( lineNumberRows, showLines ) {
+ var lineNumberList = [];
+
+ lineNumberRows.forEach( function( value, index ) {
+ if ( !showLines.includes( index + 1 ) ) {
+ lineNumberRows[index].classList.add( 'hide-line' );
+ }
+
+ lineNumberList[index] = lineNumberRows[index].outerHTML;
+ });
+
+ return lineNumberList;
+ }
+});
\ No newline at end of file
diff --git a/src/Domain/ContentRenderer/CodeRenderer.php b/src/Domain/ContentRenderer/CodeRenderer.php
index 5db202f..eafb116 100644
--- a/src/Domain/ContentRenderer/CodeRenderer.php
+++ b/src/Domain/ContentRenderer/CodeRenderer.php
@@ -12,6 +12,7 @@ class CodeRenderer implements ContentRenderer {
public function __construct(
private string $language,
private bool $showLineNumbers,
+ private string $showSpecificLines,
private bool $showEditButton
) {
}
@@ -56,6 +57,10 @@ private function getWrapperAttributes( string $contentUrl ): array {
$attributes['class'] = $this->getWrapperClasses();
+ if ( !empty( $this->showSpecificLines ) ) {
+ $attributes['data-show-lines'] = $this->lineNormalizer( $this->showSpecificLines );
+ }
+
$attributes['data-toolbar-order'] = 'copy-to-clipboard';
if ( $this->showEditButton ) {
@@ -66,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, static function( $value ): bool {
+ if ( preg_match( '/^\d+$/', $value ) || preg_match( '/^(\d+)-(\d+)$/', $value ) ) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ } );
+
+ return implode( ',', $ranges );
+ }
}
diff --git a/src/Domain/ContentRenderer/RendererConfig.php b/src/Domain/ContentRenderer/RendererConfig.php
index ac5633e..d6e9502 100644
--- a/src/Domain/ContentRenderer/RendererConfig.php
+++ b/src/Domain/ContentRenderer/RendererConfig.php
@@ -10,6 +10,7 @@ public function __construct(
public string $fileExtension,
public string $language,
public bool $showLineNumbers,
+ public string $showSpecificLines,
public bool $showEditButton
) {
}
diff --git a/src/Domain/WikiContentRendererFactory.php b/src/Domain/WikiContentRendererFactory.php
index 88f1f71..40e39b6 100644
--- a/src/Domain/WikiContentRendererFactory.php
+++ b/src/Domain/WikiContentRendererFactory.php
@@ -15,6 +15,7 @@ public function createContentRenderer( RendererConfig $config ): ContentRenderer
return new CodeRenderer(
language: $config->language,
showLineNumbers: $config->showLineNumbers,
+ showSpecificLines: $config->showSpecificLines,
showEditButton: $config->showEditButton
);
}
@@ -27,6 +28,7 @@ public function createContentRenderer( RendererConfig $config ): ContentRenderer
return new CodeRenderer(
language: $config->fileExtension, // TODO: Use an extension-to-language map, although common extensions already work.
showLineNumbers: $config->showLineNumbers,
+ showSpecificLines: $config->showSpecificLines,
showEditButton: $config->showEditButton
);
diff --git a/src/UseCases/Embed/EmbedRequest.php b/src/UseCases/Embed/EmbedRequest.php
index 3332963..2a8886d 100644
--- a/src/UseCases/Embed/EmbedRequest.php
+++ b/src/UseCases/Embed/EmbedRequest.php
@@ -10,6 +10,7 @@ public function __construct(
public string $fileUrl,
public ?string $language,
public ?bool $showLineNumbers,
+ public ?string $showSpecificLines,
public bool $showEditButton
) {
}
diff --git a/src/UseCases/Embed/EmbedRequestBuilder.php b/src/UseCases/Embed/EmbedRequestBuilder.php
index 4eef34e..3b00cf1 100644
--- a/src/UseCases/Embed/EmbedRequestBuilder.php
+++ b/src/UseCases/Embed/EmbedRequestBuilder.php
@@ -14,11 +14,13 @@ public static function argumentsToRequest( array $arguments, bool $showEditButto
$language = $normalizedArguments['lang'] ?? null;
$line = $normalizedArguments['line'] ?? null;
+ $specificLines = $normalizedArguments['specificLines'] ?? null;
return new EmbedRequest(
fileUrl: $arguments[0],
language: is_string( $language ) ? $language : null,
showLineNumbers: is_bool( $line ) ? $line : null,
+ showSpecificLines: is_string( $specificLines ) ? $specificLines : null,
showEditButton: $showEditButton
);
}
diff --git a/src/UseCases/Embed/EmbedUseCase.php b/src/UseCases/Embed/EmbedUseCase.php
index c57c23e..6712da8 100644
--- a/src/UseCases/Embed/EmbedUseCase.php
+++ b/src/UseCases/Embed/EmbedUseCase.php
@@ -79,6 +79,7 @@ private function createRendererConfig( EmbedRequest $request ): RendererConfig {
),
language: $request->language ?? '',
showLineNumbers: $request->showLineNumbers ?? false,
+ showSpecificLines: $request->showSpecificLines ?? '',
showEditButton: $request->showEditButton
);
}
diff --git a/tests/Integration/Domain/CodeRendererTest.php b/tests/Integration/Domain/CodeRendererTest.php
index 792a486..608fdc6 100644
--- a/tests/Integration/Domain/CodeRendererTest.php
+++ b/tests/Integration/Domain/CodeRendererTest.php
@@ -15,42 +15,60 @@ class CodeRendererTest extends TestCase {
public function testRendersSimplePhpWithoutLineNumbers(): void {
$this->assertSame(
'print( "Hello world" );
',
- ( new CodeRenderer( language: 'php', showLineNumbers: false, showEditButton: false ) )->render( 'print( "Hello world" );', '' )
+ ( new CodeRenderer( language: 'php', showLineNumbers: false, showSpecificLines: '', showEditButton: false ) )->render( 'print( "Hello world" );', '' )
);
}
public function testRendersSimpleTypescriptWithLineNumbers(): void {
$this->assertSame(
'console.log( "Hello world" as string );
',
- ( new CodeRenderer( language: 'typescript', showLineNumbers: true, showEditButton: false ) )->render( 'console.log( "Hello world" as string );', '' )
+ ( new CodeRenderer( language: 'typescript', showLineNumbers: true, showSpecificLines: '', showEditButton: false ) )->render( 'console.log( "Hello world" as string );', '' )
);
}
public function testRendersEscapedHtml(): void {
$this->assertSame(
'<script>alert( "HAX" );</script>
',
- ( new CodeRenderer( language: 'html', showLineNumbers: false, showEditButton: false ) )->render( '', '' )
+ ( new CodeRenderer( language: 'html', showLineNumbers: false, showSpecificLines: '', showEditButton: false ) )->render( '', '' )
);
}
public function testRendersEscapedHtmlWithWrongLanguage(): void {
$this->assertSame(
'<script>alert( "HAX" );</script>
',
- ( new CodeRenderer( language: 'javascript', showLineNumbers: false, showEditButton: false ) )->render( '', '' )
+ ( new CodeRenderer( language: 'javascript', showLineNumbers: false, showSpecificLines: '', showEditButton: false ) )->render( '', '' )
);
}
public function testRendersEscapedPartialHtml(): void {
$this->assertSame(
'</code></pre>FOO
',
- ( new CodeRenderer( language: 'html', showLineNumbers: false, showEditButton: false ) )->render( 'FOO', '' )
+ ( new CodeRenderer( language: 'html', showLineNumbers: false, showSpecificLines: '', showEditButton: false ) )->render( 'FOO', '' )
);
}
public function testRendersShowEditButton(): void {
$this->assertSame(
'</code></pre>FOO
',
- ( new CodeRenderer( language: 'php', showLineNumbers: false, showEditButton: true ) )->render( 'FOO', 'http://bitbucket:7990/projects/TEST/repos/test/browse/test.php' )
+ ( new CodeRenderer( language: 'php', showLineNumbers: false, showSpecificLines: '', showEditButton: true ) )->render( 'FOO', 'http://bitbucket:7990/projects/TEST/repos/test/browse/test.php' )
+ );
+ }
+
+ public function testRendersSpecificLineNumbers(): void {
+ $this->assertSame(
+ '<?php
+ $a = "Hello";
+ $b = "World";
+
+ print( $a . " " . $b );
+ ?>
',
+ ( new CodeRenderer( language: 'php', showLineNumbers: true, showSpecificLines: '2-3,5', showEditButton: false ) )->render(
+ '', '' )
);
}
diff --git a/tests/Unit/Domain/WikiContentRendererFactoryTest.php b/tests/Unit/Domain/WikiContentRendererFactoryTest.php
index 9b89964..591bb2f 100644
--- a/tests/Unit/Domain/WikiContentRendererFactoryTest.php
+++ b/tests/Unit/Domain/WikiContentRendererFactoryTest.php
@@ -21,6 +21,7 @@ public function testMarkdownFileAndConfigWithoutLanguageCreatesMarkdownRenderer(
fileExtension: 'md',
language: '',
showLineNumbers: false,
+ showSpecificLines: '',
showEditButton: false
)
)
@@ -29,12 +30,13 @@ public function testMarkdownFileAndConfigWithoutLanguageCreatesMarkdownRenderer(
public function testConfigWithoutLanguageCreatesCodeRenderer(): void {
$this->assertEquals(
- new CodeRenderer( language: 'php', showLineNumbers: false, showEditButton: false ),
+ new CodeRenderer( language: 'php', showLineNumbers: false, showSpecificLines: '', showEditButton: false ),
( new WikiContentRendererFactory() )->createContentRenderer(
new RendererConfig(
fileExtension: 'php',
language: '',
showLineNumbers: false,
+ showSpecificLines: '',
showEditButton: false
)
)
@@ -43,12 +45,13 @@ public function testConfigWithoutLanguageCreatesCodeRenderer(): void {
public function testConfigWithLanguageCreatesCodeRenderer(): void {
$this->assertEquals(
- new CodeRenderer( language: 'php', showLineNumbers: false, showEditButton: false ),
+ new CodeRenderer( language: 'php', showLineNumbers: false, showSpecificLines: '', showEditButton: false ),
( new WikiContentRendererFactory() )->createContentRenderer(
new RendererConfig(
fileExtension: 'php',
language: 'php',
showLineNumbers: false,
+ showSpecificLines: '',
showEditButton: false
)
)
@@ -57,12 +60,13 @@ public function testConfigWithLanguageCreatesCodeRenderer(): void {
public function testConfigWithLanguageAndLineNumbersCreatesCodeRenderer(): void {
$this->assertEquals(
- new CodeRenderer( language: 'php', showLineNumbers: true, showEditButton: false ),
+ new CodeRenderer( language: 'php', showLineNumbers: true, showSpecificLines: '', showEditButton: false ),
( new WikiContentRendererFactory() )->createContentRenderer(
new RendererConfig(
fileExtension: 'php',
language: 'php',
showLineNumbers: true,
+ showSpecificLines: '',
showEditButton: false
)
)
@@ -71,12 +75,13 @@ public function testConfigWithLanguageAndLineNumbersCreatesCodeRenderer(): void
public function testConfigWithMarkdownLanguageCreatesCodeRenderer(): void {
$this->assertEquals(
- new CodeRenderer( language: 'md', showLineNumbers: false, showEditButton: false ),
+ new CodeRenderer( language: 'md', showLineNumbers: false, showSpecificLines: '', showEditButton: false ),
( new WikiContentRendererFactory() )->createContentRenderer(
new RendererConfig(
fileExtension: 'md',
language: 'md',
showLineNumbers: false,
+ showSpecificLines: '',
showEditButton: false
)
)
diff --git a/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php b/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php
index e328699..fb073af 100644
--- a/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php
+++ b/tests/Unit/UseCases/Embed/EmbedUseCaseTest.php
@@ -70,6 +70,7 @@ private function createRequest( string $fileUrl ): EmbedRequest {
fileUrl: $fileUrl,
language: '',
showLineNumbers: false,
+ showSpecificLines: '',
showEditButton: false
);
}