0

I'm using the Pygments package in my Django project. When I try to render the code snippet in my template, it renders the whole data as follows:

Template:
...
{% pygmentify %}
<pre class="{{snippet.lang}}">{{snippet.body}}</pre>
{% endpygmentify %}
...

Final rendered HTML:

<pre lang="python">
...
<span class="kn">import</span> <span class="nn">hello</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'hey'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'in the function'</span><span class="p">)</span>
...
</pre>

It actually works with no pain. The entire code block is being highlighted properly. The thing is that I want to show the line number as well. Should I style them or there is only a simple Pygments configuration needed?

Thanks.

Sadra
  • 167
  • 1
  • 9

1 Answers1

1

If you are using django-pygmentify, you can pass keyword arguments as indicated in their docs

{% pygmentify linenos='inline' %}
<pre class="{{snippet.lang}}">{{snippet.body}}</pre>
{% endpygmentify %}
shriakhilc
  • 2,922
  • 2
  • 12
  • 17
  • It kinda worked but the whole style of the code block is completely chalked! Is it ok to style the line number sidebar? It's like this keyword converts the [pre-code] style to a table!! I don't get it. – Sadra Dec 29 '21 at 22:41
  • I have no idea what you mean by chalked, but you can pass other keywords for a specific style or css file, as shown in the docs link. There are a number of options you can tweak. – shriakhilc Dec 29 '21 at 22:44
  • 1
    @Alireza Try the new code. `'inline'` will integrate it to the
     tag, while all other `linenos` values make a table.
    – shriakhilc Dec 29 '21 at 22:46
  • That's it. Thanks. Any way to set a style for these
    s? (I know I can access them by using inner CSS selectors btw)
    – Sadra Dec 29 '21 at 22:56
  • Just explore the link in my answer and the link it has to HTMLFormatter. I think the `style` keyword argument might be relevant, but I haven't personally used it enough to give you a direct answer – shriakhilc Dec 29 '21 at 22:58
  • Enough assists I think. Much obliged. – Sadra Dec 29 '21 at 22:59