How do I get the css for a pygments formatter?

2.5k views Asked by At

I'm using pygments in the basic manner from http://pygments.org/docs/quickstart/

My formatter is created like this:

                formatter = HtmlFormatter(cssclass="codehilite", linenos='table',
                    linenostart = lineno - len(excerpt) + 1,
                    hl_lines = important_lines,
                    style='colorful')           

It works fine but I don't get any CSS from the output, just the classes. Where do I get the CSS? I want to put it in the <head> of my HTML file so I don't need a separate .css file. All I get from pygments.highlight() is this:

<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>44
45
46
47
48
49
50
51
52
53
54
55
56
57</pre></div></td><td class="code"><div class="codehilite"><pre><span class="cm">/* *********** Interrupt Service Routines *********************************** */</span>

<span class="cm">/**</span>
<span class="cm"> * UART1_RX interrupt service routine.</span>
<span class="cm"> * Clears the UARTRX interrupt flag and disables the interrupt.</span>
<span class="cm"> */</span>
<span class="n">UART1_RXISR_FUNCTION_HEADER</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">UART1_DISABLE_IRQ_RX</span><span class="p">;</span>
    <span class="n">UART1_CLEAR_IRQ_RX_FLAG</span><span class="p">;</span>
<span class="p">}</span>

<span class="o">/**</span> 
 <span class="o">*</span> <span class="n">UART1_TX</span> <span class="n">interrupt</span> <span class="n">service</span> <span class="n">routine</span><span class="p">.</span>
</pre></div>
</td></tr></table>
3

There are 3 answers

0
juulcat On

You can get pre-generated CSS from the pygments-css repository on GitHub. (Tip suggested by https://github.com/trentm/python-markdown2/wiki/fenced-code-blocks.)

0
trijezdci On

Pygments uses style classes to generate the CSS.

http://pygments.org/docs/styles/

0
lorenzog On

Unless you use the full=True option in the formatter, you can get the CSS like this:

the_css = formatter.get_style_defs()

Then you can insert it in your html header.