예제 #1
0
 def block_code(self, code, lang):
     if not lang:
         return '\n<pre><code>%s</code></pre>\n' % \
             mistune.escape(code)
     lexer = lexers.get_lexer_by_name(lang, stripall=True)
     formatter = formatters.HtmlFormatter()
     return highlight(code, lexer, formatter)
예제 #2
0
def pygmentize(value):
    """
    A filter to highlight code blocks in html with Pygments and BeautifulSoup.

        {% load pygmentize %}

        {{ var_with_code|pygmentize|safe }}

    If you want the code to be highligted for, you have to to put 'class'
    in the <pre> elements. The pre-formatted block will turn to a <code> block.
    """
    soup = BeautifulSoup(unicode(value))
    for block in soup.findAll('pre'):
        #Process HTML -> Replace whitespaces
        #code = block.renderContents().replace('<br />', "\n").replace('&nbsp;', ' ').replace('&gt;', '>')
        if block.has_key("class"):
            try:
                code = ''.join([unicode(item) for item in block.contents])
                lexer = lexers.get_lexer_by_name(block['class'][0])
                #block.replaceWith(highlight(code, lexer, HtmlFormatter(cssclass="monokai")))
                #formatter = formatters.HtmlFormatter(cssclass="monokai")
                formatter = formatters.HtmlFormatter(linenos=True,
                                                     cssclass="monokai")
                #formatter = formatters.HtmlFormatter()
                code_hl = highlight(code, lexer, formatter)
                block.contents = [BeautifulSoup(code_hl)]
                block.name = 'code'
            except:
                raise
    return unicode(soup)
예제 #3
0
def pygmentize(code_raw, language):
    lexer = lexers.get_lexer_by_name(language,
                                     encoding='utf-8',
                                     startinline=True)
    return highlight(
        code_raw, lexer,
        formatters.HtmlFormatter(encoding="utf-8", startinline=True))
예제 #4
0
def syntax_highlight(value, language):
    """Adds pygments syntax highlighting to a given code input"""
    lexer = lexers.get_lexer_by_name(language)
    output = pygments.highlight(
        value, lexer, formatters.HtmlFormatter(cssclass="syntax")
    )
    return mark_safe(output)
