This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable syntax highlighting in rst, and tweak the syntax highlighting …
…in markdown. Also update the sample site to demonstrate code highlighting in both mark up languages.
- Loading branch information
Showing
6 changed files
with
179 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
.codehilite .hll { background-color: #ffffcc } | ||
.codehilite .c { color: #60a0b0; font-style: italic } /* Comment */ | ||
.codehilite .err { border: 1px solid #FF0000 } /* Error */ | ||
.codehilite .k { color: #007020; font-weight: bold } /* Keyword */ | ||
.codehilite .o { color: #666666 } /* Operator */ | ||
.codehilite .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */ | ||
.codehilite .cp { color: #007020 } /* Comment.Preproc */ | ||
.codehilite .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */ | ||
.codehilite .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */ | ||
.codehilite .gd { color: #A00000 } /* Generic.Deleted */ | ||
.codehilite .ge { font-style: italic } /* Generic.Emph */ | ||
.codehilite .gr { color: #FF0000 } /* Generic.Error */ | ||
.codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */ | ||
.codehilite .gi { color: #00A000 } /* Generic.Inserted */ | ||
.codehilite .go { color: #808080 } /* Generic.Output */ | ||
.codehilite .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */ | ||
.codehilite .gs { font-weight: bold } /* Generic.Strong */ | ||
.codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ | ||
.codehilite .gt { color: #0040D0 } /* Generic.Traceback */ | ||
.codehilite .kc { color: #007020; font-weight: bold } /* Keyword.Constant */ | ||
.codehilite .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */ | ||
.codehilite .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */ | ||
.codehilite .kp { color: #007020 } /* Keyword.Pseudo */ | ||
.codehilite .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */ | ||
.codehilite .kt { color: #902000 } /* Keyword.Type */ | ||
.codehilite .m { color: #40a070 } /* Literal.Number */ | ||
.codehilite .s { color: #4070a0 } /* Literal.String */ | ||
.codehilite .na { color: #4070a0 } /* Name.Attribute */ | ||
.codehilite .nb { color: #007020 } /* Name.Builtin */ | ||
.codehilite .nc { color: #0e84b5; font-weight: bold } /* Name.Class */ | ||
.codehilite .no { color: #60add5 } /* Name.Constant */ | ||
.codehilite .nd { color: #555555; font-weight: bold } /* Name.Decorator */ | ||
.codehilite .ni { color: #d55537; font-weight: bold } /* Name.Entity */ | ||
.codehilite .ne { color: #007020 } /* Name.Exception */ | ||
.codehilite .nf { color: #06287e } /* Name.Function */ | ||
.codehilite .nl { color: #002070; font-weight: bold } /* Name.Label */ | ||
.codehilite .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */ | ||
.codehilite .nt { color: #062873; font-weight: bold } /* Name.Tag */ | ||
.codehilite .nv { color: #bb60d5 } /* Name.Variable */ | ||
.codehilite .ow { color: #007020; font-weight: bold } /* Operator.Word */ | ||
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */ | ||
.codehilite .mf { color: #40a070 } /* Literal.Number.Float */ | ||
.codehilite .mh { color: #40a070 } /* Literal.Number.Hex */ | ||
.codehilite .mi { color: #40a070 } /* Literal.Number.Integer */ | ||
.codehilite .mo { color: #40a070 } /* Literal.Number.Oct */ | ||
.codehilite .sb { color: #4070a0 } /* Literal.String.Backtick */ | ||
.codehilite .sc { color: #4070a0 } /* Literal.String.Char */ | ||
.codehilite .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */ | ||
.codehilite .s2 { color: #4070a0 } /* Literal.String.Double */ | ||
.codehilite .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */ | ||
.codehilite .sh { color: #4070a0 } /* Literal.String.Heredoc */ | ||
.codehilite .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */ | ||
.codehilite .sx { color: #c65d09 } /* Literal.String.Other */ | ||
.codehilite .sr { color: #235388 } /* Literal.String.Regex */ | ||
.codehilite .s1 { color: #4070a0 } /* Literal.String.Single */ | ||
.codehilite .ss { color: #517918 } /* Literal.String.Symbol */ | ||
.codehilite .bp { color: #007020 } /* Name.Builtin.Pseudo */ | ||
.codehilite .vc { color: #bb60d5 } /* Name.Variable.Class */ | ||
.codehilite .vg { color: #bb60d5 } /* Name.Variable.Global */ | ||
.codehilite .vi { color: #bb60d5 } /* Name.Variable.Instance */ | ||
.codehilite .il { color: #40a070 } /* Literal.Number.Integer.Long */ | ||
.highlight .hll { background-color: #ffffcc } | ||
.highlight .c { color: #60a0b0; font-style: italic } /* Comment */ | ||
.highlight .err { border: 1px solid #FF0000 } /* Error */ | ||
.highlight .k { color: #007020; font-weight: bold } /* Keyword */ | ||
.highlight .o { color: #666666 } /* Operator */ | ||
.highlight .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */ | ||
.highlight .cp { color: #007020 } /* Comment.Preproc */ | ||
.highlight .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */ | ||
.highlight .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */ | ||
.highlight .gd { color: #A00000 } /* Generic.Deleted */ | ||
.highlight .ge { font-style: italic } /* Generic.Emph */ | ||
.highlight .gr { color: #FF0000 } /* Generic.Error */ | ||
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */ | ||
.highlight .gi { color: #00A000 } /* Generic.Inserted */ | ||
.highlight .go { color: #808080 } /* Generic.Output */ | ||
.highlight .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */ | ||
.highlight .gs { font-weight: bold } /* Generic.Strong */ | ||
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ | ||
.highlight .gt { color: #0040D0 } /* Generic.Traceback */ | ||
.highlight .kc { color: #007020; font-weight: bold } /* Keyword.Constant */ | ||
.highlight .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */ | ||
.highlight .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */ | ||
.highlight .kp { color: #007020 } /* Keyword.Pseudo */ | ||
.highlight .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */ | ||
.highlight .kt { color: #902000 } /* Keyword.Type */ | ||
.highlight .m { color: #40a070 } /* Literal.Number */ | ||
.highlight .s { color: #4070a0 } /* Literal.String */ | ||
.highlight .na { color: #4070a0 } /* Name.Attribute */ | ||
.highlight .nb { color: #007020 } /* Name.Builtin */ | ||
.highlight .nc { color: #0e84b5; font-weight: bold } /* Name.Class */ | ||
.highlight .no { color: #60add5 } /* Name.Constant */ | ||
.highlight .nd { color: #555555; font-weight: bold } /* Name.Decorator */ | ||
.highlight .ni { color: #d55537; font-weight: bold } /* Name.Entity */ | ||
.highlight .ne { color: #007020 } /* Name.Exception */ | ||
.highlight .nf { color: #06287e } /* Name.Function */ | ||
.highlight .nl { color: #002070; font-weight: bold } /* Name.Label */ | ||
.highlight .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */ | ||
.highlight .nt { color: #062873; font-weight: bold } /* Name.Tag */ | ||
.highlight .nv { color: #bb60d5 } /* Name.Variable */ | ||
.highlight .ow { color: #007020; font-weight: bold } /* Operator.Word */ | ||
.highlight .w { color: #bbbbbb } /* Text.Whitespace */ | ||
.highlight .mf { color: #40a070 } /* Literal.Number.Float */ | ||
.highlight .mh { color: #40a070 } /* Literal.Number.Hex */ | ||
.highlight .mi { color: #40a070 } /* Literal.Number.Integer */ | ||
.highlight .mo { color: #40a070 } /* Literal.Number.Oct */ | ||
.highlight .sb { color: #4070a0 } /* Literal.String.Backtick */ | ||
.highlight .sc { color: #4070a0 } /* Literal.String.Char */ | ||
.highlight .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */ | ||
.highlight .s2 { color: #4070a0 } /* Literal.String.Double */ | ||
.highlight .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */ | ||
.highlight .sh { color: #4070a0 } /* Literal.String.Heredoc */ | ||
.highlight .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */ | ||
.highlight .sx { color: #c65d09 } /* Literal.String.Other */ | ||
.highlight .sr { color: #235388 } /* Literal.String.Regex */ | ||
.highlight .s1 { color: #4070a0 } /* Literal.String.Single */ | ||
.highlight .ss { color: #517918 } /* Literal.String.Symbol */ | ||
.highlight .bp { color: #007020 } /* Name.Builtin.Pseudo */ | ||
.highlight .vc { color: #bb60d5 } /* Name.Variable.Class */ | ||
.highlight .vg { color: #bb60d5 } /* Name.Variable.Global */ | ||
.highlight .vi { color: #bb60d5 } /* Name.Variable.Instance */ | ||
.highlight .il { color: #40a070 } /* Literal.Number.Integer.Long */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
The Pygments reStructuredText directive | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
This fragment is a Docutils_ 0.5 directive that renders source code | ||
(to HTML only, currently) via Pygments. | ||
To use it, adjust the options below and copy the code into a module | ||
that you import on initialization. The code then automatically | ||
registers a ``sourcecode`` directive that you can use instead of | ||
normal code blocks like this:: | ||
.. sourcecode:: python | ||
My code goes here. | ||
If you want to have different code styles, e.g. one with line numbers | ||
and one without, add formatters with their names in the VARIANTS dict | ||
below. You can invoke them instead of the DEFAULT one by using a | ||
directive option:: | ||
.. sourcecode:: python | ||
:linenos: | ||
My code goes here. | ||
Look at the `directive documentation`_ to get all the gory details. | ||
.. _Docutils: http://docutils.sf.net/ | ||
.. _directive documentation: | ||
http://docutils.sourceforge.net/docs/howto/rst-directives.html | ||
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS. | ||
:license: BSD, see LICENSE for details. | ||
""" | ||
|
||
# Options | ||
# ~~~~~~~ | ||
|
||
# Set to True if you want inline CSS styles instead of classes | ||
INLINESTYLES = False | ||
|
||
from pygments.formatters import HtmlFormatter | ||
|
||
# The default formatter | ||
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES) | ||
|
||
# Add name -> formatter pairs for every variant you want to use | ||
VARIANTS = { | ||
'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True), | ||
} | ||
|
||
|
||
from docutils import nodes | ||
from docutils.parsers.rst import directives, Directive | ||
|
||
from pygments import highlight | ||
from pygments.lexers import get_lexer_by_name, TextLexer | ||
|
||
class Pygments(Directive): | ||
""" Source code syntax hightlighting. | ||
""" | ||
required_arguments = 1 | ||
optional_arguments = 0 | ||
final_argument_whitespace = True | ||
option_spec = dict([(key, directives.flag) for key in VARIANTS]) | ||
has_content = True | ||
|
||
def run(self): | ||
self.assert_has_content() | ||
try: | ||
lexer = get_lexer_by_name(self.arguments[0]) | ||
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('sourcecode', Pygments) | ||
|