Beispiel #1
0
def highlight(src, syntax="python"):
    # type: (t.Text, t.Text) -> t.Text
    """Apply syntax highlighting. Only available if pygments is installed."""
    if syntax == "xml":
        src = " " * 80 + src
    src = textwrap.dedent(src).strip()
    if not config.color:
        return src
    try:
        from pygments import highlight as pyg_highlight
        from pygments.lexers import PythonLexer, XmlLexer, RstLexer
        from pygments.formatters.terminal import TerminalFormatter
    except ImportError:
        return src
    else:
        if syntax == "python":
            lexer = PythonLexer()
        elif syntax == "xml":
            lexer = XmlLexer()
        elif syntax == "rst":
            lexer = RstLexer()
        else:
            raise ValueError("Unknown syntax {!r}".format(syntax))
        return pyg_highlight(src, lexer,
                             TerminalFormatter()).strip()  # type: ignore
Beispiel #2
0
 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 = RstLexer()
     # take an arbitrary option if more than one is given
     formatter = self.options and VARIANTS[self.options.keys()
                                           [0]] or DEFAULT
     parsed = highlight('\n'.join(self.content), lexer, formatter)
     return [nodes.raw('', parsed, format='latex')]
Beispiel #3
0
    def render(self, context):
        try:
            language = template.Variable(self.lang).resolve(context)
        except:
            language = self.lang

        code = self.nodelist.render(context)
        try:
            lexer = get_lexer_by_name(language)
        except:
            lexer = RstLexer()

        # just before return, add chosen lexer as a template variable:
        return highlight(
            code,
            lexer,
            HtmlFormatter(
                linenos="pre",
                cssclass="syntax",
                anchorlines=True,
                #linenospecial='2',
                #hl_lines=[1,5,6,2,4,12],
            ))
Beispiel #4
0
from sphinx.util.texescape import tex_hl_escape_map_new

if False:
    # For type annotation
    from typing import Any, Dict  # NOQA
    from pygments.formatter import Formatter  # NOQA

logger = logging.getLogger(__name__)

lexers = dict(
    none=TextLexer(stripnl=False),
    python=PythonLexer(stripnl=False),
    python3=Python3Lexer(stripnl=False),
    pycon=PythonConsoleLexer(stripnl=False),
    pycon3=PythonConsoleLexer(python3=True, stripnl=False),
    rest=RstLexer(stripnl=False),
    c=CLexer(stripnl=False),
)  # type: Dict[unicode, Lexer]
for _lexer in lexers.values():
    _lexer.add_filter('raiseonerror')

escape_hl_chars = {
    ord(u'\\'): u'\\PYGZbs{}',
    ord(u'{'): u'\\PYGZob{}',
    ord(u'}'): u'\\PYGZcb{}'
}

