octopress / code-highlighter

Octopress's core pygments system for rendering code blocks.
MIT License
10 stars 5 forks source link

Correctly handle multi-line <span>s #8

Open randycoulman opened 8 years ago

randycoulman commented 8 years ago

With some code blocks, the highlighter might produce a <span> element that "spans" multiple lines. When that happens, the tableize_code method will generate HTML that nests <div>s inside the mutli-line <span>.

With the current code, the result of this:

{% codeblock Minimal package.json lang:json %}
{
  "name": "@",
  "main": ""
}
{% endcodeblock %}

is this:

screen shot 2016-10-05 at 6 03 47 am

This PR addresses the issue by detecting these multi-line spans and splitting them, so that each span lives on its own line. That way, tableize_code can't mess up the formatting.

With this fix in place, the above codeblock now looks like this:

screen shot 2016-10-05 at 8 35 07 pm

This may not be the best way to accomplish the goal of having correct formatting, for a few reasons:

Both Rouge and Pygments have an option to generate their own line numbers. It might be better to lean on those features. However, the resulting markup is different for each of them, and it would be harder to accomplish the goal of being able to mark lines in code blocks, which is a lot of what tableize_code is doing.

Note that I pulled the span-splitting code into its own class to make it easier to test. I included a higher-level test of the formatter itself as well. If the extra class isn't desirable, I can merge the implementation back in. But if you like this approach, there's some opportunity to refactor out some of the other work being done in the Renderer.