eric6/ThirdParty/Pygments/pygments/formatters/html.py

changeset 7547
21b0534faebc
parent 6942
2602857055c5
child 7701
25f42e208e08
--- a/eric6/ThirdParty/Pygments/pygments/formatters/html.py	Tue Apr 21 19:44:19 2020 +0200
+++ b/eric6/ThirdParty/Pygments/pygments/formatters/html.py	Tue Apr 21 19:47:10 2020 +0200
@@ -5,20 +5,18 @@
 
     Formatter for HTML output.
 
-    :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
+    :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
 
-from __future__ import print_function
-
 import os
 import sys
 import os.path
+from io import StringIO
 
 from pygments.formatter import Formatter
 from pygments.token import Token, Text, STANDARD_TYPES
-from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
-    StringIO, string_types, iteritems
+from pygments.util import get_bool_opt, get_int_opt, get_list_opt
 
 try:
     import ctags
@@ -42,6 +40,13 @@
     return text.translate(table)
 
 
+def webify(color):
+    if color.startswith('calc') or color.startswith('var'):
+        return color
+    else:
+        return '#' + color
+
+
 def _get_ttype_class(ttype):
     fname = STANDARD_TYPES.get(ttype)
     if fname:
@@ -55,6 +60,11 @@
 
 
 CSSFILE_TEMPLATE = '''\
+/*
+generated by Pygments <https://pygments.org/>
+Copyright 2006-2019 by the Pygments team.
+Licensed under the BSD license, see LICENSE for details.
+*/
 td.linenos { background-color: #f0f0f0; padding-right: 10px; }
 span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
 pre { line-height: 125%%; }
@@ -64,7 +74,11 @@
 DOC_HEADER = '''\
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
-
+<!--
+generated by Pygments <https://pygments.org/>
+Copyright 2006-2019 by the Pygments team.
+Licensed under the BSD license, see LICENSE for details.
+-->
 <html>
 <head>
   <title>%(title)s</title>
@@ -322,11 +336,17 @@
         .. versionadded:: 1.6
 
     `filename`
-        A string used to generate a filename when rendering <pre> blocks,
+        A string used to generate a filename when rendering ``<pre>`` blocks,
         for example if displaying source code.
 
         .. versionadded:: 2.1
 
+    `wrapcode`
+        Wrap the code inside ``<pre>`` blocks using ``<code>``, as recommended
+        by the HTML5 specification.
+
+        .. versionadded:: 2.4
+
 
     **Subclassing the HTML formatter**
 
@@ -395,6 +415,7 @@
         self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
         self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', ''))
         self.filename = self._decodeifneeded(options.get('filename', ''))
+        self.wrapcode = get_bool_opt(options, 'wrapcode', False)
 
         if self.tagsfile:
             if not ctags:
@@ -414,7 +435,7 @@
         self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
         self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0))
         self.nobackground = get_bool_opt(options, 'nobackground', False)
-        self.lineseparator = options.get('lineseparator', '\n')
+        self.lineseparator = options.get('lineseparator', u'\n')
         self.lineanchors = options.get('lineanchors', '')
         self.linespans = options.get('linespans', '')
         self.anchorlinenos = options.get('anchorlinenos', False)
@@ -451,7 +472,7 @@
             name = self._get_css_class(ttype)
             style = ''
             if ndef['color']:
-                style += 'color: #%s; ' % ndef['color']
+                style += 'color: %s; ' % webify(ndef['color'])
             if ndef['bold']:
                 style += 'font-weight: bold; '
             if ndef['italic']:
@@ -459,9 +480,9 @@
             if ndef['underline']:
                 style += 'text-decoration: underline; '
             if ndef['bgcolor']:
-                style += 'background-color: #%s; ' % ndef['bgcolor']
+                style += 'background-color: %s; ' % webify(ndef['bgcolor'])
             if ndef['border']:
-                style += 'border: 1px solid #%s; ' % ndef['border']
+                style += 'border: 1px solid %s; ' % webify(ndef['border'])
             if style:
                 t2c[ttype] = name
                 # save len(ttype) to enable ordering the styles by
@@ -476,7 +497,7 @@
         """
         if arg is None:
             arg = ('cssclass' in self.options and '.'+self.cssclass or '')
-        if isinstance(arg, string_types):
+        if isinstance(arg, str):
             args = [arg]
         else:
             args = list(arg)
@@ -490,7 +511,7 @@
             return ', '.join(tmp)
 
         styles = [(level, ttype, cls, style)
-                  for cls, (style, ttype, level) in iteritems(self.class2style)
+                  for cls, (style, ttype, level) in self.class2style.items()
                   if cls and style]
         styles.sort()
         lines = ['%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:])
@@ -535,10 +556,9 @@
             # write CSS file only if noclobber_cssfile isn't given as an option.
             try:
                 if not os.path.exists(cssfilename) or not self.noclobber_cssfile:
-                    cf = open(cssfilename, "w")
-                    cf.write(CSSFILE_TEMPLATE %
-                             {'styledefs': self.get_style_defs('body')})
-                    cf.close()
+                    with open(cssfilename, "w") as cf:
+                        cf.write(CSSFILE_TEMPLATE %
+                                 {'styledefs': self.get_style_defs('body')})
             except IOError as err:
                 err.strerror = 'Error writing CSS file: ' + err.strerror
                 raise
@@ -709,6 +729,12 @@
             yield tup
         yield 0, '</pre>'
 
+    def _wrap_code(self, inner):
+        yield 0, '<code>'
+        for tup in inner:
+            yield tup
+        yield 0, '</code>'
+
     def _format_lines(self, tokensource):
         """
         Just format the tokens, without any wrapping tags.
@@ -815,7 +841,10 @@
         individual lines, in custom generators. See docstring
         for `format`. Can be overridden.
         """
-        return self._wrap_div(self._wrap_pre(source))
+        if self.wrapcode:
+            return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
+        else:
+            return self._wrap_div(self._wrap_pre(source))
 
     def format_unencoded(self, tokensource, outfile):
         """

eric ide

mercurial