makew0rld / amfora

A fancy terminal browser for the Gemini protocol.
GNU General Public License v3.0
1.14k stars 65 forks source link

Add Chroma support for syntax highlighting #263

Closed mntn-xyz closed 2 years ago

mntn-xyz commented 2 years ago

For #252.

mntn-xyz commented 2 years ago

You can test it on my blog, this post has quite a lot of highlighted code blocks: gemini://mntn.xyz/posts/2021-08-22-hello-world/

mntn-xyz commented 2 years ago

On second thought, I've tested Chroma on the command line with the same problem file and it looks like there is no issue there. So now I suspect the ANSI "correction" that takes place after Chroma inserts its own ANSI codes. This could point to a problem in that code, or it could be that with what I'm trying to do I should first just strip out all ANSI if it's a highlighted code block (before Chroma highlights it) and then avoid the post-processing. I wanted to leave it in there because I figured that there might be other sanitization and recoloring that should be kept in place.

This simple code block causes a problem:

```markdown
This is a short [Markdown](https://en.wikipedia.org/wiki/Markdown) line.
```

image

mntn-xyz commented 2 years ago

I fixed the issue with the extra newline, but I think the second issue may actually be an issue with cview.TranslateANSI(). Below is the raw output from the Chroma formatting, if ANSI translation is commented out.

[1m[37mThis is a short [0m[1m[37m[[0m[1m[31mMarkdown[[0m[1m[37m]([0m[1m[33mhttps://en.wikipedia.org/wiki/Markdown[0m[1m[37m)[0m[1m[37m line.

This output appears correct. For some reason the ANSI translation is shifting the color to the right.

mntn-xyz commented 2 years ago

Yep, looks like you already found the source of the color issue: https://code.rocketnine.space/tslocum/cview/issues/48

mntn-xyz commented 2 years ago

I'll mark this as ready for review, the color issue is a bug in cview and probably affects other ANSI stuff in Amfora as is.

mntn-xyz commented 2 years ago

Interesting, so I was looking into the color issue and I'm not sure that it's a cview problem after all. This simple program works fine and displays the colors correctly.

package main

import (
        "code.rocketnine.space/tslocum/cview"
)

func main() {
        app := cview.NewApplication()

        tv := cview.NewTextView()
        tv.SetDynamicColors(true)
        tv.SetText(cview.TranslateANSI("\033[0mThis is a short [\033[1m\033[37m\033[40mMarkdown\033[0m](\033[36m\033[40mhttps://en.wikipedia.org/wiki/Markdown\033[0m) line.\033[91m\033[40m"))
        app.SetRoot(tv, true)
        if err := app.Run(); err != nil {
                panic(err)
        }
}

To be clear, Chroma is outputting the colors correctly, and it looks like cview may be converting them correctly. So I'm not sure what is pushing them over by one character, but it's probably either something in Amfora or something in cview that's coming up based on how Amfora has configured it.

mntn-xyz commented 2 years ago

Pleased to see this pop up in the wild: gemini://gerikson.com/gemlog/misc/Re-Mark-It-Down.gmi

First preformatted block has alt text with the first word being "markdown"; Amfora picks it up and highlights the Markdown portion appropriately.

I finally realized that I should alter the default theme, now it looks better (base text is the same color as preformatted text, instead of bold white). I still need to add theme selection from the config file, detection of terminal colorspace, and an option to turn off syntax highlighting entirely.

mntn-xyz commented 2 years ago

Re: the shifted colors, I'm starting to wonder if it has to do with the close bracket from Markdown links somehow confusing things. It's not a problem with any other highlighted code, and it only happens after that close bracket.

Confirmed, I opened #264 as a separate issue since it happens even with manually added ANSI.

mntn-xyz commented 2 years ago

TODO:

mntn-xyz commented 2 years ago

Updated Wiki documentation

Home.md

Source-Code-Highlighting.md

makew0rld commented 2 years ago

Updated Wiki documentation

Home.md

Source-Code-Highlighting.md

What changes did you make to the homepage? It looks the same to me. Thanks for the highlighting page though, I'll use it for the wiki when this is merged.

mntn-xyz commented 2 years ago

Thanks for reviewing, I'll get the changes made ASAP. Re: the wiki, I meant to include an updated sidebar page, good catch (homepage had no changes):

_Sidebar.md

makew0rld commented 2 years ago

Thanks! About to release v1.9.0, but this will make it into the next minor version.

mntn-xyz commented 2 years ago

Everything should be resolved now.

mntn-xyz commented 2 years ago

Should be ready to go again!

makew0rld commented 2 years ago

Looks good now! Thanks a lot for this work. I've added your pages to wiki as well with some minor edits, take a look and let me know here if you have any suggestions.