If I run a python source file through pygments, it outputs html code whose elements class belong to some CSS file pygments is using. Could the style attributes be included in the outputted html so that I don't have to provide a CSS file?
4 Answers
By setting the noclasses attribute to True, only inline styles will be generated. Here's a snippet that does the job just fine:
formatter = HtmlFormatter(style=MyStyle)
formatter.noclasses = True
print highlight(content,PythonLexer(),formatter)

- 93,257
- 117
- 344
- 520
@Ignacio: quite the opposite:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
code = 'print "Hello World"'
print highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))
[ ref.: http://pygments.org/docs/formatters/, see HtmlFormatter ]
( Basically it is the same as Tempus's answer, I just thought that a full code snippet may save a few seconds ) )
PS. The ones who think that the original question is ill-posed, may imagine e.g. the task of pasting highlighted code into a blog entry hosted by a third-party service.

- 1,476
- 1
- 22
- 29
Usually, your code is only one of many things on a web page. You often want code to look different from the other content. You, generally, want to control the style of the code as part of the overall page style. CSS is your first, best choice for this.
However, you can embed the styles in the HTML if that seems better. Here's an example that shows the <style>
tag in the <head>
tag.

- 384,516
- 81
- 508
- 779
-
Scott, I want to do this programatically. – Geo Mar 08 '09 at 23:20
-
What does "programmatically" mean in this context? You get a document that references styles that you can provide. What else do you want? – S.Lott Mar 08 '09 at 23:35
-
2@S.Lott pygments gives you different ways to format text. The default way is to wrap the code around spans which signify different tokens in the programming language, to which you wish to add syntax highlighting. The style is then applied via CSS. However pygments can allow you to output the style as a separate CSS file or embedded. Your answer is extremely general and shows you have little understanding about the question or pygments. – Tom Nov 30 '11 at 16:44