예제 #5
0
    def highlight_file(self, input_file, linenos=True, style='default'):
        """ Highlight the input file, and return HTML as a string. """
        try:
            lexer = lexers.get_lexer_for_filename(input_file)
        except pygments.util.ClassNotFound:
            # Try guessing the lexer (file type) later.
            lexer = None

        try:
            formatter = formatters.HtmlFormatter(
                linenos='table',
                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)
            print("\nInvalid style name: {}\nExpecting one of:\n \
                {}".format(style, "\n    ".join(sorted(styles.STYLE_MAP))))

        try:
            with open(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{}"
            print(fmt.format(input_file, exread))
            # logging.error(fmt.format(input_file, exread))
            # sys.exit(2)

        return pygments.highlight(content, lexer, formatter)
    def __init__(self, parent, lexer=None):
        super(PygmentsHighlighter, self).__init__(parent)

        self._lexer = lexer if lexer else lexers.PythonLexer()
        self.set_style('default')
        self._document = QtGui.QTextDocument()
        self._formatter = formatters.HtmlFormatter(nowrap=True)
예제 #7
0
파일: jinja.py 프로젝트: wilsoniya/hyde
def syntax(env, value, lexer=None, filename=None):
    """
    Processes the contained block using `pygments`
    """
    try:
        import pygments
        from pygments import lexers
        from pygments import formatters
    except ImportError:
        logger.error(u"pygments library is required to"
                     " use syntax highlighting tags.")
        raise TemplateError("Cannot load pygments")

    pyg = (lexers.get_lexer_by_name(lexer)
           if lexer else lexers.guess_lexer(value))
    settings = {}
    if hasattr(env.config, 'syntax'):
        settings = getattr(env.config.syntax, 'options', Expando({})).to_dict()

    formatter = formatters.HtmlFormatter(**settings)
    code = pygments.highlight(value, pyg, formatter)
    code = code.replace('\n\n', '\n&nbsp;\n').replace('\n', '<br />')
    caption = filename if filename else pyg.name
    if hasattr(env.config, 'syntax'):
        if not getattr(env.config.syntax, 'use_figure', True):
            return Markup(code)
    return Markup(
        '<div class="codebox"><figure class="code">%s<figcaption>%s</figcaption></figure></div>\n\n'
        % (code, caption))
예제 #8
0
    def filter(self, source, **kwargs):
        encoded = super(CodeHighlighter, self).filter(source, **kwargs)
        try:
            from pygments import highlight
            from pygments import lexers
            from pygments import formatters
        except ImportError as ex:
            print('<%s> - Failed to import pygments! (%s)' %
                  (self.__class__.__name__, ex))
            print('-- You may need to install it from: http://pygments.org')
            return encoded

        lexer = None
        try:
            lexer = lexers.guess_lexer(source)
        except lexers.ClassNotFound:
            lexer = lexers.PythonLexer()

        formatter = formatters.HtmlFormatter(cssclass='code_highlighter')
        encoded = highlight(encoded, lexer, formatter)
        css = formatter.get_style_defs('.code_highlighter')
        return '''<style type="text/css"><!--
                %(css)s
            --></style>%(source)s''' % {
            'css': css,
            'source': encoded
        }
예제 #9
0
파일: main.py 프로젝트: jasper9/bender
def example():
    from pygments import highlight, lexers, formatters
    message = "Basic examples of how to use the service.  This is how you can get details for exact builds.  There is more ideas to come, most notably a way of finding out if X is compatible if Y, or exactly what is required etc.<br><br>"
    formatter = formatters.HtmlFormatter(full=True)
    css = formatter.get_style_defs()

    message += "curl https://bender.apps.pcfone.io/api/v1/vmware/esxi/9484548"

    obj = [
        "esxi", {
            "build": "9484548",
            "fullName": "ESXi 6.7 EP 03",
            "imageprofile": "ESXi-6.7.0-20180804001-standard",
            "releaseDate": "2018-08-14",
            "releaseName": "ESXi670-201808001",
            "releaseNotes": "https://kb.vmware.com/kb/56535"
        }
    ]

    formatted_json = json1.dumps(obj, sort_keys=True, indent=4)

    colorful_json = highlight(formatted_json, lexers.JsonLexer(),
                              formatters.HtmlFormatter())
    message += colorful_json

    message += "curl https://bender.apps.pcfone.io/api/v1/vmware/vcenter/linux/13010631"

    obj = "vcenters", {
        "build": "13010631",
        "build2": "13007421",
        "fullName": "vCenter Server 6.7 Update 2",
        "osType": "linux",
        "releaseDate": "2019-04-11",
        "releaseNotes": ""
    }

    formatted_json = json1.dumps(obj, sort_keys=True, indent=4)

    colorful_json = highlight(formatted_json, lexers.JsonLexer(),
                              formatters.HtmlFormatter())
    message += colorful_json

    title = "Title"
    return render_template("message.html",
                           message=message,
                           title=title,
                           css=css)
예제 #10
0
def _format(self, file):
    if self.attributes['arguments'] is None:
        self.attributes['arguments'] = {}

    linenos = False
    if ('numbers' in self.attributes['arguments']
            or 'numbers' in document.userdata['listings']):
        linenos = 'inline'

    # If this listing includes a label, inform plasTeX.
    if 'label' in list(self.attributes['arguments'].keys()):
        if hasattr(self.attributes['arguments']['label'], 'textContent'):
            self.ownerDocument.context.label(
                self.attributes['arguments']['label'].textContent)
        else:
            self.ownerDocument.context.label(
                self.attributes['arguments']['label'])

    # Check the textual LaTeX arguments and convert them to Python
    # attributes.
    if 'firstline' in list(self.attributes['arguments'].keys()):
        first_line_number = int(self.attributes['arguments']['firstline'])
    else:
        first_line_number = 0

    if 'lastline' in list(self.attributes['arguments'].keys()):
        last_line_number = int(self.attributes['arguments']['lastline'])
    else:
        last_line_number = sys.maxsize

    # Read the file, all the while respecting the "firstline" and
    # "lastline" arguments given in the document.
    self.plain_listing = ''
    for current_line_number, line in enumerate(file):
        current_line_number += 1
        if (current_line_number >= first_line_number) and \
           (current_line_number <= last_line_number):
            # Remove single-line "listings" comments. Only
            # comments started by "/*@" and ended by "@*/" are
            # supported.
            line = re.sub(r'/\*@[^@]*@\*/', '', line)

            # Add the just-read line to the listing.
            if hasattr(file, 'read'):
                self.plain_listing += line
            else:
                self.plain_listing += '\n' + line

    # Create a syntax highlighted XHTML version of the file using Pygments
    if pygments is not None:
        from pygments import lexers, formatters
        try:
            lexer = lexers.get_lexer_by_name(
                self.ownerDocument.context.current_language.lower())
        except Exception as msg:
            lexer = lexers.TextLexer()
        self.xhtml_listing = pygments.highlight(
            self.plain_listing, lexer,
            formatters.HtmlFormatter(linenos=linenos))
예제 #11
0
def json_to_html(obj):
    formatted_json = json.dumps(obj,
                                sort_keys=True,
                                indent=4,
                                ensure_ascii=False)
    colorful_json = highlight(formatted_json, lexers.JsonLexer(),
                              formatters.HtmlFormatter())
    return colorful_json
예제 #12
0
def home(request):
    codes = Code.objects.all()
    for c in codes:
        lex = lexers.get_lexer_by_name(c.language.split()[0])
        formatter = formatters.HtmlFormatter(noclasses=True)
        c.h_code = highlight(c.code, lex, formatter)

    return render(request, 'home.html', {'codes': codes})
예제 #13
0
 def highlight(self):
     """
     Returns this Snippet's originally-input code, highlighted via
     Pygments.
     
     """
     return highlight(self.code, self.language.get_lexer(),
                      formatters.HtmlFormatter(linenos=True))
예제 #14
0
 def highlight(self):
     """Returns highlighted code from pygments.highlight
        It takes three arguments 
         1)Code to highlight
         2)Lexer to use
         3)Formatter to generate output
     """
     return highlight(self.code, self.language.get_lexer(),
                      formatters.HtmlFormatter(lineos=True))
예제 #15
0
 def render(self):
     return self.renderer.env.from_string(TEMPLATE).render(
         route_name=self.route_name,
         controller=self.controller,
         template=self.template,
         usage=self.usage,
         config=pygments.highlight(pprint.pformat(self.application.config),
                                   lexers.PythonLexer(),
                                   formatters.HtmlFormatter()))
예제 #16
0
def _pygmentize(paste, lang):
    """
    Guess (or force if lang is given) highlight on a given paste via pygments
    """

    util.log.debug("{0} in {1} language".format(
        paste,
        lang,
    ))
    if lang:
        try:
            lexer = lexers.get_lexer_by_name(lang)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_by_name('text')
    else:
        try:
            util.log.debug(paste.lexer)
            lexer = lexers.get_lexer_by_name(paste.lexer)
            util.log.debug(lexer)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_for_mimetype(paste.mimetype)
    util.log.debug('Lexer is {0}'.format(lexer, ))
    if paste.ip:
        ip = IPy.IP(int(paste.ip, 2))
        util.log.debug('Originally pasted from {0}'.format(ip, ))
    if paste.filename:
        title = '{0}, created on {1}'.format(
            paste.filename,
            paste.created,
        )
    else:
        title = 'created on {0}'.format(paste.created, )
    title = '{0} {1}'.format(
        paste.mimetype,
        title,
    )
    util.log.debug(lexer)
    content = pygments.highlight(
        paste.content, lexer,
        formatters.HtmlFormatter(
            linenos='table',
            encoding='utf-8',
            lineanchors='ln',
            anchorlinenos=True,
        ))
    _add_header_metadata(paste)
    return bottle.template(
        'pygmentize.html',
        pygmentized=content,
        title=title,
        version=pasttle.__version__,
        current_year=CURRENT_YEAR,
        url=get_url(),
        id=paste.id,
        parent=paste.parent or u'',
        pygments_style=util.conf.get(util.cfg_section, 'pygments_style'),
    )
예제 #17
0
def get_all_highlight_css():
    """Yields pairs of (<name>, <css-string>) for every Pygment HTML style."""
    for name in styles.get_all_styles():
        cssclass = 'highlight__' + name
        formatter = formatters.HtmlFormatter(style=name, cssclass=cssclass)
        css = formatter.get_style_defs()
        css = (u'/* Pygment\'s %s style. */\n' % name) + css

        yield (name, cssclass, css)
예제 #18
0
 def highlight(data, language=None, default='python'):
     """Simple wrapper around pygments"""
     try:
         lexer = get_lexer_by_name(language, stripall=True, encoding='UTF-8')
     except ValueError:
         lexer = get_lexer_by_name(default, stripall=True, encoding='UTF-8')
     
     formatter = formatters.HtmlFormatter()
     return syntax_highlight(data, lexer, formatter)
예제 #19
0
def highlight_blob_css():
    '''get highlight css style'''

    formatter = formatters.HtmlFormatter(linenos=True,
                                         encoding='utf-8',
                                         style='friendly',
                                         noclasses="True")

    return formatter.get_style_defs()
예제 #20
0
def report(template, **kwargs):
    """Non-magic version of the report.

    This function can be used as

        report(template, source=submission_source.source, results=results)

    The keyword arguments are forwarded to the invocation of
    `template.render()`.  If `source` keyword argument is present, a
    syntax-highlighted HTML copy of it is additionally passed with
    `formatted_source` keyword argument.

    Args:
      template - A template earlier defined with %%template magic,
                 or an inline test. In case of an inline test, the template
                 is automatically defined to include the source code (if provided)
                 and the error message from the inline test result.

    Returns:
      A displayable HTML object.
    """
    if 'source' in kwargs:
        kwargs['formatted_source'] = pygments.highlight(
            kwargs['source'],
            lexers.PythonLexer(),  # pylint: disable=E1101
            formatters.HtmlFormatter()).rstrip()  # pylint: disable=E1101
    # Render the template giving the specified variable as 'results',
    # and render the result as inlined HTML in cell output. 'source' is
    # the prerendered source code.
    if isinstance(template, jinja2.Template):
        html = template.render(**kwargs)
    elif isinstance(template,
                    types.SimpleNamespace) and template.type == 'inlinetest':
        source_template = """
<h4 style='color: #387;'>Your submission</h4>
<pre style='background: #F0F0F0; padding: 3pt; margin: 4pt; border: 1pt solid #DDD; border-radius: 3pt;'>{{ formatted_source }}</pre>"""
        result_template = """
<h4 style='color: #387;'>Results</h4>
{% if 'passed' in results and results['passed'] %}
Looks OK.
{% elif 'error' in results %}
{{results['error'] | e}}
{% else %}
Something is wrong.
{% endif %}"""
        if 'formatted_source' in kwargs:
            template_source = source_template
        else:
            template_source = ''
        template_source += result_template
        actual_template = jinja2.Template(template_source)
        html = actual_template.render(**kwargs)
    else:
        raise Exception("Unrecognized template argument of class %s" %
                        (test_case.__class__))
    return display.HTML(html)
예제 #21
0
 def render_pygments_code(self):
     """Render a piece of code into HTML."""
     code = self.fetch_gist()
     lexer = self.pygments_lexer()
     formatter = formatters.HtmlFormatter(
         linenos=(self.data.get('pygments_linenos') and 'table') or False,
         noclasses=self.data.get('pygments_inline_css') or False,
         style=self.data.get('pygments_style') or 'colorful',
     )
     return highlight(code, lexer, formatter)
예제 #22
0
def highlight(language, text):
    if pygments is None:
        return text

    try:
        lexer = lexers.get_lexer_by_name(language)
    except pygments.util.ClassNotFound:
        lexer = lexers.guess_lexer(text)
    formatter = formatters.HtmlFormatter()
    return pygments.highlight(text, lexer, formatter)
예제 #23
0
def string_to_html(obj):
    formatted_json = obj
    try:
        js = json.loads(obj)
        formatted_json = json.dumps(js, sort_keys=True, indent=4, ensure_ascii=False)
    except Exception as ex:
        print(ex)
    finally:
        colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.HtmlFormatter())
        return colorful_json
