Example #1
0
    def _highlight_lines(self, tokensource):
        """
        Highlighted the lines specified in the `hl_lines` option by
        post-processing the token stream coming from `_format_lines`.
        """
        if not isinstance(self.hl_lines, dict):
            HtmlFormatter._highlight_lines(tokensource)
        else:
            hl_lines = {}
            for css, lines in self.hl_lines.items():
                hl_lines.update(dict([(line, css) for line in lines]))

            hls = hl_lines.keys()
            for i, (t, value) in enumerate(tokensource):
                if t != 1:
                    yield t, value
                if i + 1 in hls: # i + 1 because Python indexes start at 0
                    css = hl_lines[i + 1]
                    if css:
                        yield 1, '<span class="%s">%s</span>' % (css, value)
                    elif self.noclasses:
                        style = ''
                        if self.style.highlight_color is not None:
                            style = (' style="background-color: %s"' %
                                     (self.style.highlight_color,))
                        yield 1, '<span%s>%s</span>' % (style, value)
                    else:
                        yield 1, '<span class="hll">%s</span>' % value
                else:
                    yield 1, value
Example #2
0
    def process_request(self, req):
        style = req.args['style']
        try:
            style_cls = get_style_by_name(style)
        except ValueError as e:
            raise HTTPNotFound(e)

        parts = style_cls.__module__.split('.')
        filename = resource_filename('.'.join(parts[:-1]), parts[-1] + '.py')
        mtime = datetime.fromtimestamp(os.path.getmtime(filename), localtz)
        last_modified = http_date(mtime)
        if last_modified == req.get_header('If-Modified-Since'):
            req.send_response(304)
            req.end_headers()
            return

        formatter = HtmlFormatter(style=style_cls)
        content = u'\n\n'.join([
            formatter.get_style_defs('div.code pre'),
            formatter.get_style_defs('table.code td')
        ]).encode('utf-8')

        req.send_response(200)
        req.send_header('Content-Type', 'text/css; charset=utf-8')
        req.send_header('Last-Modified', last_modified)
        req.send_header('Content-Length', len(content))
        req.write(content)
Example #3
0
File: model.py Project: ADFD/adfd
 def _bbcodeAsHtml(self):
     style = get_style_by_name('igor')
     formatter = HtmlFormatter(style=style)
     lexer = get_lexer_by_name("bbcode", stripall=True)
     css = formatter.get_style_defs()
     txt = highlight(self._bbcode, lexer, HtmlFormatter())
     return "<style>%s</style>\n%s" % (css, txt)
Example #4
0
    def render_code(self):
        formatter = HtmlFormatter(style='default', nowrap=True, classprefix='code%s-' % self.pk)
        html = highlight(self.code, get_lexer_by_name(self.syntax), formatter)
        css = formatter.get_style_defs()

        # Included in a DIV, so the next item will be displayed below.
        return _('<div class="code"><style type="text/css">%(css)s</style>\n<pre>%(html)s</pre></div>\n') % {'css':css, 'html':html}
Example #5
0
 def prev_view_code(self, code, laugange):
     htmlFormatter = HtmlFormatter()
     lexer = CppLexer()
     if laugange == "Cpp":
         lexer = CppLexer()
     elif laugange == "CSharp":
         lexer = CSharpLexer()
     codeDiv = highlight(code, lexer, htmlFormatter)
     codeCss = htmlFormatter.get_style_defs(".highlight")
     #
     html = """
     <html>
         <head>
             <style type="text/css">
             %s
             </style>
         </head>
         <body>
         %s
         </body>
     </html>
     """ % (codeCss, codeDiv)
     self.webView.setHtml(html)
     self.setStyleSheet(codeCss)
     # 输出文件测试验证
     # ff = open('test.html', 'w')
     # ff.write(html)
     # ff.close()
     pass
Example #6
0
def highlight_paste(paste, hl_lines):
    '''Use pygments to syntax highlight a paste, returns by the way the CSS'''
    lexer = get_lexer_by_name(paste.hl_alias)
    formatter = HtmlFormatter(linenos=True, cssclass='source', hl_lines=hl_lines)
    return (
        highlight(paste.content, lexer, formatter),
        formatter.get_style_defs('.source')
    )
Example #7
0
 def style_defs(cls):
     """
     Return the CSS style definitions required
     by the formatted snippet.
     """
     formatter = HtmlFormatter()
     formatter.style.highlight_color = cls.VIOLATION_COLOR
     return formatter.get_style_defs()
Example #8
0
 def __init__(self, **options):
     HtmlFormatter.__init__(self, **options)
     self.hl_lines = {None: self.hl_lines}
     if isinstance(options.get('css_lines'), dict):
         for k, lines in options['css_lines'].items():
             self.hl_lines[k] = set()
             for lineno in lines:
                 try:
                     self.hl_lines[k].add(int(lineno))
                 except ValueError:
                     pass
Example #9
0
 def get(self, paste_id):
     try:
         aid = b64.num_decode(paste_id)
         paste = Paste.get_by_id(aid)
         content, lang = paste.content, paste.type
         formatter = HtmlFormatter()
         self.response.out.write(template.render("paste.html", {'css': formatter.get_style_defs('.highlight'),
                                                                'paste': highlight(content, get_lexer_by_name(lang), formatter)}))
     except Exception:
         self.response.set_status(404)
         self.response.out.write(template.render("404.html", {}))
    def getOutput(self):
        """
        Returns the output

        :rtype: str
        """
        if not self.formatted:
            lex = JavaLexer()
            formatter = HtmlFormatter(full=True)
            formatter.noclasses = True
            self._output = highlight(self._output, lex, formatter)
            self.formatted = True
        return self._output
def render_code(instance, style_name='default'):
    # Some interesting options in the HtmlFormatter:
    # - nowrap       -> no wrap inside <pre>
    # - classprefix  -> prefix for the classnames
    # - noclasses    -> all inline styles.
    #
    # To get_style_defs(), you can pass a selector prefix.
    #
    style = styles.get_style_by_name(style_name)
    formatter = HtmlFormatter(linenos=instance.linenumbers, style=style, nowrap=True, classprefix='code%s-' % instance.pk)
    html = highlight(instance.code, get_lexer_by_name(instance.language), formatter)
    css = formatter.get_style_defs()

    # Included in a DIV, so the next item will be displayed below.
    return '<div class="code"><style type="text/css">' + css + '</style>\n<pre>' + html + '</pre></div>\n'
    def __init__(self, parent, lexer=None):
        super(PygmentsHighlighter, self).__init__(parent)

        self._document = QtGui.QTextDocument()
        self._formatter = HtmlFormatter(nowrap=True)
        self._lexer = lexer if lexer else PythonLexer()
        self.set_style('default')
 def __init__(self, document, lexer=None):
     super(PygmentsSyntaxHighlighter, self).__init__(document)
     self._document = QtGui.QTextDocument()
     self._formatter = HtmlFormatter(nowrap=True)
     self._lexer = lexer if lexer else PythonLexer()
     self.__previousFilename = ""
     self.style = "default"