# used if Pygments is available
# use textcomp quote to get a true single quote
_LATEX_ADD_STYLES = r'''
\renewcommand\PYGZsq{\textquotesingle}
Beispiel #5
0
                type
                text = "*%s*" % text
                pass
            elif ttype is Token.Generic.Strong:
                text = text.upper()
                pass
            pass

            outfile.write(text)
            pass
        return

    pass


rst_lex = RstLexer()
rst_filt = RstFilter()
rst_lex.add_filter(rst_filt)
color_tf = RSTTerminalFormatter(colorscheme=color_scheme)
mono_tf = MonoRSTTerminalFormatter()


def rst_text(text, mono, width=80):
    if mono:
        tf = mono_tf
    else:
        tf = color_tf
        pass
    tf.reset(width)
    return highlight(text, rst_lex, tf)
Beispiel #6
0
if False:
    # For type annotation
    from typing import Any, Dict  # NOQA
    from pygments.formatter import Formatter  # NOQA
    from sphinx.util.typing import unicode  # NOQA


logger = logging.getLogger(__name__)

lexers = {
    'none': TextLexer(stripnl=False),
    'python': PythonLexer(stripnl=False),
    'python3': Python3Lexer(stripnl=False),
    'pycon': PythonConsoleLexer(stripnl=False),
    'pycon3': PythonConsoleLexer(python3=True, stripnl=False),
    'rest': RstLexer(stripnl=False),
    'c': CLexer(stripnl=False),
}  # type: Dict[unicode, Lexer]
for _lexer in lexers.values():
    _lexer.add_filter('raiseonerror')


escape_hl_chars = {ord(u'\\'): u'\\PYGZbs{}',
                   ord(u'{'): u'\\PYGZob{}',
                   ord(u'}'): u'\\PYGZcb{}'}

# used if Pygments is available
# use textcomp quote to get a true single quote
_LATEX_ADD_STYLES = r'''
\renewcommand\PYGZsq{\textquotesingle}
'''
Beispiel #7
0
    def test_mono(self):

        # Could be in setup()
        rst_lex = RstLexer()
        rst_filt = Mformat.RstFilter()
        rst_lex.add_filter(rst_filt)
        rst_tf = Mformat.MonoRSTTerminalFormatter()
        text = '`A` very *emphasis* **strong** `code`'
        if sys.version_info[0:2] <= (3, 2):
            return
        got = Mformat.highlight(text, rst_lex, rst_tf)
        self.assertEqual('"A" very *emphasis* STRONG "code" ', got)

        self.maxDiff = 3000
        quit_text = """**quit** - gently terminate the debugged program.

The program being debugged is aborted via a *DebuggerQuit*
exception.

When the debugger from the outside (e.g. via a `trepan` command), the
debugged program is contained inside a try block which handles the
*DebuggerQuit* exception.  However if you called the debugger was
started in the middle of a program, there might not be such an
exception handler; the debugged program still terminates but generally
with a traceback showing that exception.

If the debugged program is threaded or worse threaded and deadlocked,
raising an exception in one thread isn't going to quit the
program. For this see `exit` or `kill` for more forceful termination
commands.

Also, see `run` and `restart` for other ways to restart the debugged
program.
"""
        rst_tf.reset(80)
        got = Mformat.highlight(quit_text, rst_lex, rst_tf)
        # FIXME: reinstate
        if False:
            self.assertEqual(
                """QUIT - gently terminate the debugged program.

The program being debugged is aborted via a *DebuggerQuit* exception.

When the debugger from the outside (e.g. via a "trepan" command), the debugged
program is contained inside a try block which handles the *DebuggerQuit*
exception. However if you called the debugger was started in the middle of a
program, there might not be such an exception handler; the debugged program
still terminates but generally with a traceback showing that exception.

If the debugged program is threaded or worse threaded and deadlocked, raising
an exception in one thread isn't going to quit the program. For this see "exit"
or "kill" for more forceful termination commands.

Also, see "run" and "restart" for other ways to restart the debugged program. """,
                got)

        text = '''
This is an example to show off *reformatting.*
We have several lines
here which should be reflowed.

But paragraphs should be respected.

    And verbatim
    text should not be
    touched

