summaryrefslogtreecommitdiff
path: root/content/resources/rstodt/rst2html-pygments.py
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2014-10-04 16:59:06 +0200
committerSébastien Dailly <sebastien@chimrod.com>2014-10-04 16:59:06 +0200
commit2c3cf5179f16e8303ece57e3dbb191a46f3dcb85 (patch)
tree3d4ea31cd91388149ba43d3c8698c90532a51a59 /content/resources/rstodt/rst2html-pygments.py
parent19e71ce6cd7ca7540e0e004d180998e051c57402 (diff)
Moved resources to the blog site
Diffstat (limited to 'content/resources/rstodt/rst2html-pygments.py')
-rw-r--r--content/resources/rstodt/rst2html-pygments.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/content/resources/rstodt/rst2html-pygments.py b/content/resources/rstodt/rst2html-pygments.py
new file mode 100644
index 0000000..7e2a826
--- /dev/null
+++ b/content/resources/rstodt/rst2html-pygments.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+from docutils.core import publish_cmdline
+
+# Define a new directive `code-block` that uses the `pygments` source
+# highlighter to render code in color.
+#
+
+from docutils import nodes
+from docutils.parsers.rst import directives, Directive
+from pygments import highlight
+
+from pygments.lexers import get_lexer_by_name, TextLexer
+from pygments.formatters import HtmlFormatter
+
+INLINESTYLES = False
+pygments_formatter = HtmlFormatter()
+DEFAULT = HtmlFormatter(noclasses=INLINESTYLES,linenos="table")
+VARIANTS = {}
+
+class Pygments(Directive):
+ """ Source code syntax hightlighting.
+ """
+ required_arguments = 1
+ optional_arguments = 1
+ final_argument_whitespace = True
+
+ has_content = True
+ linenos=1
+
+ def run(self):
+ dic = {}
+
+ # On crée un dictionnaire à partir des options
+ for options in self.arguments[1:]:
+ clef, arg = options.split("=")
+ dic[clef] = arg
+
+ self.assert_has_content()
+ try:
+ lexer = get_lexer_by_name(self.arguments[0], **dic)
+ except ValueError:
+ # no lexer found - use the text one instead of an exception
+ lexer = TextLexer()
+ # take an arbitrary option if more than one is given
+ formatter = self.options and VARIANTS[self.options.keys()[0]] or DEFAULT
+ parsed = highlight(u'\n'.join(self.content), lexer, formatter)
+ return [nodes.raw('', parsed, format='html')]
+
+
+directives.register_directive('code-block', Pygments)
+
+publish_cmdline(writer_name='html')
+