当前位置: 首页>>代码示例>>Python>>正文


Python lexers.guess_lexer函数代码示例

本文整理汇总了Python中pygments.lexers.guess_lexer函数的典型用法代码示例。如果您正苦于以下问题:Python guess_lexer函数的具体用法?Python guess_lexer怎么用?Python guess_lexer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了guess_lexer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: parse

 def parse(self):
     """
     pygment highlighting
     """ 
     inner = ''
     for node in self.nodes:
         inner += node.raw_content
     if highlight is None:
         return '<pre>%s</pre>' % inner
     if self.arguments['lang']:
         try:
             lexer = get_lexer_by_name(self.arguments['lang'])
         except ClassNotFound:
             try:
                 lexer = guess_lexer(inner)
             except ClassNotFound:
                 lexer = TextLexer()
     else:
         try:
             lexer = guess_lexer(inner)
         except ClassNotFound:
             lexer = TextLexer()
     hl_line = self.arguments['hl_line']
     if not hl_line.isdigit():
         return self.soft_raise("Code argument hl_line must be digit")
     hl_line = int(hl_line)
     hl_lines = [hl_line] if hl_line else []
     formatter = HtmlFormatter(cssclass='code',
                               noclasses=True,
                               linenos='inline' if self.arguments['linenos'] == '1' else False,
                               hl_lines=hl_lines)
     hilighted = highlight(inner, lexer, formatter)
     return hilighted
开发者ID:DicaxDorcas,项目名称:DjangoBlog,代码行数:33,代码来源:text_formatting.py

示例2: _highlight_syntax

    def _highlight_syntax(self):
        """If we have pygments, syntax highlight the file."""
        if self.syntax_format:
            stringified_text = \
                ''.join(str(s.decode("utf-8")) for s in self.read_lines())

            if self.syntax_format is not True:
                lexer = get_lexer_by_name(self.syntax_format)

            elif self.file_name:
                try:
                    lexer = \
                        guess_lexer_for_filename(
                            self.file_name, stringified_text)
                except TypeError:
                    # XXX pygments py3 incompatibility workaround; fixed in tip
                    lexer = guess_lexer(stringified_text)
            else:
                lexer = guess_lexer(stringified_text)

            highlighted_text = \
                highlight(
                    stringified_text, lexer,
                    TerminalFormatter(bg="dark")).split('\n')

            line_num = 1
            try:
                for line in highlighted_text:
                    self._lines[
                        self._find_line_index(line_num)].update_highlight(line)
                    line_num += 1
            except KeyError:
                # our conversions to strings sometimes adds a trailing line
                pass
开发者ID:DerekMarshall,项目名称:py-ex,代码行数:34,代码来源:ex_buffer.py

示例3: highlight_code

    def highlight_code(self, string, pattern, lang_pattern):
        """Highlights syntaxes in the given string enclosed in a <syntax> tag.

        string -- String containing the code to highlight.
        pattern -- Compiled regex object for highlight pattern matching.
        lang_pattern -- Compiled regex for obtaining language name (if provided)
        
        """
        codeblocks = re.findall(pattern, string)
        # results: list of tuples of 2 or 3 items
        # item[0] is the whole code block (syntax tag + code to highlight)
        # item[1] is the programming language (optional, depends on usage)
        # item[2] is the code to highlight

        if codeblocks:
            for match, lang, code in codeblocks:
                if lang:
                    lang = re.sub(lang_pattern, '', lang)
                    try:
                        lexer = get_lexer_by_name(lang.lower(), **self.config.PYGMENTS_LEXER)
                    # if the lang is not supported or has a typo
                    # let pygments guess the language
                    except ClassNotFound:
                        lexer = guess_lexer(code, **self.config.PYGMENTS_LEXER)
                else:
                    lexer = guess_lexer(code, **self.config.PYGMENTS_LEXER)

                formatter = HtmlFormatter(**self.config.PYGMENTS_HTML)
                highlighted = highlight(code, lexer, formatter)
                # add 1 arg because replacement should only be done
                # once for each match
                string = string.replace(match, highlighted, 1)

        return string
开发者ID:nithinpb,项目名称:volt,代码行数:34,代码来源:syntax.py

示例4: __init__

    def __init__(self, file_content, file_name=None, language=None,
                 private=False):
        self.set_file_content(file_content)
        self.file_name = file_name
        self.language = language
        self.private = private
        self.pub_date = datetime.utcnow()

        # guess language, if needed
        if self.language is None:
            if self.file_name is None:
                lexer = guess_lexer(self.file_content)
            else:
                try:
                    lexer = guess_lexer_for_filename(self.file_name,
                                                     self.file_content)
                except:
                    lexer = guess_lexer(self.file_content)

                # verify if lexer is ok for filename
                found = False
                for pattern in lexer.filenames:
                    if fnmatch(self.file_name, pattern):
                        found = True
                        break
                else:
                    found = True
                if not found:
                    lexer = TextLexer
            self.language = lexer.aliases[0]
开发者ID:athmane,项目名称:ownpaste,代码行数:30,代码来源:models.py