예제 #24
0
 def format(code, _, wrapperBegin, wrapperEnd):
     """Generates markup for highlighting syntax in the given code"""
     if inspect.isfunction(code):
         sourceCode = inspect.getsource(code)
     else:
         sourceCode = inspect.getsource(type(code))
     formatter = formatters.HtmlFormatter(style='monokai')
     formattedCode = highlight(sourceCode, lexers.PythonLexer(), formatter)
     formattedCode = wrapperBegin + formattedCode + wrapperEnd
     return formattedCode
예제 #25
0
파일: oil_doc.py 프로젝트: asokoloski/oil
    def PrintHighlighted(self, out):
        from pygments import lexers
        from pygments import formatters
        from pygments import highlight

        lexer = lexers.get_lexer_by_name(self.lang)

        formatter = formatters.HtmlFormatter()
        code = self.s[self.start_pos:self.end_pos]
        highlighted = highlight(code, lexer, formatter)
        out.Print(highlighted)
예제 #26
0
파일: server.py 프로젝트: hkmshb/pasttle
def _pygmentize(paste, lang):
    """
    Guess (or force if lang is given) highlight on a given paste via pygments
    """

    util.log.debug("%s in %s language" % (
        paste,
        lang,
    ))
    if lang:
        try:
            lexer = lexers.get_lexer_by_name(lang)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_by_name('text')
    else:
        try:
            util.log.debug(paste.lexer)
            lexer = lexers.get_lexer_by_name(paste.lexer)
            util.log.debug(lexer)
        except lexers.ClassNotFound:
            lexer = lexers.get_lexer_for_mimetype(paste.mimetype)
    util.log.debug('Lexer is %s' % (lexer, ))
    if paste.ip:
        ip = IPy.IP(long(paste.ip, 2))
        util.log.debug('Originally pasted from %s' % (ip, ))
    if paste.filename:
        title = u'%s, created on %s' % (
            paste.filename,
            paste.created,
        )
    else:
        title = u'created on %s' % (paste.created, )
    title = '%s %s' % (
        paste.mimetype,
        title,
    )
    util.log.debug(lexer)
    content = pygments.highlight(
        paste.content, lexer,
        formatters.HtmlFormatter(
            linenos='table',
            encoding='utf-8',
            lineanchors='ln',
            anchorlinenos=True,
        ))
    return template(
        'pygmentize.html',
        pygmentized=content,
        title=title,
        version=pasttle.__version__,
        url=get_url(),
        id=paste.id,
        pygments_style=util.conf.get(util.cfg_section, 'pygments_style'),
    )
    def list_command(self, arg=""):
        """Show current function. If arg is given, show its source code."""
        display_current_line = self._debugger.curr_line

        code = self._debugger.get_source_for_func()

        source_lines = open(code["filename"]).read()

        css = f"""
        <style>
        {cssfile}
        </style>
        """

        lexer = lexers.get_lexer_by_name("python")
        formatter = formatters.HtmlFormatter(
            linenos=True,
            anchorlinenos=True,
            full=True,
            linespans=True,
            wrapcode=True,
        )
        coloured = highlight(source_lines, lexer=lexer, formatter=formatter)

        doc = html.fromstring(coloured)

        current_line_breakpoint = False

        # Highlight all breakpoints on the current file
        for bp in self._debugger.breakpoints:
            if self._debugger.curr_diff.file_name != bp.abs_filename:
                continue
            if bp.breakpoint_type != BPType.FUNC:
                elem = doc.get_element_by_id(f"True-{bp.lineno}", None)
                if bp.lineno == display_current_line:
                    current_line_breakpoint = True
                if elem is not None:
                    elem.set("class", "breakpoint")
                    if not bp.active:
                        elem.set("class", "inactive")

        elem = doc.get_element_by_id(f"True-{display_current_line}", None)
        if elem is not None:
            if not current_line_breakpoint and not self._autoplay._playing:
                elem.set("class", "currentline")
            elif self._autoplay._playing:
                elem.set("class", "currentline-running")
            else:
                elem.set("class", "hit")

        coloured = html.tostring(doc).decode("utf-8").strip()

        self._code_output.value = css + coloured
