我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用pygments.lexers.get_lexer_for_filename()。
def view_source(context, web_view, filename): from base64 import b64encode from java.io import BufferedReader, InputStreamReader from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import get_lexer_for_filename stream = context.getAssets().open(join(ASSET_SOURCE_DIR, filename)) reader = BufferedReader(InputStreamReader(stream)) text = "\n".join(iter(reader.readLine, None)) formatter = HtmlFormatter() body = highlight(text, get_lexer_for_filename(filename), formatter) html = ("<html><head><style>{}\n{}</style></head><body>{}</body></html>" .format(formatter.get_style_defs(), EXTRA_CSS, body)).encode() web_view.loadData(b64encode(html).decode(), "text/html", "base64")
def tokenize_file(source_file, language=None, literal_option=3): print(source_file) code = "" try: with codecs.open(source_file, "r",encoding='utf-8', errors='ignore') as f: code = f.read() except UnicodeDecodeError: return '', [] if language is None: try: lexer = get_lexer_for_filename(source_file) language = languageForLexer(lexer) except KeyError: # Not a valid extension lexer = guess_lexer(code) language = languageForLexer(lexer) else: lexer = get_lexer_by_name(language) return tokenize_code(code, lexer, language, literal_option)
def get_lexer(self): # this function gets the lexer depending on the files name try: lexer = get_lexer_for_filename(self.short_name) except pygments.util.ClassNotFound: lexer = TextLexer() lexer.add_filter('tokenmerge') return lexer
def from_filename(cls, filename, sync_from_start=True): """ Create a `Lexer` from a filename. """ # Inline imports: the Pygments dependency is optional! from pygments.util import ClassNotFound from pygments.lexers import get_lexer_for_filename try: pygments_lexer = get_lexer_for_filename(filename) except ClassNotFound: return SimpleLexer() else: return cls(pygments_lexer.__class__, sync_from_start=sync_from_start)
def view_file(filename): """ Views file with syntax highlighting (if applicable) Args: filename (str): Full path to filename to render view response for. """ folder = filename.split(_AEON_TOPDIR).pop().strip('/') filename = os.path.join(_AEON_TOPDIR, filename) try: with open(filename, 'r') as f: data = f.read() # lexer = guess_lexer_for_filename(filename, data) formatter = HtmlFormatter(linenos=True) try: lexer = get_lexer_for_filename(filename) code = highlight(data, lexer, formatter) except ClassNotFound: lexer = TextLexer() code = highlight(data, lexer, formatter) stat = os.stat(filename) return render_template('view.html', content=code, folder=folder, stat=stat, filename=filename) except (OSError, IOError) as e: code = e[0] reason = e[1] flash('Error: Could not view file {filename}: {reason} ({code})'.format(filename=filename, reason=reason, code=code), 'danger') return render_template('view.html')
def from_filename(cls, filename, sync_from_start=True): """ Create a `Lexer` from a filename. """ from pygments.util import ClassNotFound from pygments.lexers import get_lexer_for_filename try: pygments_lexer = get_lexer_for_filename(filename) except ClassNotFound: return SimpleLexer() else: return cls(pygments_lexer.__class__, sync_from_start=sync_from_start)
def fetch_lexer( source: str, language: str = None, filename: str = None, mime_type: str = None ) -> Lexer: """ :param source: :param language: :param filename: :param mime_type: :return: """ environ.abort_thread() try: if language: return get_lexer_by_name(language, stripall=True) except ClassNotFound: pass if filename: try: return get_lexer_for_filename(filename, stripall=True) except ClassNotFound: pass try: return guess_lexer_for_filename(filename, source, stripall=True) except ClassNotFound: pass try: if mime_type: return get_lexer_for_mimetype(mime_type, stripall=True) except ClassNotFound: pass try: return guess_lexer(source, stripall=True) except ClassNotFound: return TextLexer()