Skip to content

HTMLTable adds an extra empty line when delegated to HTMLLineHighlighter #2184

@OverShifted

Description

@OverShifted

Describe the bug
HTMLTable currently appends an extra newline to the formatted code if the last token does not end with \n. This behavior can be problematic when an HTMLLineHighlighter is used as it's @inner. Since HTMLLineHighlighter always appends a newline to the formatted output, which is not reflected in the input tokens. This results in two \ns to be inserted and an extra empty line.

To Reproduce
Steps to reproduce the behavior:
I originally stumbled upon this problem when using Jekyll with something like:

{% highlight plaintext linenos mark_lines="1" %}
foo
bar
{% endhighlight %}

{% highlight plaintext linenos %}
foo
bar
{% endhighlight %}

Which produced (using a custom theme):
A screenshot demonstrating current vs expected behavior

Expected behavior
I expected the first code block to not include the last empty line. like:
A screenshot of the expected behavior

Possible fixes
Either:

  1. Make HTMLLineHighlighter not append an extra \n after the last line.
  2. or make HTMLTable append the \n only when the formatted output of @inner does not end with a new line:
    unless last_val && last_val.end_with?(?\n)
      num_lines += 1
      formatted << ?\n unless formatted.end_with?(?\n)
    end
    I can confirm this solution solved my specific problem and have created a pull request.

System:

  • Ruby version 3.4.7
  • Rouge version: 4.6.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfix-requestA request for a bugfix to be developed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions