-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mod] refactor, encapsulate pygments optional dependancy in a module
- Loading branch information
1 parent
4be526f
commit feb52fa
Showing
5 changed files
with
59 additions
and
58 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
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,50 @@ | ||
HAS_PYGMENTS = True | ||
|
||
try: | ||
import pygments | ||
except ImportError: | ||
HAS_PYGMENTS = False | ||
|
||
|
||
if HAS_PYGMENTS: | ||
from pygments.token import Comment, Text, String, Keyword, Name, Operator | ||
from pygments.lexer import RegexLexer, bygroups | ||
from pygments import highlight | ||
from pygments.lexers import PythonLexer | ||
from pygments.formatters import Terminal256Formatter, HtmlFormatter | ||
|
||
|
||
class HelpLexer(RegexLexer): | ||
name = 'Lexer for RedBaron .help() method output' | ||
|
||
tokens = { | ||
'root': [ | ||
(r"#.*$", Comment), | ||
(r"('([^\\']|\\.)*'|\"([^\\\"]|\\.)*\")", String), | ||
(r"(None|False|True)", String), | ||
(r'(\*)( \w+Node)', bygroups(Operator, Keyword)), | ||
(r'\w+Node', Name.Function), | ||
(r'(\*|=|->|\(|\)|\.\.\.)', Operator), | ||
(r'\w+', Text), | ||
(r'\s+', Text), | ||
] | ||
} | ||
|
||
def help_highlight(string): | ||
return highlight(string, HelpLexer(), Terminal256Formatter(style='monokai')) | ||
|
||
def python_highlight(string): | ||
return highlight(string, PythonLexer(encoding="Utf-8"), | ||
Terminal256Formatter(style='monokai', | ||
encoding="Utf-8")) | ||
|
||
def python_html_highlight(string): | ||
return highlight(string, PythonLexer(encode="Utf-8"), | ||
HtmlFormatter(noclasses=True, encoding="UTf-8")) | ||
|
||
else: | ||
def help_highlight(string): | ||
return string | ||
|
||
def python_highlight(string): | ||
return string |
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