alecthomas / chroma

A general purpose syntax highlighter in pure Go
MIT License
4.34k stars 397 forks source link

WriteCSS not outputting rule for NameBuiltinPseudo #346

Closed acahir closed 4 years ago

acahir commented 4 years ago

Description

When using WriteCSS to autogenerate CSS rules (through Hugo v63.8, but traced that call back to chroma/html.go), the NameBuiltinPseudo CSS (".bp") class is never output.

It's possible this is by design, but I couldn't find a reason why in the code (Not much experience with Go, sorry). Either way, that CSS class is generated during processing for at least Swift, not sure about other languages.

To Reproduce

echo "print(\"Hello World\")" | ./chroma --html --html-only --lexer=swift generates:

<pre class="chroma"><span class="bp">print</span><span class="p">(</span><span class="s">&#34;Hello World&#34;</span><span class="p">)</span>
</pre>

./chroma --html-styles | grep bp gives no output

alecthomas commented 4 years ago

This is somewhat deliberate, though perhaps a bit confusing. Basically if a particular style's token type evaluates to the same as the background, it will not be emitted.

For example, the abap style has a white background and NameBuiltinPseudo is black, so is thus emitted:

$ echo "print(\"Hello World\")" | chroma --style=abap --html --lexer=swift | less | grep bp
/* NameBuiltinPseudo */ .chroma .bp { color: #000000 }
<pre class="chroma"><span class="bp">print</span><span class="p">(</span><span class="s">&#34;Hello World&#34;</span><span class="p">)</span>

Effectively this should cause no issues; are you seeing some problems due to this?

acahir commented 4 years ago

Think I get it, basically leaving out unneeded classes.

Yea, I did notice this because of a problem. I had used the WriteCSS function to generate a list of css as a starting point to customize the styling. I had noticed once before that a class wasn't output, but at the time didn't trace it back to the function call in Chroma (since it was used through hugo).

I noticed it again recently adding a 'dark' theme (with toggle on static site) and noticed some words were black on a now black background. Went looking and found that the .bp class had no definition output, so no where to tell it to change.

Perhaps my understanding of the writeCSS function (called through hugo gen chromastyles --style=monokai > syntax.css) is not the correct way to form a base to customize a style? Given this from the hugo docs, I could see a lot of people going this way once the stylesheet was output.

I'd be happy to submit some changes to the hugo docs on the best way to change anything would be.

Thanks for the response.

acahir commented 4 years ago

I just noticed in your Chroma docs:

If WithClasses() is used, the corresponding CSS can be obtained from the formatter with:

formatter := html.New(html.WithClasses())
err := formatter.WriteCSS(w, style)

So I guess I'd have the same question. If someone wanted to use WithClasses, and generated a stylesheet with WriteCSS, then I think there's the same potential for missing classes causing a problem if customized. Once those styles are outside Chroma, it can no longer intelligently decide which classes are redundant.

The irony is that I probably chose to use WithClasses partly to avoid the redundant inline styling in the HTML, yet the attempt to remove redundant classes is what cause the problem I ran into.

alecthomas commented 4 years ago

That use case makes sense. I've added an option to the HTML formatter to output even the redundant styles. If you'd like to file a bug with Hugo, bep can update gen chromastyles to use this.

acahir commented 4 years ago

bep is requesting if you could tag a release version to pull?

alecthomas commented 4 years ago

Yep, tagged v0.7.2!