jgm / skylighting

A Haskell syntax highlighting library with tokenizers derived from KDE syntax highlighting descriptions
189 stars 61 forks source link

Support for custom attributes #123

Closed christophgockel closed 2 years ago

christophgockel commented 3 years ago

Hello,

I searched the history and closed issues but couldn't find anything, so I thought I'd ask. Is it possible to add behaviour to pass on custom attributes of a code block to the target language?

I'm not sure myself yet how it would look like for all supported formats, but I'm currently thinking specifically from Markdown to LaTeX, I'd like to be able to define a code block like this

```{.diff .numberLines startFrom="2" newattribute="test"}

that should result in this LaTeX code:

\begin{Highlighting}[numbers=left,,firstnumber=2,newattribute={test},]

But right now, it seems the only attributes that are recognised/allowed are the language classes, .numberLines and startFrom as a keyword argument. No other keyword arguments are allowed - or carried over.

Being able to define additional attributes in Markdown would enable me to add custom behaviour in an additional .tex file to make use of these attributes. So I wanted to ask if there's a possibility to add this behaviour? I'm not really familiar with Haskel, but happy to give it a try in a PR. But I wanted to check in first.

Thank you!

Update 20th April:

I guess an alternative question could also be, is there a way to achieve the output of additional attributes today already?

Or could this be implemented by wrapping the existing LaTeX writer with a custom filter (somehow), so that it could treat CodeBlock elements with the custom code, but leave everything else as it is? I guess the line(s) I'm looking at are specifically in file https://github.com/jgm/skylighting/blob/master/skylighting-core/src/Skylighting/Format/LaTeX.hs#L75. Where the formatLaTeXBlock function looks into the FormatOptions it's been given. The struggle I have with thinking of a custom filter for it, that no matter what the filter does to the AST it still depends on the LaTeX writer at the end of the day which doesn't recognise attributes it doesn't know about.

jgm commented 3 years ago

You could use a filter, perhaps. When you match a code block with your special attribute, your filter can add raw LaTeX blocks before and after, which redefine things or whatever...

christophgockel commented 2 years ago

I must admit I completely forgot to get back to this. My bad!

Thanks for the response! Using filters to essentially wrap the existing content with the additional data I need is an approach I haven't considered yet! I will give that a go, thank you!

christophgockel commented 2 years ago

I must admit I completely forgot to get back to this. My bad!

Thanks for the response! Using filters to essentially wrap the existing content with the additional data I need is an approach I haven't considered yet! I will give that a go, thank you!