示例5: colorize

def colorize(paste_key):
	" Color the paste in a deferred task. "
	
	from pygments import highlight
	from pygments.lexers import get_lexer_by_name
	from pygments.formatters import HtmlFormatter
	from pygments.lexers import guess_lexer
	
	paste = db.get(paste_key)
	
	if paste.syntax == 'auto':
		lexer = guess_lexer(paste.text)
	else:
		lexer = get_lexer_by_name(paste.syntax, stripall=True)
		if lexer is None:
			lexer = guess_lexer(paste.text)
		
	if lexer is None:
		lexer = get_lexer_by_name('text')
		
	formatter = HtmlFormatter(linenos=True, cssclass="highlight")
	paste.syntax = lexer.name
	paste.highlighted = highlight(paste.text, lexer, formatter)
	paste.colorized=True
	paste.put()
开发者ID:KeyIngredient,项目名称:gaepaste,代码行数:25,代码来源:tasks.py

示例6: highlight_content

def highlight_content(content, lexer_name = None):
    if not lexer_name:
        lexer = guess_lexer(content)
    else:
        try:
            lexer = get_lexer_by_name(lexer_name)
        except ClassNotFound:
            lexer = guess_lexer(content)
    formatter = HtmlFormatter()
    return highlight(content.decode('utf8'), lexer, formatter)
开发者ID:RedKrieg,项目名称:sadbin,代码行数:10,代码来源:sadbin.py

示例7: get_lexer

 def get_lexer(self, code, language):
     if language:
         try:
             return get_lexer_by_name(language)
         except pygments.util.ClassNotFound:
             try:
                 return get_lexer_by_name(language.lower())
             except pygments.util.ClassNotFound:
                 return guess_lexer(code)
     else:
         return guess_lexer(code)
开发者ID:karpitsky,项目名称:motor-blog,代码行数:11,代码来源:markup.py

示例8: apply_pygments

def apply_pygments(str):
    lines = str.splitlines()
    str = '\n'.join(lines[1:-1])
    first = lines[0].strip().lstrip('`')
    if first:
        try:
            lexer = get_lexer_by_name(first)
        except:
            lexer = guess_lexer(str)
    else:
        lexer = guess_lexer(str)
    formatter = HtmlFormatter(style='colorful')
    return highlight(str, lexer, formatter)
开发者ID:jongman,项目名称:jmk.pe.kr,代码行数:13,代码来源:utils.py

示例9: highlight

def highlight(text, filename=None):
    """Guess language in *text* and return its html highlighted version"""
    if filename is not None:
        #Pygments does not know dpr
        filename = filename.replace('dpr', 'pas')
        try:
            lexer = guess_lexer_for_filename(filename, text)
        except pygments.util.ClassNotFound:
            lexer = guess_lexer(text)
    else:
        lexer = guess_lexer(text)
    formatter = HtmlFormatter(style='manni', linenos=True, classprefix="highlight-")
    return formatter.get_style_defs(), p_highlight(text, lexer, formatter)
开发者ID:AVBelyy,项目名称:TSWeb,代码行数:13,代码来源:util.py

示例10: get_lexer

def get_lexer(filename, data, lexer='auto'):
    """
    Return a particular lexer instance.

    This method wraps pygments' methods guess_lexer_for_filename()
    and guess_lexer(). First guess_lexer_for_filename() is invoked;
    if there is no result the presence of a shebang is checked.
    If the subsequent call to guess_lexer() does not bring any
    results the fallback value TextLexer() is returned.

    Args:
        filename    The name of the file to be displayed
        data        The content of the file (utf-8 encoded)
        lexer       Specifying another value than 'auto' skips any
                    guess_lexer() calls. The pygments method
                    get_lexer_by_name() is used to find a particular
                    lexer class. If nothing has been found, c.py
                    fails.
    """
    filename = filename if filename else '-'
    if lexer == 'auto':
        debug("Trying to guess lexer for filename: '{}'".format(filename))
        try:
            lexer_cls = guess_lexer_for_filename(filename, data)
        except ClassNotFound:
            if data[0:2] == b'#!':
                debug("Shebang '{}' present".format(data.splitlines()[0]))
                lexer_cls = guess_lexer(data.decode())
            elif filename == '-':
                try:
                    debug("Have read from 'stdin'; guessing lexer for content")
                    lexer_cls = guess_lexer(data)
                except ClassNotFound:
                    debug('Guessing failed, using fallback lexer')
                    lexer_cls = TextLexer()
            else:
                debug('No shebang present, using fallback lexer')
                lexer_cls = TextLexer()
        except TypeError:
            debug('Guessing failed, using fallback lexer')
            lexer_cls = TextLexer()
    else:
        try:
            debug("Trying to find lexer: '{}'".format(lexer))
            lexer_cls = get_lexer_by_name(lexer)
        except ClassNotFound:
            print("[Error] No lexer found: '{}'".format(lexer), file=sys.stderr)
            exit(1)

    debug('Using lexer: {}'.format(lexer_cls))
    return lexer_cls
