Python pygments.lexers.get_lexer_by_name() Examples
The following are 30
code examples of pygments.lexers.get_lexer_by_name().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygments.lexers
, or try the search function
.
Example #1
Source File: main.py From dcc with Apache License 2.0 | 6 votes |
def androaxml_main(inp, outp=None, resource=None): ret_type = androconf.is_android(inp) if ret_type == "APK": a = apk.APK(inp) if resource: if resource not in a.files: print("The APK does not contain a file called '{}'".format(resource), file=sys.stderr) sys.exit(1) axml = AXMLPrinter(a.get_file(resource)).get_xml_obj() else: axml = a.get_android_manifest_xml() elif ".xml" in inp: axml = AXMLPrinter(read(inp)).get_xml_obj() else: print("Unknown file type") sys.exit(1) buff = etree.tostring(axml, pretty_print=True, encoding="utf-8") if outp: with open(outp, "wb") as fd: fd.write(buff) else: sys.stdout.write(highlight(buff.decode("UTF-8"), get_lexer_by_name("xml"), TerminalFormatter()))
Example #2
Source File: highlight.py From mistune-contrib with BSD 3-Clause "New" or "Revised" License | 6 votes |
def block_code(text, lang, inlinestyles=False, linenos=False): if not lang: text = text.strip() return u'<pre><code>%s</code></pre>\n' % mistune.escape(text) try: lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter( noclasses=inlinestyles, linenos=linenos ) code = highlight(text, lexer, formatter) if linenos: return '<div class="highlight-wrapper">%s</div>\n' % code return code except: return '<pre class="%s"><code>%s</code></pre>\n' % ( lang, mistune.escape(text) )
Example #3
Source File: code_analyzer.py From deepWordBug with Apache License 2.0 | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #4
Source File: sql.py From pySINDy with MIT License | 6 votes |
def _get_lexer(self, lang): if lang.lower() == 'sql': return get_lexer_by_name('postgresql', **self.options) tries = [lang] if lang.startswith('pl'): tries.append(lang[2:]) if lang.endswith('u'): tries.append(lang[:-1]) if lang.startswith('pl') and lang.endswith('u'): tries.append(lang[2:-1]) for l in tries: try: return get_lexer_by_name(l, **self.options) except ClassNotFound: pass else: # TODO: better logging # print >>sys.stderr, "language not found:", lang return None
Example #5
Source File: jupyter_widget.py From pySINDy with MIT License | 6 votes |
def _handle_kernel_info_reply(self, rep): """Handle kernel info replies.""" content = rep['content'] language_name = content['language_info']['name'] pygments_lexer = content['language_info'].get('pygments_lexer', '') try: # Other kernels with pygments_lexer info will have to be # added here by hand. if pygments_lexer == 'ipython3': lexer = IPython3Lexer() elif pygments_lexer == 'ipython2': lexer = IPythonLexer() else: lexer = get_lexer_by_name(language_name) self._highlighter._lexer = lexer except ClassNotFound: pass self.kernel_banner = content.get('banner', '') if self._starting: # finish handling started channels self._starting = False super(JupyterWidget, self)._started_channels()
Example #6
Source File: highlight.py From Computable with MIT License | 6 votes |
def _pygment_highlight(source, output_formatter, language='ipython'): """ Return a syntax-highlighted version of the input source Parameters ---------- source : str Source code to highlight the syntax of. output_formatter : Pygments formatter language : str Language to highlight the syntax of. """ if language == 'ipython': lexer = IPythonLexer() else: lexer = get_lexer_by_name(language, stripall=True) return pygements_highlight(source, lexer, output_formatter)
Example #7
Source File: operations.py From wordinserter with MIT License | 6 votes |
def highlighted_operations(self): from pygments.lexers import get_lexer_by_name from pygments.util import ClassNotFound from pygments import highlight from pygments.formatters import get_formatter_by_name from wordinserter import parse import warnings try: formatter = get_formatter_by_name("html") lexer = get_lexer_by_name(self.highlight) except ClassNotFound: warnings.warn("Lexer {0} or formatter html not found, not highlighting".format(self.highlight)) return None formatter.noclasses = True highlighted_code = highlight(self.text, lexer=lexer, formatter=formatter) return parse(highlighted_code, parser="html")
Example #8
Source File: code_analyzer.py From bash-lambda-layer with MIT License | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #9
Source File: mistune_markdown.py From Django-blog with MIT License | 6 votes |
def block_code(self, code, lang=None): """Rendering block level code. ``pre > code``. :param code: text content of the code block. :param lang: language of the given code. """ code = code.rstrip('\n') # 去掉尾部的换行符 # 如果没有lang, 就返回代码块 if not lang: code = mistune.escape(code) return '<pre><code>%s\n</code></pre>\n' % code # 给代码加上高亮 例如: lang='python'的话 # ```python # print('666') # ``` try: lexer = get_lexer_by_name(lang, stripall=True) except ClassNotFound: # 如果lang是不合法, 没有匹配到, 就设置为python lexer = get_lexer_by_name('python', stripall=True) formatter = html.HtmlFormatter() # linenos=True return highlight(code, lexer, formatter)
Example #10
Source File: code_analyzer.py From faces with GNU General Public License v2.0 | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #11
Source File: code_analyzer.py From faces with GNU General Public License v2.0 | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #12
Source File: code_analyzer.py From cadquery-freecad-module with GNU Lesser General Public License v3.0 | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #13
Source File: decompiler.py From dcc with Apache License 2.0 | 6 votes |
def get_source_method(self, m): """ Return the Java source of a single method :param m: `EncodedMethod` Object :return: """ class_name = m.get_class_name() method_name = m.get_name() if class_name not in self.classes: return "" lexer = get_lexer_by_name("java", stripall=True) lexer.add_filter(MethodFilter(method_name=method_name)) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result
Example #14
Source File: code_analyzer.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #15
Source File: sql.py From pigaios with GNU General Public License v3.0 | 6 votes |
def _get_lexer(self, lang): if lang.lower() == 'sql': return get_lexer_by_name('postgresql', **self.options) tries = [lang] if lang.startswith('pl'): tries.append(lang[2:]) if lang.endswith('u'): tries.append(lang[:-1]) if lang.startswith('pl') and lang.endswith('u'): tries.append(lang[2:-1]) for l in tries: try: return get_lexer_by_name(l, **self.options) except ClassNotFound: pass else: # TODO: better logging # print >>sys.stderr, "language not found:", lang return None
Example #16
Source File: code_analyzer.py From AWS-Transit-Gateway-Demo-MultiAccount with MIT License | 6 votes |
def __init__(self, code, language, tokennames='short'): """ Set up a lexical analyzer for `code` in `language`. """ self.code = code self.language = language self.tokennames = tokennames self.lexer = None # get lexical analyzer for `language`: if language in ('', 'text') or tokennames == 'none': return if not with_pygments: raise LexerError('Cannot analyze code. ' 'Pygments package not found.') try: self.lexer = get_lexer_by_name(self.language) except pygments.util.ClassNotFound: raise LexerError('Cannot analyze code. ' 'No Pygments lexer found for "%s".' % language) # Since version 1.2. (released Jan 01, 2010) Pygments has a # TokenMergeFilter. However, this requires Python >= 2.4. When Docutils # requires same minimal version, ``self.merge(tokens)`` in __iter__ can # be replaced by ``self.lexer.add_filter('tokenmerge')`` in __init__.
Example #17
Source File: other.py From mindpark with GNU General Public License v3.0 | 6 votes |
def color_stack_trace(): def excepthook(type_, value, trace): text = ''.join(traceback.format_exception(type_, value, trace)) try: from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import TerminalFormatter lexer = get_lexer_by_name('pytb', stripall=True) formatter = TerminalFormatter() sys.stderr.write(highlight(text, lexer, formatter)) except Exception: sys.stderr.write(text) sys.stderr.write('Failed to colorize the traceback.') sys.excepthook = excepthook setup_thread_excepthook()
Example #18
Source File: jinja2_filters.py From kube-web-view with GNU General Public License v3.0 | 5 votes |
def highlight(value, linenos=False): if linenos: formatter = HtmlFormatter( lineanchors="line", anchorlinenos=True, linenos="table", linespans="yaml-line", ) else: formatter = HtmlFormatter() return pygments.highlight(value, get_lexer_by_name("yaml"), formatter)
Example #19
Source File: decompiler.py From MARA_Framework with GNU Lesser General Public License v3.0 | 5 votes |
def get_source_method(self, method): class_name = method.get_class_name() method_name = method.get_name() if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) lexer.add_filter(MethodFilter(method_name=method_name)) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #20
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_source_method(self, method): class_name = method.get_class_name() method_name = method.get_name() if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) lexer.add_filter(MethodFilter(method_name=method_name)) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #21
Source File: pygments2xpre.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def pygments2xpre(s, language="python"): "Return markup suitable for XPreformatted" try: from pygments import highlight from pygments.formatters import HtmlFormatter except ImportError: return s from pygments.lexers import get_lexer_by_name rconv = lambda x: x if isPy3: out = getStringIO() else: if isUnicode(s): s = asBytes(s) rconv = asUnicode out = getBytesIO() l = get_lexer_by_name(language) h = HtmlFormatter() highlight(s,l,h,out) styles = [(cls, style.split(';')[0].split(':')[1].strip()) for cls, (style, ttype, level) in h.class2style.items() if cls and style and style.startswith('color:')] return rconv(_2xpre(out.getvalue(),styles))
Example #22
Source File: decompiler.py From MARA_Framework with GNU Lesser General Public License v3.0 | 5 votes |
def get_all(self, class_name): if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #23
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_all(self, class_name): if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #24
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_source_method(self, method): class_name = method.get_class_name() method_name = method.get_name() if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) lexer.add_filter(MethodFilter(method_name=method_name)) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #25
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_all(self, class_name): if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #26
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_source_method(self, method): class_name = method.get_class_name() method_name = method.get_name() if class_name not in self.classes: return "" lexer = get_lexer_by_name("java", stripall=True) lexer.add_filter(MethodFilter(method_name=method_name)) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result
Example #27
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def display_all(self, _class): result = self.get_source_class(_class) if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) formatter = TerminalFormatter() result = highlight(result, lexer, formatter) print result
Example #28
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_source_method(self, method): class_name = method.get_class_name() method_name = method.get_name() if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) lexer.add_filter(MethodFilter(method_name=method_name)) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]
Example #29
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def display_source(self, m): result = self.get_source_method(m) if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) formatter = TerminalFormatter() result = highlight(result, lexer, formatter) print result
Example #30
Source File: decompiler.py From AndroBugs_Framework with GNU General Public License v3.0 | 5 votes |
def get_all(self, class_name): if class_name not in self.classes: return "" if PYGMENTS: lexer = get_lexer_by_name("java", stripall=True) formatter = TerminalFormatter() result = highlight(self.classes[class_name], lexer, formatter) return result return self.classes[class_name]