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


Python lexers.guess_lexer方法代码示例

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


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

示例1: guess_language

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def guess_language(raw: str, filename: Optional[str] = None) -> str:
    options = {'stripnl': True}

    # Guess a lexer based on filename and raw text first
    if filename:
        try:
            return guess_lexer_for_filename(filename, raw, **options).aliases[0]
        except (ValueError, IndexError):
            pass

    # If that didn't work guess lexer just by looking at the raw text
    try:
        language = guess_lexer(raw, **options).aliases[0]
    except (ValueError, IndexError):
        # If no lexer was detected, fallback to plain text.
        return 'text'

    # These are odd lexers that match far too often, so exclude them.
    if language in GUESS_LANG_IGNORES:
        return 'text'

    # Finally check for language overrides and return
    return GUESS_LANG_OVERRIDES.get(language, language) 
开发者ID:supakeen,项目名称:pinnwand,代码行数:25,代码来源:utility.py

示例2: test_guess_c_lexer

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def test_guess_c_lexer():
    code = '''
    #include <stdio.h>
    #include <stdlib.h>

    int main(void);

    int main(void) {
        uint8_t x = 42;
        uint8_t y = x + 1;

        /* exit 1 for success! */
        return 1;
    }
    '''
    lexer = guess_lexer(code)
    assert isinstance(lexer, CLexer) 
开发者ID:pygments,项目名称:pygments,代码行数:19,代码来源:test_cpp.py

示例3: get_lexer

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def get_lexer(self, src, language):
        """Get the Pygments lexer."""

        if language:
            language, lexer_options = self.get_extended_language(language)
        else:
            lexer_options = {}

        # Try and get lexer by the name given.
        try:
            lexer = get_lexer_by_name(language, **lexer_options)
        except Exception:
            lexer = None

        if lexer is None:
            if self.guess_lang:
                try:
                    lexer = guess_lexer(src)
                except Exception:  # pragma: no cover
                    pass
        if lexer is None:
            lexer = get_lexer_by_name('text')
        return lexer 
开发者ID:facelessuser,项目名称:sublime-markdown-popups,代码行数:25,代码来源:highlight.py

示例4: lang_for_block

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def lang_for_block(source, lang):
    if lang in ('py', 'python'):
        if source.startswith('>>>'):
            # interactive session
            return 'pycon'
        else:
            # maybe Python -- try parsing it
            if try_parse(source):
                return 'python'
            else:  # Guess
                return lang_for_block(source, 'guess')
    elif lang in ('python3', 'py3') and source.startswith('>>>'):
        # for py3, recognize interactive sessions, but do not try parsing...
        return 'pycon3'
    elif lang == 'guess':
        try:
            # return 'python'
            lexer = guess_lexer(source)
            return lexer.aliases[0]
        except Exception:
            return None
    else:
        return lang 
开发者ID:rst2pdf,项目名称:rst2pdf,代码行数:25,代码来源:pdfbuilder.py

示例5: highlight_file

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def highlight_file(self, linenos=True, style='default'):
        """ Highlight the input file, and return HTML as a string. """
        try:
            lexer = lexers.get_lexer_for_filename(self.input_file)
        except pygments.util.ClassNotFound:
            # Try guessing the lexer (file type) later.
            lexer = None

        try:
            formatter = formatters.HtmlFormatter(
                linenos=linenos,
                style=style,
                full=True)
        except pygments.util.ClassNotFound:
            logging.error("\nInvalid style name: {}\nExpecting one of:\n \
                {}".format(style, "\n    ".join(sorted(styles.STYLE_MAP))))
            sys.exit(1)

        try:
            with open(self.input_file, "r") as f:
                content = f.read()
                try:
                    lexer = lexer or lexers.guess_lexer(content)
                except pygments.util.ClassNotFound:
                    # No lexer could be guessed.
                    lexer = lexers.get_lexer_by_name("text")
        except EnvironmentError as exread:
            fmt = "\nUnable to read file: {}\n{}"
            logging.error(fmt.format(self.input_file, exread))
            sys.exit(2)

        return pygments.highlight(content, lexer, formatter) 
开发者ID:tushar-rishav,项目名称:code2pdf,代码行数:34,代码来源:code2pdf.py

示例6: render_fenced_code

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def render_fenced_code(self, element):
        code = element.children[0].children
        options = CodeHiliteRendererMixin.options.copy()
        options.update(_parse_extras(getattr(element, "extra", None)))
        if element.lang:
            try:
                lexer = get_lexer_by_name(element.lang, stripall=True)
            except ClassNotFound:
                lexer = guess_lexer(code)
        else:
            lexer = guess_lexer(code)
        formatter = html.HtmlFormatter(**options)
        return highlight(code, lexer, formatter) 
开发者ID:frostming,项目名称:marko,代码行数:15,代码来源:codehilite.py

示例7: set_lexer

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def set_lexer(self):
        """
        Try to detect the lexer by filename if it fails
        then try to guess the lex by shebang statement.
        
        The shebang statement should be placed in the first
        20 lines of the file.
        """

        try:
            self.lexer = get_lexer_for_filename(self.area.filename, '')
        except Exception as e:
            self.lexer = guess_lexer(self.area.get('1.0', '20.0')) 
开发者ID:vyapp,项目名称:vy,代码行数:15,代码来源:spider.py

示例8: render_block_code

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def render_block_code(self, token):
        code = token.children[0].content
        lexer = get_lexer(token.language) if token.language else guess_lexer(code)
        return highlight(code, lexer, self.formatter) 
