Skip to content
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

Pass code block's language to the specified Pygments formatter in CodeHilite #1258

Merged
merged 7 commits into from May 18, 2022
Merged

Pass code block's language to the specified Pygments formatter in CodeHilite #1258

merged 7 commits into from May 18, 2022

Conversation

ccwang002
Copy link
Contributor

@ccwang002 ccwang002 commented May 18, 2022

As per the discussions in PR #1255, I add a new option lang_str that passes the language of a code block to the specified Pygments formatter. lang_str has the value of {lang_prefix}{lang}, so it respects the existing language prefix option.

While lang_str has no effect the builtin Pygments formatters, users can leverage this information in their custom formatter to annotate the generated output.

An example I added to the documentation annotates the language as a class of the <code> tag. I copied it below:

from pygments.formatters import HtmlFormatter


class CustomHtmlFormatter(HtmlFormatter):
    def __init__(self, lang_str='', **options):
        super().__init__(**options)
        # lang_str has the value {lang_prefix}{lang} 
        # specified by the CodeHilite's options
        self.lang_str = lang_str

    def _wrap_code(self, source):
        yield 0, f'<code class="{self.lang_str}">'
        yield from source
        yield 0, '</code>'


some_text = '''\
    :::python
    print('hellow world')
'''

markdown.markdown(
    some_text,
    extensions=['markdown.extensions.codehilite'],
    extension_configs={'markdown.extensions.codehilite': {
        'pygments_formatter': CustomHtmlFormatter
    }}
)

The formatter above will output the following HTML structure for the code block:

<div class="codehilite">
    <pre>
        <code class="language-python">
        ...
        </code>
    </pre>
</div>

ccwang002 and others added 7 commits May 18, 2022 15:44
Add an extra option `lang_str` to pass the language of the code block
to the specified Pygments formatter.
Include an example of custom Pygments formatter that keep the
language of the code in the output using the new option lang_str
@waylan
Copy link
Member

waylan commented May 18, 2022

I made a few tweaks to adjust some awkward wording. Also, I altered the example to use the extension class directly rather than using the string name. While both ways are supported, we recommend users use the Python objects. Therefore, our documentation should reflect that.

Otherwise, this looks good to me.

@waylan waylan added the approved The pull request is ready to be merged. label May 18, 2022
@waylan waylan merged commit 12c3378 into Python-Markdown:master May 18, 2022
@ccwang002 ccwang002 deleted the pass-pygments-fmt-lang branch May 19, 2022 00:35
@ccwang002
Copy link
Contributor Author

Nice. Thanks for the feedback and review 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved The pull request is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants