3

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>
Jason S
  • 184,598
  • 164
  • 608
  • 970

3 Answers3

4

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.

lorenzog
  • 3,483
  • 4
  • 29
  • 50
2

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.)

juulcat
  • 260
  • 2
  • 7
0

Pygments uses style classes to generate the CSS.

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

trijezdci
  • 5,284
  • 2
  • 19
  • 15