Example #14
0
    def __init__(self, parent, lexer=None):
        super(QPygmentsHighlighter, self).__init__(parent)

        self._document = QtGui.QTextDocument()
        self._formatter = HtmlFormatter(nowrap=True)
        self._lexer = lexer if lexer else PythonLexer()
        self.style = styles.getStyle("Default").pygmentsStyle
        self.enabled = True
Example #15
0
    def process_request(self, req):
        # settings panel
        if not 'style' in req.args:
            req._no_pygments_stylesheet = True
            styles = list(get_all_styles())
            styles.sort(lambda a, b: cmp(a.lower(), b.lower()))

            if req.method == 'POST':
                style = req.args.get('new_style')
                if style and style in styles:
                    req.session['pygments_style'] = style

            output = self._highlight('html', self.EXAMPLE, False)
            req.hdf['output'] = Markup(output)
            req.hdf['current'] = req.session.get('pygments_style',
                                                 self.default_style)
            req.hdf['styles'] = styles
            req.hdf['pygments_path'] = self.env.href.pygments()
            return 'pygments_settings.cs', None

        # provide stylesheet
        else:
            style = req.args['style']

            parts = style.__module__.split('.')
            filename = resource_filename('.'.join(parts[:-1]), parts[-1] + '.py')
            mtime = datetime.utcfromtimestamp(os.path.getmtime(filename))
            last_modified = http_date(time.mktime(mtime.timetuple()))
            if last_modified == req.get_header('If-Modified-Since'):
                req.send_response(304)
                req.end_headers()
                return

            formatter = HtmlFormatter(style=style)
            content = u'\n\n'.join([
                formatter.get_style_defs('div.code pre'),
                formatter.get_style_defs('table.code td')
            ]).encode('utf-8')

            req.send_response(200)
            req.send_header('Content-Type', 'text/css; charset=utf-8')
            req.send_header('Last-Modified', last_modified)
            req.send_header('Content-Length', len(content))
            req.write(content)
Example #16
0
def export():
    if not os.path.exists(PYGMENTS_PATH):
        os.makedirs(PYGMENTS_PATH)

    styles = list(get_all_styles())

    for style in styles:
        print("Generating CSS for %s" % style)

        opts = {"style": style}

        path = os.path.join(PYGMENTS_PATH, "%s.css" % style)
        formatter = HtmlFormatter(**opts)
        css_content = formatter.get_style_defs()
        # little fix because pelican doesn't append background color.
        css_content = css_content.replace(".hll", ".highlight")

        with open(path, "w") as f:
            f.write(css_content)
Example #17
0
def export():
    if not os.path.exists(PYGMENTS_PATH):
        os.makedirs(PYGMENTS_PATH)

    styles = list(get_all_styles())

    for style in styles:
        print('Generating CSS for %s' % style)

        opts = {
            'style': style,
            'noclasses': False,
            'nobackground': False,
        }

        path = os.path.join(PYGMENTS_PATH, '%s.css' % style)
        formatter = HtmlFormatter(**opts)
        css_content = formatter.get_style_defs('.highlight')

        with open(path, 'w') as f:
            f.write(css_content)
    def __init__(self, parent, lexer=None):
        super(PygmentsHighlighter, self).__init__(parent)

        self._document = self.document()
        self._formatter = HtmlFormatter(nowrap=True)
        self.set_style('default')
        if lexer is not None:
            self._lexer = lexer
        else:
            if PY3:
                self._lexer = Python3Lexer()
            else:
                self._lexer = PythonLexer()
Example #19
0
def trans(content, type, style, suffix):
    print(type, style, suffix)
    if not suffix:
        lexer = get_lexer_by_name(type)
    else:
        try:
            lexer = get_lexer_for_filename(suffix)
        except:
            lexer = get_lexer_by_name(type)
    # 指定风格
    formatter = HtmlFormatter(style=style)
    # 获取html
    html = highlight(content, lexer, formatter)
    return html
Example #20
0
    def save(self, *args, **kwargs):
        """
        利用pygments,创建高亮的html文本呈现code块,然后再保存
        """
        lexer = get_lexer_by_name(self.language)
        linenos = self.linenos and 'table' or False
        options = self.title and {'title': self.title} or {}
        formatter = HtmlFormatter(style=self.style,
                                  linenos=linenos,
                                  full=True,
                                  **options)
        self.highlighted = highlight(self.code, lexer, formatter)

        super(Snippet, self).save(*args, **kwargs)
Example #21
0
 def save(self, *args, **kwargs):
     lexer = get_lexer_by_name(self.language)
     # linenos = self.linenos and 'table' or False
     linenos = 'table' if self.linenos else False
     # options = self.title and {'title': self.title} or {}
     options = {'title': self.title} if self.title else {}
     formatter = HtmlFormatter(
         style=self.style,
         linenos=linenos,
         full=True,
         **options,
     )
     self.highlighted = highlight(self.code, lexer, formatter)
     super().save(*args, **kwargs)
