Skip to content

Commit 536f5f8

Browse files
committed
feat: new rendererOption: spaceToHtmlTag
convert spaces/tabs into HTML codes like `<span class="ch sp"> </span>` and the frontend is responsible for rendering them with CSS. when using this, "spacesToNbsp" should be false and "tabSize" is not respected. Signed-off-by: Jack Cherng <[email protected]>
1 parent 47f9e74 commit 536f5f8

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

example/demo_base.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
'separateBlock' => true,
3838
// show the (table) header
3939
'showHeader' => true,
40+
// convert spaces/tabs into HTML codes like `<span class="ch sp"> </span>`
41+
// and the frontend is responsible for rendering them with CSS.
42+
// when using this, "spacesToNbsp" should be false and "tabSize" is not respected.
43+
'spaceToHtmlTag' => false,
4044
// the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
4145
// but if you want to visualize them in the backend with "&nbsp;", you can set this to true
4246
'spacesToNbsp' => false,

src/Renderer/AbstractRenderer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ abstract class AbstractRenderer implements RendererInterface
6666
'separateBlock' => true,
6767
// show the (table) header
6868
'showHeader' => true,
69+
// convert spaces/tabs into HTML codes like `<span class="ch sp"> </span>`
70+
// and the frontend is responsible for rendering them with CSS.
71+
// when using this, "spacesToNbsp" should be false and "tabSize" is not respected.
72+
'spaceToHtmlTag' => false,
6973
// the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
7074
// but if you want to visualize them in the backend with "&nbsp;", you can set this to true
7175
'spacesToNbsp' => false,

src/Renderer/Html/AbstractHtml.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,20 @@ protected function formatLines(array $lines): array
255255
*/
256256
protected function formatStringFromLines(string $string): string
257257
{
258-
$string = $this->expandTabs($string, $this->options['tabSize']);
258+
if (!$this->options['spaceToHtmlTag']) {
259+
$string = $this->expandTabs($string, $this->options['tabSize']);
260+
}
261+
259262
$string = $this->htmlSafe($string);
260263

261264
if ($this->options['spacesToNbsp']) {
262265
$string = $this->htmlFixSpaces($string);
263266
}
264267

268+
if ($this->options['spaceToHtmlTag']) {
269+
$string = $this->htmlReplaceSpacesToHtmlTag($string);
270+
}
271+
265272
return $string;
266273
}
267274

@@ -315,6 +322,21 @@ protected function htmlFixSpaces(string $string): string
315322
return str_replace(' ', '&nbsp;', $string);
316323
}
317324

325+
/**
326+
* Replace spaces/tabs with HTML tags, which may be styled in frontend with CSS.
327+
*
328+
* @param string $string the string of spaces
329+
*
330+
* @return string the HTML representation of the string
331+
*/
332+
protected function htmlReplaceSpacesToHtmlTag(string $string): string
333+
{
334+
return strtr($string, [
335+
' ' => '<span class="ch sp"> </span>',
336+
"\t" => "<span class=\"ch tab\">\t</span>",
337+
]);
338+
}
339+
318340
/**
319341
* Make sure the "changes" array uses int "tag".
320342
*

0 commit comments

Comments
 (0)