コード例 #1
0
ファイル: highlighting.py プロジェクト: jahir/klaus
def highlight_or_render(code, filename, render_markup=True, ctags=None, ctags_baseurl=None):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.

    :param code: the program code to highlight, str
    :param filename: name of the source file the code is taken from, str
    :param render_markup: whether to render markup if possible, bool
    :param ctags: tagsfile obj used for source code hyperlinks, ``ctags.CTags``
    :param ctags_baseurl: base url used for source code hyperlinks, str
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()

    formatter_cls = {
        'Python': KlausPythonFormatter,
    }.get(lexer.name, KlausDefaultFormatter)
    if ctags:
        ctags_urlscheme = ctags_baseurl + "%(path)s%(fname)s%(fext)s"
    else:
        ctags_urlscheme = None
    formatter = formatter_cls(
        language=PYGMENTS_CTAGS_LANGUAGE_MAP.get(lexer.name),
        ctags=ctags,
        tagurlformat=ctags_urlscheme,
    )

    return highlight(code, lexer, formatter)
コード例 #2
0
def pygmentize(code, filename, render_markup, ctags=None, ctags_baseurl=None):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.

    :param code: the program code to highlight, str
    :param filename: name of the source file the code is taken from, str
    :param render_markup: whether to render markup if possible, bool
    :param ctags: tagsfile obj used for source code hyperlinks, ``ctags.CTags``
    :param ctags_baseurl: base url used for source code hyperlinks, str
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()

    formatter_cls = {
        'Python': KlausPythonFormatter,
    }.get(lexer.name, KlausDefaultFormatter)
    if ctags:
        ctags_urlscheme = ctags_baseurl + "%(path)s%(fname)s%(fext)s"
    else:
        ctags_urlscheme = None
    formatter = formatter_cls(
        language=PYGMENTS_CTAGS_LANGUAGE_MAP.get(lexer.name),
        ctags=ctags,
        tagurlformat=ctags_urlscheme,
    )

    return highlight(code, lexer, formatter)
コード例 #3
0
ファイル: utils.py プロジェクト: TomLottermann/klaus
def pygmentize(code, filename=None, render_markup=True):
    """
    Renders code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    lexer = get_lexer_for_filename(filename, code)
    return highlight(code, lexer, KlausFormatter())
コード例 #4
0
ファイル: utils.py プロジェクト: TomLottermann/klaus
def pygmentize(code, filename=None, render_markup=True):
    """
    Renders code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    lexer = get_lexer_for_filename(filename, code)
    return highlight(code, lexer, KlausFormatter())
コード例 #5
0
ファイル: repo.py プロジェクト: ewdurbin/klaus
    def get_readme(self):
        rev = self.get_default_branch()
        commit = self.get_commit(rev)
        tree = self.get_blob_or_tree(commit, "/")

        for item in tree.items():
            if item.path.startswith("README."):
                content = self[item.sha].data
                if can_render(item.path):
                    return {"rendered": True, "content": render(item.path, content)}
                else:
                    return {"rendered": False, "content": force_unicode(content)}

        return None
コード例 #6
0
ファイル: utils.py プロジェクト: zhanghongquan/klaus
def pygmentize(code, filename=None, render_markup=True):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()
    return highlight(code, lexer, KlausFormatter())
コード例 #7
0
ファイル: utils.py プロジェクト: marslabtron/klaus
def pygmentize(code, filename=None, render_markup=True):
    """Render code using Pygments, markup (markdown, rst, ...) using the
    corresponding renderer, if available.
    """
    if render_markup and markup.can_render(filename):
        return markup.render(filename, code)

    try:
        lexer = get_lexer_for_filename(filename, code)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()
    return highlight(code, lexer, KlausFormatter())