예제 #28
0
def highlighter(value):
    """
    highlighter a programming language
    """
    class_code = ''
    code = ''
    last_end = 0
    final_text = ''
    format_text = ''
    replace_items = {
        '&nbsp;': ' ',
        '<div>': '',
        '\r': '',
        '\n\n': '\n',
        '\t': '',
        '</div>': '',
        '</code>': ''
    }

    for inf in regex.finditer(value):
        class_code = re.split(r"&#39;|&quot;", inf.group(1))
        code = inf.group(2)

        for k, v in replace_items.items():
            code = code.replace(k, v)

        #Sometimes there are some characters before the first piece of code
        for char in code:
            if char.isnumeric() or char.isalpha():
                ind = code.find(char)
                code = code[ind:]
                break
        try:
            if class_code:
                class_code = class_code[1]
                lexer = lexers.get_lexer_by_name(class_code)
        except:
            try:
                lexer = lexers.guess_lexer(str(code))
            except ValueError:
                lexer = lexers.PythonLexer()

        format_text = pygments.highlight(code, lexer,
                                         formatters.HtmlFormatter())
        final_text = final_text + value[last_end:inf.start(0)] + format_text
        last_end = inf.end(2)

        value = value.replace('&lt;/code&gt;', '')
        final_text += value[last_end:]
    if final_text == '':
        return value

    return final_text
예제 #29
0
    def replace_func( match_object ):
      language = match_object.group( 1 )
      content = match_object.group( 2 )
      # We need to unescape the content first because it's already escaped, and
      # Pygments will escape it too, and we don't want it to be double-escaped.
      raw_content = self.html_parser.unescape( content )
      lexer = ( lexers.get_lexer_by_name( language ) if language else
                lexers.guess_lexer( raw_content ) )

      formatter = formatters.HtmlFormatter( nowrap=True )
      highlighted_code = pygments.highlight( raw_content, lexer, formatter )
      return '<code class="{0}">{1}</code>'.format( language,
                                                    highlighted_code.strip() )
예제 #30
0
    def PrintHighlighted(self, out):
        from pygments import lexers
        from pygments import formatters
        from pygments import highlight

        # unescape before passing to pygments, which will escape
        code = html.ToText(self.s, self.start_pos, self.end_pos)

        lexer = lexers.get_lexer_by_name(self.lang)
        formatter = formatters.HtmlFormatter()

        highlighted = highlight(code, lexer, formatter)
        out.Print(highlighted)