Example #22
0
 def save(self, *args, **kwargs):
     """
     Use the `pygments` library to create a highlighted HTML
     representation of the code snippet.
     """
     lexer = get_lexer_by_name(self.language)
     linenos = 'table' if self.linenos else False
     options = {'title': self.title} if self.title else {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     super(Snippet, self).save(*args, **kwargs)
Example #23
0
def create_highlighted_text(text,
                            lexer_name='text',
                            style='friendly',
                            title=None):
    lexer = get_lexer_by_name(lexer_name)
    # linenos = 'table' if self.linenos else False
    linenos = False
    options = {'title': title} if title else {}
    formatter = HtmlFormatter(style=style,
                              linenos=linenos,
                              full=True,
                              **options)
    highlighted = highlight(text, lexer, formatter)
    return highlighted
Example #24
0
 def save(self, *args, **kwargs):
     """
     code snippet의 하이라이팅을 생성하기 위해
     pygments 라이브러리를 사용한다
     """
     lexer = get_lexer_by_name(self.language)
     linenos = 'table' if self.linenos else False
     options = {'title': self.title} if self.title else {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     super(Snippet, self).save(*args, **kwargs)
Example #25
0
    def diff_prettified(self):
        """Function to display pretty version of our data"""

        # Convert the data to sorted, indented JSON

        response = json.loads(self.difference)

        response = json.dumps(response, sort_keys=True, indent=1, ensure_ascii=False).replace('&oacute;', "ó")
        # response = codecs.encode(str(response)).decode('utf-8')
        # Truncate the data. Alter as needed
        response = response[:10000]

        # Get the Pygments formatter
        formatter = HtmlFormatter(style='colorful', lineseparator="<br>")

        # Highlight the data
        response = highlight(response, JsonLexer(), formatter)

        # Get the stylesheet
        style = "<style>" + formatter.get_style_defs() + "</style>"

        # Safe the output
        return mark_safe(style + response)
Example #26
0
 def save(self, *args, **kwargs):
     """
     Use the 'pygments' library to create a highlighted HTML
     representation of the code snippet
     """
     lexer = get_lexer_by_name(self.language)
     linenos = self.linenos and 'table' or False
     options = self.title and {'title': self.title} or {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     super(Blog, self).save(*args, **kwargs)
Example #27
0
    def html(self):
        """
        Return an HTML representation of the snippet.
        """
        formatter = HtmlFormatter(
            cssclass=self.DIV_CSS_CLASS,
            linenos=True,
            linenostart=self._start_line,
            hl_lines=self._shift_lines(self._violation_lines,
                                       self._start_line),
            lineanchors=self._src_filename,
        )

        return pygments.format(self.src_tokens(), formatter)
Example #28
0
def save(self, *args, **kwargs):
    """
    Use the `pygments` library to create a highlighted HTML
    representation of the code snippet.
    """
    lexer = get_lexer_by_name(self.name)
    boardingid = 'table' if self.boardingid else False
    options = {'title': self.name} if self.name else {}
    formatter = HtmlFormatter(style=self.name,
                              boardingid=boardingid,
                              full=True,
                              **options)
    self.highlighted = highlight(self.contact, lexer, formatter)
    super(passengers, self).save(*args, **kwargs)
Example #29
0
 def save(self, *args, **kwargs):
     """
     使用‘pygments’库去创建一个高亮的html表示的代码片段
     :return:
     """
     lexer = get_lexer_by_name(self.language)
     linenos = self.linenos and "table" or False
     options = self.title and {"title": self.title} or {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     super(Snippet, self).save(*args, **kwargs)
Example #30
0
    def blockcode(self, text, lang):
        if not lang:
            return '\n<pre><code>{}</code></pre>\n'.format(text.strip())

        try:
            lexer = get_lexer_by_name(lang, stripall=True)
        except ClassNotFound:
            lexer = None

        if lexer:
            formatter = HtmlFormatter(linenos='inline')
            return highlight(text, lexer, formatter)
        # default
        return '\n<pre><code>{}</code></pre>\n'.format(text.strip())
Example #31
0
def prepare_style():
    """ read and process style/ directory """
    config = Config()
    # copy static files
    if not os.path.exists(os.path.join(config.outputdir, 'style')):
        os.makedirs(os.path.join(config.outputdir, 'style'))

    # copy supplementary files to ouput dir
    for filename in os.listdir(config.styledir):
        if os.path.splitext(filename)[1].lower() != '.css':
            shutil.copy(
                os.path.join(config.styledir, filename),
                os.path.join(config.outputdir, 'style')
            )

    # generate syntax highlight css and append all other CSS files
    allcss = HtmlFormatter().get_style_defs('.codehilite')
    for cssfile in glob.iglob(os.path.join(config.styledir, '*.css')):
        allcss = allcss + codecs.open(cssfile, 'r', 'utf-8').read()
    allcss = allcss.replace('{{styleurl}}', "{}style/".format(config.blogurl))

    # minimise css
    return cssmin(allcss, wrap=1000)
Example #32
0
def upload_file_remote(dest_file: Path, ext: str, file_path: Path, zip_flag,
                       plain, in_file, ppb_path_on_host, ppb_target_host,
                       metadata: dict) -> None:
    """
    dest_file: destination filename + extension (example: 77a80349869f.png)
    ext: file extension, like dest_file.suffix
    file_path: path to input file.
    """
    with tempfile.TemporaryDirectory() as tmpdirname:
        send_metadata(dest_file, metadata, ppb_path_on_host, ppb_target_host,
                      tmpdirname)
        tempfile_path = os.path.join(tmpdirname, in_file)
        if zip_flag:

            zip_command = f'zip -r {tempfile_path} {file_path}'
            log.info(zip_command)
            os.system(zip_command)
            file_path = tempfile_path

        elif not plain and not check_extension(
                ext, {
                    *IMAGE_EXTENSIONS, *VIDEO_EXTENSIONS,
                    *COMPRESSION_EXTENSIONS, '', 'pdf'
                }):
            with open(file_path, mode='r') as c:
                code = c.read()
            with open(tempfile_path, mode='w') as f:
                if ext == '.log':
                    lexer = BashLexer()
                else:
                    try:
                        lexer = guess_lexer_for_filename(file_path, c)
                    except Exception as e:
                        print(e)
                try:
                    highlight(code,
                              lexer,
                              HtmlFormatter(linenos=True, full=True),
                              outfile=f)
                except:
                    # if pygments fails to find a lexer, continue without highlighting
                    tempfile_path = file_path
                    lexer = None
            file_path = tempfile_path
            log.info(f'Highlighted {file_path} with lexer {lexer}.')

        rsync_command = f'rsync -avP {file_path} {ppb_target_host}:{ppb_path_on_host}/{dest_file}'
        log.info(rsync_command)
        os.system(rsync_command)
Example #33
0
 def html(self):
     # TODO: section tags for each section and subsection
     out = HTML()
     out.start_tag("article")
     for block in self.blocks:
         if block.content_type is None:
             out.element("p", html=Text(" ".join(block.lines)).html)
         elif block.content_type in (Heading, HorizontalRule):
             for line in block.lines:
                 out.write_html(line.html)
         elif block.content_type is Literal:
             source = "".join(line.line for line in block.lines)
             lang, _, metadata = block.metadata.partition(" ")
             try:
                 lexer = get_lexer_by_name(lang)
             except ClassNotFound:
                 lexer = None
             if lexer is None:
                 out.start_tag("pre")
                 out.write_text(source)
                 out.end_tag("pre")
             else:
                 out.write_raw(highlight(source, lexer, HtmlFormatter()))
         elif block.content_type is Quote:
             out.start_tag("blockquote")
             for line in block.lines:
                 out.write_html(line.html)
             out.end_tag("blockquote")
         elif block.content_type is ListItem:
             level = 0
             for line in block.lines:
                 while level > line.level:
                     out.end_tag()
                     level -= 1
                 while level < line.level:
                     out.start_tag(line.list_tag(level))
                     level += 1
                 out.write_html(line.html)
             while level:
                 out.end_tag()
                 level -= 1
         elif block.content_type is TableRow:
             out.start_tag("table")
             for line in block.lines:
                 out.write_html(line.html)
             out.end_tag("table")
     out.element("footer")
     out.end_tag("article")
     return out.html
def export():
    if not os.path.exists(PYGMENTS_PATH):
        os.makedirs(PYGMENTS_PATH)

    styles = list(get_all_styles())

    for style in styles:
        print("Generating CSS for %s" % style)

        opts = {
            "style": style,
            "noclasses": False,
            "nobackground": False,
        }

        path = os.path.join(PYGMENTS_PATH, "%s.css" % style)
        formatter = HtmlFormatter(**opts)
        md_css = formatter.get_style_defs(".highlight")
        rst_css = formatter.get_style_defs(".literal-block")

        with open(path, "w+") as f:
            f.write(md_css)
            f.write("\n")
            f.write(rst_css)
Example #35
0
def pygmentize(code, linenos=False, tofile=False, outputfile=''):
    style = 'colorful'
    defstyles = 'overflow:auto;width:auto;'

    formatter = HtmlFormatter(style=style,
                              linenos=False,
                              noclasses=True,
                              cssclass='',
                              cssstyles=defstyles + get_default_style(),
                              prestyles='margin: 0')
    html = highlight(code, JavaLexer(), formatter)
    if linenos:
        html = insert_line_numbers(html)
    html = "<!-- Rapport généré avec TPGen -->" + html
    return html
Example #36
0
def get_classes_by_start(code):
    """Returns a dictionary mapping positions in the code to html equivalent to what Pygments would
    insert prior to that position."""
    lexer = Python3Lexer()
    formatter = HtmlFormatter()

    # pygments seems to ignore initial newlines (etc.?), so to line up with all our other stuff,
    # we need to shift the starting position by that much
    start_position = len(code) - len(code.lstrip())
    parser = MyHTMLParser(start_position=start_position)

    tokens = lexer.get_tokens(code.lstrip())
    pygments_html = io.StringIO()
    formatter.format(tokens, pygments_html)
    pygments_html.seek(0)
    html = pygments_html.read()
    parser.feed(html)

    # not really sure why these are different by 1, but let's make sure we find out about it if
    # that changes
    assert parser.current_position == len(
        code) + 1, 'code is length {}, parser ended up at {}'.format(
            len(code), parser.current_position)
    return parser.start_classes
Example #37
0
    def handle_output(self, process: QProcess):
        output: QByteArray = process.readAllStandardOutput()
        message = output.data().decode('utf-8').strip()
        if message.startswith('{') or message.startswith('['):
            formatter = HtmlFormatter()
            formatter.noclasses = True
            formatter.linenos = False
            formatter.nobackground = True
            message = highlight(message, JsonLexer(), formatter)
            self.output.insertHtml(message)
        else:
            self.output.append(message)

        # This is just for generating the command lists in constants
        # commands = None
        # if '== Blockchain ==' in message:
        #     commands = self.parse_litecoin_cli_commands(message)
        # elif 'lncli [global options] command [command options]' in message:
        #     commands = self.parse_lncli_commands(message)
        # if commands is not None:
        #     log.debug('commands', commands=commands)

        max_scroll = self.output.verticalScrollBar().maximum()
        self.output.verticalScrollBar().setValue(max_scroll)
Example #38
0
def format_code_for_display(concept_key, lang):
    """
    Returns the formatted HTML formatted syntax-highlighted text for a concept key (from a meta language file) and a language
    :param concept_key: name of the key to format
    :param lang: language to format it (in meta language/syntax highlighter format)
    :return: string with code with applied HTML formatting
    """
    if lang.concept_unknown(
            concept_key) or lang.concept_code(concept_key) is None:
        return "Unknown"
    if lang.concept_implemented(concept_key):
        return highlight(lang.concept_code(concept_key),
                         get_lexer_by_name(lang.key, startinline=True),
                         HtmlFormatter())
    return None
Example #39
0
 def __init__(self, query, highlightFormat: str = "html"):
     '''
     construct me for the given query and highlightFormat
     
     Args:
         query(Query): the query to do the syntax highlighting for
         highlightFormat(str): the highlight format to be used
     '''
     self.query = query
     self.highlightFormat = highlightFormat
     self.lexer = get_lexer_by_name(self.query.lang)
     if self.highlightFormat == "html":
         self.formatter = HtmlFormatter()
     elif self.highlightFormat == "latex":
         self.formatter = LatexFormatter()
Example #40
0
def export():
    if not os.path.exists(PYGMENTS_PATH):
        os.makedirs(PYGMENTS_PATH)

    styles = list(get_all_styles())

    for style in styles:
        print('Generating CSS for %s' % style)

        opts = {
            'style': style,
            'noclasses': False,
            'nobackground': False,
        }

        path = os.path.join(PYGMENTS_PATH, '%s.css' % style)
        formatter = HtmlFormatter(**opts)
        md_css = formatter.get_style_defs('.highlight')
        rst_css = formatter.get_style_defs('.literal-block')

        with open(path, 'w+') as f:
            f.write(md_css)
            f.write('\n')
            f.write(rst_css)
Example #41
0
 def save(self, *args, **kwargs):
     """使用`pygments`库创建一个高亮显示的HTML表示代码段。"""
     lexer = get_lexer_by_name(self.language)
     """
     linenos = (True and 'table') or False => or 前的语句为True,所以不执行 or 后的语句 => linenos = 'table' 
     linenos = (False and 'table') or False => or 前的语句为False,所以执行 or 后的语句 => linenos = False
     """
     linenos = self.linenos and 'table' or False
     options = self.title and {'title': self.title} or {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     super().save(*args, **kwargs)
Example #42
0
File: utils.py Project: FoxDev/pste
def syntax_highlight(file):
    code = file.path.read_text()
    try:
        lexer = get_lexer_for_mimetype(file.client_mimetype)
    except ClassNotFound:
        try:
            lexer = guess_lexer(code)
        except ClassNotFound:
            lexer = TextLexer()

    return highlight(
        code,
        lexer,
        HtmlFormatter(linenos="table", anchorlinenos=True, lineanchors="line"),
    )
Example #43
0
 def save(self, *args, **kwargs):
     """
     `pygments` 라이브러리를 사용하여 하이라이트된 코드를 만든다.
     """
     lexer = get_lexer_by_name(self.language)
     linenos = self.linenos and 'table' or False
     options = self.title and {'title': self.title} or {}
     formatter = HtmlFormatter(
         style=self.style,
         linenos=linenos,
         full=True,
         **options,
     )
     self.highlighted = highlight(self.code, lexer, formatter)
     super().save(*args, **kwargs)
Example #44
0
 def highlightCode(self, code, language):
     #converting the html to platin
     td = QTextDocument()
     td.setHtml(code)
     print("the plain text is here : " + td.toPlainText())
     codeLexer = get_lexer_by_name(language)
     f = open("highlightTest.html", 'wb')
     #fi = open("highlightTest.png",'wb')
     #style = get_style_by_name("native")
     formatter = HtmlFormatter(full=True, noclasses=True, encoding="UTF-8")
     #imgFormater = ImageFormatter()
     result = highlight(td.toPlainText(), codeLexer, formatter)
     td.setHtml(result.decode("UTF-8"))
     print(td.toHtml())
     return td.toHtml()
Example #45
0
    def format(self, extension=None):
        """Write pretty HTML logs."""
        M = self.M
        # pygments lexing setup:
        # (pygments HTML-formatter handles HTML-escaping)
        import pygments
        from pygments.lexers.text import IrcLogsLexer
        from pygments.formatters.html import HtmlFormatter
        import pygments.token as token
        from pygments.lexer import bygroups
        # Don't do any encoding in this function with pygments.
        # That's only right before the i/o functions in the Config
        # object.
        formatter = HtmlFormatter(lineanchors='l',
                                  full=True, style=M.config.pygmentizeStyle,
                                  outencoding=self.M.config.output_codec)
        Lexer = IrcLogsLexer
        Lexer.tokens['msg'][1:1] = [
            # match:   #topic commands
            (r"(\#topic[ \t\f\v]*)(.*\n)",
             bygroups(token.Keyword, token.Generic.Heading), '#pop'),
            # match:   #command   (others)
            (r"(\#[^\s]+[ \t\f\v]*)(.*\n)",
             bygroups(token.Keyword, token.Generic.Strong), '#pop'),
        ]
        lexer = Lexer()

        out = pygments.highlight("\n".join(M.lines), lexer, formatter)
        # Hack it to add "pre { white-space: pre-wrap; }", which make
        # it wrap the pygments html logs.  I think that in a newer
        # version of pygmetns, the "prestyles" HTMLFormatter option
        # would do this, but I want to maintain compatibility with
        # lenny.  Thus, I do these substitution hacks to add the
        # format in.  Thanks to a comment on the blog of Francis
        # Giannaros (http://francis.giannaros.org) for the suggestion
        # and instructions for how.
        out, n = re.subn(
            r"(\n\s*pre\s*\{[^}]+;\s*)(\})",
            r"\1\n      white-space: pre-wrap;\2",
            out, count=1
        )
        if n == 0:
            out = re.sub(
                r"(\n\s*</style>)",
                r"\npre { white-space: pre-wrap; }\1",
                out, count=1
            )
        return out
Example #46
0
    def save(self, *args, **kwargs):
        lexer = get_lexer_by_name(self.language)
        linenos = 'table' if self.linenos else False
        options = {'title': self.title} if self.title else {}
        formatter = HtmlFormatter(
            style=self.style,
            linenos=linenos,
            full=True,
            **options,  # 그냥 딕셔너리 이름.
        )
        self.highlighted = highlight(self.code, lexer, formatter)
        super().save(*args, **kwargs)

        # admin에서 저장할 때도 이 메서드가 호출이 됨.
        # admin 페이지에서 'Save and continue editing'하면 아래 Highlighted
        # 에 저장됨.
Example #47
0
    def save(self, *args, **kwargs):
        lexer = get_lexer_by_name(self.language)
        display_linenos = self.display_linenos and 'table' or False
        options = self.title and {'title': self.title} or {}
        formatter = HtmlFormatter(style=self.style,
                                  linenos=display_linenos,
                                  full=True,
                                  **options)
        self.highlighted = highlight(self.code, lexer, formatter)

        if not self.code.endswith('\r\n'):
            self.code += u'\r\n'

        self.linenos = self.code.count('\r\n')

        super(Snippet, self).save(*args, **kwargs)
Example #48
0
 def save(self, *args, **kwargs):
     """
     使用 pygments 来创建高亮的 HTML 代码。
     :param args: 
     :param kwargs: 
     :return: 
     """
     lexer = get_lexer_by_name(self.language)
     linenos = self.linenos and 'table' or False
     options = self.title and {'title': self.title} or {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     super(Snippet, self).save(*args, **kwargs)
Example #49
0
 def save(self, *args, **kwargs):
     '''
     使用 pygments 創建高亮的HTML文本
     '''
     lexer = get_lexer_by_name(self.language)
     linenos = self.linenos and 'table' or False
     options = self.title and {'title': self.title} or {}
     formatter = HtmlFormatter(style=self.style,
                               linenos=linenos,
                               full=True,
                               **options)
     self.highlighted = highlight(self.code, lexer, formatter)
     '''
     呼叫父類別內的 save() 存檔
     '''
     super().save(*args, **kwargs)
Example #50
0
 def __init__(self, parent, lexer=None):
     super(Highlighter, self).__init__(parent)
     self._document = self.document()
     self._formatter = HtmlFormatter()
     self._lexer = lexer if lexer else PythonLexer()
     self.set_style('default')
class PygmentsHighlighter(QtGui.QSyntaxHighlighter):
    """ Syntax highlighter that uses Pygments for parsing. """

    #---------------------------------------------------------------------------
    # 'QSyntaxHighlighter' interface
    #---------------------------------------------------------------------------

    def __init__(self, parent, lexer=None):
        super(PygmentsHighlighter, self).__init__(parent)

        self._document = QtGui.QTextDocument()
        self._formatter = HtmlFormatter(nowrap=True)
        self._lexer = lexer if lexer else PythonLexer()
        self.set_style('default')

    def highlightBlock(self, string):
        """ Highlight a block of text.
        """
        prev_data = self.currentBlock().previous().userData()
        if prev_data is not None:
            self._lexer._saved_state_stack = prev_data.syntax_stack
        elif hasattr(self._lexer, '_saved_state_stack'):
            del self._lexer._saved_state_stack

        # Lex the text using Pygments
        index = 0
        for token, text in self._lexer.get_tokens(string):
            length = len(text)
            self.setFormat(index, length, self._get_format(token))
            index += length

        if hasattr(self._lexer, '_saved_state_stack'):
            data = PygmentsBlockUserData(
                syntax_stack=self._lexer._saved_state_stack)
            self.currentBlock().setUserData(data)
            # Clean up for the next go-round.
            del self._lexer._saved_state_stack

    #---------------------------------------------------------------------------
    # 'PygmentsHighlighter' interface
    #---------------------------------------------------------------------------

    def set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if isinstance(style, str):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches()

    def set_style_sheet(self, stylesheet):
        """ Sets a CSS stylesheet. The classes in the stylesheet should
        correspond to those generated by:

            pygmentize -S <style> -f html

        Note that 'set_style' and 'set_style_sheet' completely override each
        other, i.e. they cannot be used in conjunction.
        """
        self._document.setDefaultStyleSheet(stylesheet)
        self._style = None
        self._clear_caches()

    #---------------------------------------------------------------------------
    # Protected interface
    #---------------------------------------------------------------------------

    def _clear_caches(self):
        """ Clear caches for brushes and formats.
        """
        self._brushes = {}
        self._formats = {}

    def _get_format(self, token):
        """ Returns a QTextCharFormat for token or None.
        """
        if token in self._formats:
            return self._formats[token]

        if self._style is None:
            result = self._get_format_from_document(token, self._document)
        else:
            result = self._get_format_from_style(token, self._style)

        self._formats[token] = result
        return result

    def _get_format_from_document(self, token, document):
        """ Returns a QTextCharFormat for token by 
        """
        code, html = next(self._formatter._format_lines([(token, 'dummy')]))
        self._document.setHtml(html)
        return QtGui.QTextCursor(self._document).charFormat()

    def _get_format_from_style(self, token, style):
        """ Returns a QTextCharFormat for token by reading a Pygments style.
        """
        result = QtGui.QTextCharFormat()
        for key, value in list(style.style_for_token(token).items()):
            if value:
                if key == 'color':
                    result.setForeground(self._get_brush(value))
                elif key == 'bgcolor':
                    result.setBackground(self._get_brush(value))
                elif key == 'bold':
                    result.setFontWeight(QtGui.QFont.Bold)
                elif key == 'italic':
                    result.setFontItalic(True)
                elif key == 'underline':
                    result.setUnderlineStyle(
                        QtGui.QTextCharFormat.SingleUnderline)
                elif key == 'sans':
                    result.setFontStyleHint(QtGui.QFont.SansSerif)
                elif key == 'roman':
                    result.setFontStyleHint(QtGui.QFont.Times)
                elif key == 'mono':
                    result.setFontStyleHint(QtGui.QFont.TypeWriter)
        return result

    def _get_brush(self, color):
        """ Returns a brush for the color.
        """
        result = self._brushes.get(color)
        if result is None:
            qcolor = self._get_color(color)
            result = QtGui.QBrush(qcolor)
            self._brushes[color] = result
        return result

    def _get_color(self, color):
        """ Returns a QColor built from a Pygments color string.
        """
        qcolor = QtGui.QColor()
        qcolor.setRgb(int(color[:2], base=16),
                      int(color[2:4], base=16),
                      int(color[4:6], base=16))
        return qcolor
Example #52
0
class QPygmentsHighlighter(QSyntaxHighlighter):
    """ Syntax highlighter that uses Pygments for parsing. """

    hilighlightingBlock = Signal(unicode, QSyntaxHighlighter)

    #---------------------------------------------------------------------------
    # 'QSyntaxHighlighter' interface
    #---------------------------------------------------------------------------

    def __init__(self, parent, lexer=None):
        super(QPygmentsHighlighter, self).__init__(parent)

        self._document = QtGui.QTextDocument()
        self._formatter = HtmlFormatter(nowrap=True)
        self._lexer = lexer if lexer else PythonLexer()
        self.style = styles.getStyle("Default").pygmentsStyle
        self.enabled = True

    def setLexerFromFilename(self, filename):
        """
        Change the lexer based on the filename (actually only the extension is needed)

        :param filename: Filename or extension
        """
        try:
            self._lexer = get_lexer_for_filename(filename)
        except ClassNotFound:
            self._lexer = PythonLexer()

    def highlightBlock(self, text):
        """ Highlight a block of text """
        if self.enabled is False:
            return
        text = unicode(text)
        original_text = text
        prev_data = self.currentBlock().previous().userData()

        if prev_data is not None:
            self._lexer._saved_state_stack = prev_data.syntax_stack
        elif hasattr(self._lexer, '_saved_state_stack'):
            del self._lexer._saved_state_stack

        # Lex the text using Pygments
        index = 0
        for token, text in self._lexer.get_tokens(text):
            length = len(text)
            self.setFormat(index, length, self._get_format(token))
            index += length

        if hasattr(self._lexer, '_saved_state_stack'):
            data = PygmentsBlockUserData(
                syntax_stack=self._lexer._saved_state_stack)
            self.currentBlock().setUserData(data)
            # Clean up for the next go-round.
            del self._lexer._saved_state_stack

        #Spaces
        expression = QRegExp('\s+')
        index = expression.indexIn(original_text, 0)
        while index >= 0:
            index = expression.pos(0)
            length = len(expression.cap(0))
            self.setFormat(index, length, self._get_format(Whitespace))
            index = expression.indexIn(original_text, index + length)

        self.hilighlightingBlock.emit(original_text, self)

        # expression = QRegExp('\s+')
        # index = expression.indexIn(original_text, 0)
        # while index >= 0:
        #     index = expression.pos(0)
        #     length = len(expression.cap(0))
        #     self.setFormat(index, length, self._get_format(Whitespace))
        #     index = expression.indexIn(original_text, index + length)

    #---------------------------------------------------------------------------
    # 'PygmentsHighlighter' interface
    #---------------------------------------------------------------------------
    def __set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if (isinstance(style, str) or
                isinstance(style, unicode)):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches()

    def set_style_sheet(self, stylesheet):
        """ Sets a CSS stylesheet. The classes in the stylesheet should
        correspond to those generated by:

            pygmentize -S <style> -f html

        Note that 'set_style' and 'set_style_sheet' completely override each
        other, i.e. they cannot be used in conjunction.
        """
        self._document.setDefaultStyleSheet(stylesheet)
        self._style = None
        self._clear_caches()

    def __get_style(self):
        return self._style

    #: gets/sets the **pygments** style.
    style = property(__get_style, __set_style)

    #---------------------------------------------------------------------------
    # Protected interface
    #---------------------------------------------------------------------------

    def _clear_caches(self):
        """ Clear caches for brushes and formats.
        """
        self._brushes = {}
        self._formats = {}

    def _get_format(self, token):
        """ Returns a QTextCharFormat for token or None.
        """
        if token in self._formats:
            return self._formats[token]

        if self._style is None:
            result = self._get_format_from_document(token, self._document)
        else:
            result = self._get_format_from_style(token, self._style)

        self._formats[token] = result
        return result

    def _get_format_from_document(self, token, document):
        """ Returns a QTextCharFormat for token by
        """
        code, html = next(self._formatter._format_lines([(token, 'dummy')]))
        self._document.setHtml(html)
        return QtGui.QTextCursor(self._document).charFormat()

    def _get_format_from_style(self, token, style):
        """ Returns a QTextCharFormat for token by reading a Pygments style.
        """
        result = QtGui.QTextCharFormat()
        for key, value in list(style.style_for_token(token).items()):
            if value:
                if key == 'color':
                    result.setForeground(self._get_brush(value))
                elif key == 'bgcolor':
                    result.setBackground(self._get_brush(value))
                elif key == 'bold':
                    result.setFontWeight(QtGui.QFont.Bold)
                elif key == 'italic':
                    result.setFontItalic(True)
                elif key == 'underline':
                    result.setUnderlineStyle(
                        QtGui.QTextCharFormat.SingleUnderline)
                elif key == 'sans':
                    result.setFontStyleHint(QtGui.QFont.SansSerif)
                elif key == 'roman':
                    result.setFontStyleHint(QtGui.QFont.Times)
                elif key == 'mono':
                    result.setFontStyleHint(QtGui.QFont.TypeWriter)
        return result

    def _get_brush(self, color):
        """ Returns a brush for the color.
        """
        result = self._brushes.get(color)
        if result is None:
            qcolor = self._get_color(color)
            result = QtGui.QBrush(qcolor)
            self._brushes[color] = result
        return result

    def _get_color(self, color):
        """ Returns a QColor built from a Pygments color string.
        """
        color = unicode(color).replace("#", "")
        qcolor = QtGui.QColor()
        qcolor.setRgb(int(color[:2], base=16),
                      int(color[2:4], base=16),
                      int(color[4:6], base=16))
        return qcolor
 def __init__(self, **options):
     HtmlFormatter.__init__(self, **options)
class PygmentsSyntaxHighlighter(SyntaxHighlighter):
    """
    This mode enable syntax highlighting using the pygments library

    Here the properties added by the mode to
    :attr:`pyqode.core.QCodeEdit.style`:

    ====================== ====================== ======= ====================== =====================
    Key                    Section                Type    Default value          Description
    ====================== ====================== ======= ====================== =====================
    pygmentsStyle          General                QColor  Computed.              Background color for matching symbols
    ====================== ====================== ======= ====================== =====================

    .. warning:: There are some issues with multi-line comments, they are not
                 properly highlighted until a full re-highlight is triggered.
                 The text is automatically re-highlighted on save.
    """
    #: Mode description
    DESCRIPTION = "Apply syntax highlighting to the editor using pygments"

    #: Associates a fold detector to a specific pygments lexer.
    try:
        LEXERS_FOLD_DETECTORS = {
            # indent based
            PythonLexer: IndentBasedFoldDetector(),
            CythonLexer: IndentBasedFoldDetector(),
            BashLexer: IndentBasedFoldDetector(),
            BatchLexer: IndentBasedFoldDetector(),
            XmlLexer: IndentBasedFoldDetector(),
            HtmlLexer: IndentBasedFoldDetector(),
            JsonLexer: IndentBasedFoldDetector(),
            BooLexer: IndentBasedFoldDetector(),
            MakefileLexer: IndentBasedFoldDetector(),
            CMakeLexer: IndentBasedFoldDetector(),
            RstLexer: IndentBasedFoldDetector(),

            # c family
            CLexer: CharBasedFoldDetector(),
            CppLexer: CharBasedFoldDetector(),
            CSharpLexer: CharBasedFoldDetector(),
            ActionScriptLexer: CharBasedFoldDetector(),
            CoffeeScriptLexer: CharBasedFoldDetector(),
            CssLexer: CharBasedFoldDetector(),
            JavascriptLexer: CharBasedFoldDetector(),
            JavaLexer: CharBasedFoldDetector(),
            QmlLexer: CharBasedFoldDetector(),
            PhpLexer: CharBasedFoldDetector(),
            AdaLexer: CharBasedFoldDetector(),
            CudaLexer: CharBasedFoldDetector(),
            DLexer: CharBasedFoldDetector(),
            GLShaderLexer: CharBasedFoldDetector(),
            GoLexer: CharBasedFoldDetector(),
            ObjectiveCLexer: CharBasedFoldDetector(),
            ObjectiveCppLexer: CharBasedFoldDetector(),
            ValaLexer: CharBasedFoldDetector(),
        }
    except NameError:
        logger.warning("PygmentsSyntaxHighlighter: Failed to setup fold "
                       "detectors associations. Please upgrade your pygments "
                       "installation.")
        LEXERS_FOLD_DETECTORS = {}

    @property
    def pygmentsStyle(self):
        return self.editor.style.value("pygmentsStyle")

    @pygmentsStyle.setter
    def pygmentsStyle(self, value):
        return self.editor.style.setValue("pygmentsStyle", value)

    def __init__(self, document, lexer=None):
        super(PygmentsSyntaxHighlighter, self).__init__(document)
        self._document = QtGui.QTextDocument()
        self._formatter = HtmlFormatter(nowrap=True)
        self._lexer = lexer if lexer else PythonLexer()
        self.__previousFilename = ""
        self.style = "default"

    def _onInstall(self, editor):
        """
        :type editor: pyqode.code.QCodeEdit
        """
        SyntaxHighlighter._onInstall(self, editor)
        self.triggers = ["*", '**', '"', "'", "/"]
        self._clear_caches()
        self.prev_txt = ""
        style = editor.style.addProperty("pygmentsStyle", "default")
        self.style = style
        self.editor.style.setValue(
                    "background",
                    QtGui.QColor(self.style.background_color))
        c = self.style.style_for_token(Text)['color']
        if c is None:
            c = '000000'
        self.editor.style.setValue(
            "foreground", QtGui.QColor("#{0}".format(c)))

    def _onStateChanged(self, state):
        self.enabled = state
        if state is True:
            self.editor.textSaved.connect(self.rehighlight)
        else:
            self.editor.textSaved.disconnect(self.rehighlight)
        self.rehighlight()

    def __updateLexer(self):
        self.setLexerFromFilename(self.editor.fileName)
        if hasattr(self.editor, "foldingPanel"):
            if type(self._lexer) in self.LEXERS_FOLD_DETECTORS:
                self.setFoldDetector(
                    self.LEXERS_FOLD_DETECTORS[type(self._lexer)])
                self.editor.foldingPanel.enabled = True
            else:
                self.editor.foldingPanel.enabled = False

    def __onTextSaved(self):
        self.rehighlight()

    def _onStyleChanged(self, section, key):
        """ Updates the pygments style """
        if key == "pygmentsStyle" or not key:
            self.style = self.editor.style.value(
                "pygmentsStyle")
            self.rehighlight()
            self.editor.style.setValue(
                "background",
                QtGui.QColor(self.style.background_color))
            c = self.style.style_for_token(Text)['color']
            if c is None:
                c = '000000'
            self.editor.style.setValue(
                "foreground", QtGui.QColor("#{0}".format(c)))

    def setLexerFromFilename(self, filename):
        """
        Change the lexer based on the filename (actually only the extension is
        needed)

        :param filename: Filename or extension
        """
        try:
            if filename.endswith("~"):
                filename = filename[0:len(filename) - 1]
            self._lexer = get_lexer_for_filename(filename)
        except ClassNotFound:
            logger.warning("Failed to find lexer from filename %s" % filename)
            self._lexer = None

    def setLexerFromMimeType(self, mime, **options):
        try:
            self._lexer = get_lexer_for_mimetype(mime, **options)
        except ClassNotFound:
            logger.warning("Failed to find lexer from mime type %s" % mime)
            self._lexer = None

    def doHighlightBlock(self, text):
        fn = self.editor.fileName
        if fn != self.__previousFilename:
            self.__previousFilename = fn
            self.__updateLexer()
        if self._lexer is None:
            return

        #Spaces
        expression = QRegExp('\s+')
        index = expression.indexIn(text, 0)
        while index >= 0:
            index = expression.pos(0)
            length = len(expression.cap(0))
            self.setFormat(index, length, self._get_format(Whitespace))
            index = expression.indexIn(text, index + length)

        if self.enabled is False:
            return
        prev_data = self.currentBlock().previous().userData()

        if hasattr(prev_data, "syntax_stack"):
            self._lexer._saved_state_stack = prev_data.syntax_stack
        elif hasattr(self._lexer, '_saved_state_stack'):
            del self._lexer._saved_state_stack

        # Lex the text using Pygments
        index = 0
        usd = self.currentBlock().userData()
        usd.cc_disabled_zones[:] = []
        for token, text in self._lexer.get_tokens(text):
            length = len(text)
            if "comment" in str(token).lower():
                # to the end
                usd.cc_disabled_zones.append((index, pow(2, 32)))
            elif "string" in str(token).lower():
                usd.cc_disabled_zones.append((index, index + length))
            self.setFormat(index, length, self._get_format(token))
            index += length

        if hasattr(self._lexer, '_saved_state_stack'):
            data = self.currentBlock().userData()
            setattr(data, "syntax_stack", self._lexer._saved_state_stack)
            self.currentBlock().setUserData(data)
            # Clean up for the next go-round.
            del self._lexer._saved_state_stack

    def __set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if (isinstance(style, str) or
                isinstance(style, unicode)):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches()

    def __get_style(self):
        return self._style

    #: gets/sets the **pygments** style.
    style = property(__get_style, __set_style)

    def _clear_caches(self):
        """ Clear caches for brushes and formats.
        """
        self._brushes = {}
        self._formats = {}

    def _get_format(self, token):
        """ Returns a QTextCharFormat for token or None.
        """
        if token in self._formats:
            return self._formats[token]

        if self._style is None:
            result = self._get_format_from_document(token, self._document)
        else:
            result = self._get_format_from_style(token, self._style)

        self._formats[token] = result
        return result

    def _get_format_from_document(self, token, document):
        """ Returns a QTextCharFormat for token by
        """
        code, html = next(self._formatter._format_lines([(token, 'dummy')]))
        self._document.setHtml(html)
        return QtGui.QTextCursor(self._document).charFormat()

    def _get_format_from_style(self, token, style):
        """ Returns a QTextCharFormat for token by reading a Pygments style.
        """
        result = QtGui.QTextCharFormat()
        for key, value in list(style.style_for_token(token).items()):
            if value:
                if key == 'color':
                    result.setForeground(self._get_brush(value))
                elif key == 'bgcolor':
                    result.setBackground(self._get_brush(value))
                elif key == 'bold':
                    result.setFontWeight(QtGui.QFont.Bold)
                elif key == 'italic':
                    result.setFontItalic(True)
                elif key == 'underline':
                    result.setUnderlineStyle(
                        QtGui.QTextCharFormat.SingleUnderline)
                elif key == 'sans':
                    result.setFontStyleHint(QtGui.QFont.SansSerif)
                elif key == 'roman':
                    result.setFontStyleHint(QtGui.QFont.Times)
                elif key == 'mono':
                    result.setFontStyleHint(QtGui.QFont.TypeWriter)
        return result

    def _get_brush(self, color):
        """ Returns a brush for the color.
        """
        result = self._brushes.get(color)
        if result is None:
            qcolor = self._get_color(color)
            result = QtGui.QBrush(qcolor)
            self._brushes[color] = result
        return result

    @staticmethod
    def _get_color(color):
        """ Returns a QColor built from a Pygments color string.
        """
        color = str(color).replace("#", "")
        qcolor = QtGui.QColor()
        qcolor.setRgb(int(color[:2], base=16),
                      int(color[2:4], base=16),
                      int(color[4:6], base=16))
        return qcolor