-
Notifications
You must be signed in to change notification settings - Fork 876
Add a new option of CodeHilite to assign lang class when Pygments is in use #1255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new option of CodeHilite to assign lang class when Pygments is in use #1255
Conversation
The new option assigns the language of the code block as a class to the generated <code> tag even when pygments is in use. It respects the options of lang and lang_prefix like the existing behavior. So the generated output of CodeHilite with or without Pygments is always HTML5 compliant.
Obviously this still needs some work to get all test passing. However, I thought I would comment on the general idea. Personally, I find this intriguing. I have never spent much time exploring what can be done with custom formatters. That said, I have a few concerns. I'm not crazy about the name Which brings me to my final concern. I wonder if this would be better as a third party addon. All that is really needed is for the user to have the definition of the custom formatter. Then they can pass than in to
The above comment concerns me a little. Let's consider the relevant section of the spec:
First of all, it is clear that there is "no formal" specification for how to define a language. Second, it is suggested that one "can use the class attribute." However, that is a suggestion, not a requirement. And then the example of using the prefixed class is an example of one possible way that could be done. I agree with the spec writers and think that the suggestions are good ones. However, this is not required by the spec and we don't want to suggest that it is. Therefore, our output is compliant with or without this change. To suggest otherwise is misleading. Finally, the suggested reason for identifying the language is so that syntax highlighters can properly work on the code. However, in our case, the syntax highlighter has already done its work. Therefore, there is no functional need for the language to be identified. The language only serves as meta-data at this point. I'm not saying that it serves no value, but it is certainly less important to include. Given all of the above, I'm leaning more and more to my documentation idea. Thoughts anyone? |
Personally, I don't know why the class needs to be attached to the I maintain an alternative to Using an HTML formatted is certainly an option, and if we find this approach attractive to implement this feature, especially if we deem that such a class must be attached to |
I will say that I'm +1 on adding the language class as I've come to learn people like to use it for different reasons. I'm +0 on using a custom HTML format class 🤷🏻. |
Thanks for your suggestions and feedbacks! I agree there might be better option names. Happy to change it. I also agree that the custom formatter can comes as a code recipe on the documentation. That said, currently in the CodeHilite extenstion, the formatter doesn't have access to the language information. If we decide not to include a custom formatter and get rid of the option, we should at least consider passing markdown/markdown/extensions/codehilite.py Lines 188 to 191 in 7d4ecad
Regarding the HTML5 compliance, I saw this wording on the documentation for the non-pygments generation, so I decided to copy it. I will tone it down later. The language class doesn't need to be at the Indeed, this option is about the language identification. My main problem is that there is no way I am aware of that indicates the original language in the generated code blocks, using the official extensions. Personally I'd love to just stick to the official ones because I won't be able to maintain my own in the long run. Besides syntax highlighting, having the language identification in the generated code blocks opens up some interactive functionalities. One example I can think of right away is have the console outputs toggleable on the official Python documentation:
To sum up, we have a few approaches if we want to provide customization:
|
Yes, I do agree there is utility in using a |
Right. That would be reasonable. I will need to think on what I consider the best way forward here. |
Pygments will accept any keyword arguments passed to it and will simply ignore any it does not recognize without error. Therefore, we can easily pass the language (and prefix) to all formatters, even the Pygments built-in ones. Therefore, I think the correct way forward would be to:
And that's it. We could insert the language on the div, but I don't see a need for this. Those users who really want that can use a custom formatter and then they can set the language wherever they want it. I would close this PR, but I'll leave it open as the "issue" to be closed when a new PR is merged that implements the above. |
👍 sounds good to me! I can make another PR with the suggested implementation. |
This change adds a new option
pygments_add_lang_class
to the CodeHilite extension. When Pygments and this option are enabled, the language of the code block is assigned to the generated<code>
tag. The option respects optionslang
andlang_prefix
like the existing behavior. If the language of the code block is guessed by Pygments, the name of the guessed lexer is used as the language name (technically the first alias of the lexer).In short, with this option enabled, CodeHilite's generated output will always be HTML5 compliant with or without Pygments.
The generated HTML output of the following code block:
will become:
Regarding the implementation, this option utilizes the new behavior of
pygments_formatter
that can take in a custom Pygments formatter introduced in PR #1187. We create a custom HTML formatter class that takes in the language and the prefix. User can still override to use their custom formatter when they specifypygments_formatter
.I am open to any suggestions and feedbacks. Thank you for making this package!