开发者ID:rumpelsepp,项目名称:c.py,代码行数:51,代码来源:c.py

示例11: get_language

def get_language(source, code, language=None):
    """Get the current language we're documenting, based on the extension."""

    if language is not None:
        for l in languages.values():
            if l["name"] == language:
                return l
        else:
            raise ValueError("Unknown forced language: " + language)

    m = re.match(r'.*(\..+)', os.path.basename(source)) if source else None
    if m and m.group(1) in languages:
        return languages[m.group(1)]
    else:
        try:
            lang = lexers.guess_lexer(code).name.lower()
            for l in languages.values():
                if l["name"] == lang:
                    return l
            else:
                raise ValueError()
        except ValueError:
            # If pygments can't find any lexers, it will raise its own
            # subclass of ValueError. We will catch it and raise ours
            # for consistency.
            raise ValueError("Can't figure out the language!")
开发者ID:iamtrask,项目名称:pycco,代码行数:26,代码来源:main.py

示例12: _blob_detail

def _blob_detail(request, project, branch_name, git_object, path_list, branches):
    """Render a blob. Pretty prints using Pygments"""
    breadcrumbs = generate_breadcrumbs(path_list)
    
    file_name = path_list[-1]['name']
    try:
        lexer = guess_lexer_for_filename(file_name, git_object.as_raw_string())
    except:
        lexer = guess_lexer(git_object.as_raw_string())
    formatter = HtmlFormatter(linenos=True)
    pretty_printed_file = highlight(git_object.as_raw_string(), lexer, formatter)
    
    return render_to_response('project/blob.html', {
        'project': project,
        'branch_name': branch_name,
        'object': git_object,
        'path': path_list,
        'breadcrumbs': breadcrumbs,
        'pretty_print': pretty_printed_file,
        'branches': branches,
    }, context_instance=RequestContext(request))
    
    
    
    
    
开发者ID:wiremine,项目名称:cannonball,代码行数:21,代码来源:views.py

示例13: get_lexer_name_from_code

def get_lexer_name_from_code(code):
    try:
        return guess_lexer(code, encoding=None).aliases[0]
    except ClassNotFound:
        pass
    
    return ''
开发者ID:Dav1dde,项目名称:vp,代码行数:7,代码来源:util.py

示例14: _render

    def _render(self):
        if self.children:
            if not _pygments_available:
                raise NotAvailableError("Pygments is not available")

            try:
                lex_name = self.haml.split(" ", 1)[1]
            except IndexError:
                lex_name = False

            self.before = self.render_newlines()
            indent_offset = len(self.children[0].spaces)
            text = ''.join(''.join([c.spaces[indent_offset:], c.haml, c.render_newlines()]) for c in self.children)

            try:
                if lex_name:
                    lexer = get_lexer_by_name(lex_name)
                else:
                    lexer = guess_lexer(text)
            except ClassNotFound:
                # if invalid lexer name is given or
                # if no lexer thinks it can handle the content
                # we set default lexer
                lexer = TextLexer()

            self.before += highlight(text, lexer, HtmlFormatter())
        else:
            self.after = self.render_newlines()
开发者ID:Thyrst,项目名称:HamlPy,代码行数:28,代码来源:nodes.py

示例15: get_line_types

def get_line_types(repo, repo_uri, rev, path):
    """Returns an array, where each item means a line of code.
       Each item is labled 'code', 'comment' or 'empty'"""

    #profiler_start("Processing LineTypes for revision %s:%s", (self.rev, self.file_path))
    uri = os.path.join(repo_uri, path) # concat repo_uri and file_path for full path
    file_content = _get_file_content(repo, uri, rev)  # get file_content

    if file_content is None or file_content == '':
        printerr("[get_line_types] Error: No file content for " + str(rev) + ":" + str(path) + " found! Skipping.")
        line_types = None
    else:
        try:
            lexer = get_lexer_for_filename(path)
        except ClassNotFound:
            try:
                printdbg("[get_line_types] Guessing lexer for" + str(rev) + ":" + str(path) + ".")
                lexer = guess_lexer(file_content)
            except ClassNotFound:
                printdbg("[get_line_types] No guess or lexer found for " + str(rev) + ":" + str(path) + ". Using TextLexer instead.")
                lexer = TextLexer()

        if isinstance(lexer, NemerleLexer):
            # this lexer is broken and yield an unstoppable process
            # see https://bitbucket.org/birkenfeld/pygments-main/issue/706/nemerle-lexer-ends-in-an-infinite-loop
            lexer = TextLexer()

        # Not shure if this should be skipped, when the language uses off-side rules (e.g. python,
        # see http://en.wikipedia.org/wiki/Off-side_rule for list)
        stripped_code = _strip_lines(file_content)
        lexer_output = _iterate_lexer_output(lexer.get_tokens(stripped_code))
        line_types_str = _comment_empty_or_code(lexer_output)
        line_types = line_types_str.split("\n")

    return line_types
开发者ID:ProjectHistory,项目名称:MininGit,代码行数:35,代码来源:line_types.py


注:本文中的pygments.lexers.guess_lexer函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。