End of test.
'''
        rst_tf.reset(30)
        got = Mformat.highlight(text, rst_lex, rst_tf)
        # FIXME: reinstate
        if False:
            self.assertEqual(
                """This is an example to show
off *reformatting.* We have
several lines here which
should be reflowed.

But paragraphs should be
respected.

    And verbatim
    text should not be
    touched


End of test. """, got)

        return
Beispiel #8
0
        background_color = '#eeffcc'
        default_style = ''

        styles = FriendlyStyle.styles
        styles.update({
            Generic.Output: 'italic #333',
            Comment: 'italic #408090',
        })

    lexers = defaultdict(
        TextLexer,
        none=TextLexer(),
        python=PythonLexer(),
        pycon=PythonConsoleLexer(),
        rest=RstLexer(),
        c=CLexer(),
    )
    for _lexer in lexers.values():
        _lexer.add_filter('raiseonerror')

    fmter = HtmlFormatter(style=PythonDocStyle)


def highlight_block(source, lang):
    if not pygments:
        return '<pre>' + cgi.escape(source) + '</pre>\n'
    if lang == 'python':
        if source.startswith('>>>'):
            lexer = lexers['pycon']
        else:
Beispiel #9
0
                pass
            elif ttype is Token.Generic.Emph:
                type
                text = "*%s*" % text
                pass            
            elif ttype is Token.Generic.Strong:
                text = text.upper()
                pass
            pass
            
            outfile.write(text)
            pass
        return
    pass

rst_lex = RstLexer()
rst_filt = RstFilter()
rst_lex.add_filter(rst_filt)
color_tf = RSTTerminalFormatter(colorscheme=color_scheme)
mono_tf  = MonoRSTTerminalFormatter()

def rst_text(text, mono, width=80):
    if mono:
        tf = mono_tf
    else:
        tf = color_tf
        pass
    tf.reset(width)
    return highlight(text, rst_lex, tf)

if __name__ == '__main__':
Beispiel #10
0
from pygments import highlight
from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \
    TextLexer, RstLexer
from pygments.lexers import get_lexer_by_name, guess_lexer
from pygments.formatters import HtmlFormatter, LatexFormatter
from pygments.filters import ErrorToken
from pygments.styles import get_style_by_name
from pygments.util import ClassNotFound
from sphinx.pygments_styles import SphinxStyle, NoneStyle

lexers = dict(
    none=TextLexer(stripnl=False),
    python=PythonLexer(stripnl=False),
    pycon=PythonConsoleLexer(stripnl=False),
    pycon3=PythonConsoleLexer(python3=True, stripnl=False),
    rest=RstLexer(stripnl=False),
    c=CLexer(stripnl=False),
)
for _lexer in lexers.values():
    _lexer.add_filter('raiseonerror')
lexers.update(
    dict(
        force_python=PythonLexer(stripnl=False),
        force_pycon=PythonConsoleLexer(stripnl=False),
        force_pycon3=PythonConsoleLexer(python3=True, stripnl=False),
        force_rest=RstLexer(stripnl=False),
        force_c=CLexer(stripnl=False),
    ))

escape_hl_chars = {
    ord(u'\\'): u'\\PYGZbs{}',
    def test_mono(self):

        # Could be in setup()
        rst_lex  = RstLexer()
        rst_filt = Mformat.RstFilter()
        rst_lex.add_filter(rst_filt)
        rst_tf = Mformat.MonoRSTTerminalFormatter()
        text = '`A` very *emphasis* **strong** `code`'
        got = Mformat.highlight(text, rst_lex, rst_tf)
        self.assertEqual('"A" very *emphasis* STRONG "code" ', got)

        quit_text = """**quit** - gently terminate the debugged program.

The program being debugged is aborted via a *DebuggerQuit*
exception.

When the debugger from the outside (e.g. via a `trepan` command), the
debugged program is contained inside a try block which handles the
*DebuggerQuit* exception.  However if you called the debugger was
started in the middle of a program, there might not be such an
exception handler; the debugged program still terminates but generally
with a traceback showing that exception.

If the debugged program is threaded or worse threaded and deadlocked,
raising an exception in one thread isn't going to quit the
program. For this see `exit` or `kill` for more forceful termination
commands.

Also, see `run` and `restart` for other ways to restart the debugged
program.
"""
        rst_tf.reset(80)
        got = Mformat.highlight(quit_text, rst_lex, rst_tf)
        self.assertEqual("""QUIT - gently terminate the debugged program. 

The program being debugged is aborted via a *DebuggerQuit* exception. 

When the debugger from the outside (e.g. via a "trepan" command), the debugged 
program is contained inside a try block which handles the *DebuggerQuit* 
exception. However if you called the debugger was started in the middle of a 
program, there might not be such an exception handler; the debugged program 
still terminates but generally with a traceback showing that exception. 

If the debugged program is threaded or worse threaded and deadlocked, raising 
an exception in one thread isn't going to quit the program. For this see "exit"
or "kill" for more forceful termination commands. 

Also, see "run" and "restart" for other ways to restart the debugged program. """,
                         got)

        text ='''
This is an example to show off *reformatting.*
We have several lines
here which should be reflowed.

But paragraphs should be respected.

    And verbatim
    text should not be
    touched

End of test.
'''
        rst_tf.reset(30)
        got = Mformat.highlight(text, rst_lex, rst_tf)
        self.assertEqual(
        """This is an example to show 
off *reformatting.* We have 
several lines here which 
should be reflowed. 

But paragraphs should be 
respected. 

    And verbatim
    text should not be
    touched


End of test. """,
            got)

         
        return