开发者ID:miyuchina,项目名称:mistletoe,代码行数:6,代码来源:pygments_renderer.py

示例9: _highlight_code

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def _highlight_code(self, match):
        code = match.group(1)

        try:
            if self.body_lexer:
                lexer = get_lexer_by_name(self.body_lexer)
            else:
                lexer = guess_lexer(code.strip())
        except ClassNotFound:
            lexer = get_lexer_by_name('text')

        for item in lexer.get_tokens_unprocessed(code):
            yield item 
开发者ID:pygments,项目名称:pygments,代码行数:15,代码来源:textfmts.py

示例10: test_get_lexers

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def test_get_lexers():
    # test that the lexers functions work
    for func, args in [(lexers.get_lexer_by_name, ("python",)),
                       (lexers.get_lexer_for_filename, ("test.py",)),
                       (lexers.get_lexer_for_mimetype, ("text/x-python",)),
                       (lexers.guess_lexer, ("#!/usr/bin/python3 -O\nprint",)),
                       (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>"))
                       ]:
        x = func(opt='val', *args)
        assert isinstance(x, lexers.PythonLexer)
        assert x.options["opt"] == "val"

    for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items():
        assert cls == lexers.find_lexer_class(lname).__name__

        for alias in aliases:
            assert cls == lexers.get_lexer_by_name(alias).__class__.__name__

        for mimetype in mimetypes:
            assert cls == lexers.get_lexer_for_mimetype(mimetype).__class__.__name__

    try:
        lexers.get_lexer_by_name(None)
    except ClassNotFound:
        pass
    else:
        raise Exception 
开发者ID:pygments,项目名称:pygments,代码行数:29,代码来源:test_basic_api.py

示例11: highlight

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def highlight(html: str) -> str:
    soup = BeautifulSoup(html, "html5lib")
    for code in soup.find_all("code"):
        if not code.parent.name == "pre":
            continue
        lexer = guess_lexer(code.text)
        tag = BeautifulSoup(
            phighlight(code.text, lexer, _FORMATTER), "html5lib"
        ).body.next
        pre = code.parent
        pre.replaceWith(tag)
    out = soup.body
    out.name = "div"
    return str(out) 
开发者ID:tsileo,项目名称:microblog.pub,代码行数:16,代码来源:highlight.py

示例12: detect_language_from_file

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def detect_language_from_file(file_path):
    if os.path.exists(file_path):
        try:
            with open(file_path, 'r') as file:
                code = file.read()
                language = guess_lexer(code)
                if language.name == 'Objective-C':
                    return language.name
                else:
                    return "MATLAB"
        except:
            return None 
开发者ID:codersrank-org,项目名称:repo_info_extractor,代码行数:14,代码来源:detect_language_from_file.py

示例13: _default_lexer

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def _default_lexer(self):
        d = self.declaration
        if d.language:
            return lexers.find_lexer_class_by_name(d.language)()
        return lexers.guess_lexer(d.source) 
开发者ID:codelv,项目名称:enaml-web,代码行数:7,代码来源:lxml_code.py

示例14: syntax_hl

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def syntax_hl(src, lang=None, guess_lang=False, inline=False, code_wrap=False):
    """Highlight."""

    css_class = 'highlight'

    src = src.strip('\n')

    try:
        lexer = get_lexer_by_name(lang)
    except ValueError:
        try:
            if guess_lang:
                lexer = guess_lexer(src)
            else:
                lexer = get_lexer_by_name('text')
        except ValueError:
            lexer = get_lexer_by_name('text')
    if inline:
        formatter = SublimeInlineHtmlFormatter(
            cssclass=css_class
        )
    elif code_wrap:
        formatter = SublimeWrapBlockFormatter(
            cssclass=css_class
        )
    else:
        formatter = SublimeBlockFormatter(
            cssclass=css_class
        )
    return highlight(src, lexer, formatter) 
开发者ID:facelessuser,项目名称:sublime-markdown-popups,代码行数:32,代码来源:st_pygments_highlight.py

示例15: hilite

# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import guess_lexer [as 别名]
def hilite(self):
        """
        Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with
        optional line numbers. The output should then be styled with css to
        your liking. No styles are applied by default - only styling hooks
        (i.e.: <span class="k">).

        returns : A string of html.

        """

        self.src = self.src.strip('\n')

        if self.lang is None:
            self._getLang()

        if pygments:
            try:
                lexer = get_lexer_by_name(self.lang)
            except ValueError:
                try:
                    if self.guess_lang:
                        lexer = guess_lexer(self.src)
                    else:
                        lexer = TextLexer()
                except ValueError:
                    lexer = TextLexer()
            formatter = HtmlFormatter(linenos=self.linenos,
                                      cssclass=self.css_class,
                                      style=self.style,
                                      noclasses=self.noclasses)
            return highlight(self.src, lexer, formatter)
        else:
            # just escape and build markup usable by JS highlighting libs
            txt = self.src.replace('&', '&amp;')
            txt = txt.replace('<', '&lt;')
            txt = txt.replace('>', '&gt;')
            txt = txt.replace('"', '&quot;')
            classes = []
            if self.lang:
                classes.append('language-%s' % self.lang)
            if self.linenos:
                classes.append('linenums')
            class_str = ''
            if classes:
                class_str = ' class="%s"' % ' '.join(classes) 
            return '<pre class="%s"><code%s>%s</code></pre>\n'% \
                        (self.css_class, class_str, txt) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:50,代码来源:codehilite.py


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