diff options
author | shadchin <shadchin@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:44:39 +0300 |
commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/python/Pygments/py3/pygments/formatters/html.py | |
parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
download | ydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz |
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/python/Pygments/py3/pygments/formatters/html.py')
-rw-r--r-- | contrib/python/Pygments/py3/pygments/formatters/html.py | 460 |
1 files changed, 230 insertions, 230 deletions
diff --git a/contrib/python/Pygments/py3/pygments/formatters/html.py b/contrib/python/Pygments/py3/pygments/formatters/html.py index 4096506ad5..f3a77a2ddf 100644 --- a/contrib/python/Pygments/py3/pygments/formatters/html.py +++ b/contrib/python/Pygments/py3/pygments/formatters/html.py @@ -4,19 +4,19 @@ Formatter for HTML output. - :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ -import functools +import functools import os import sys import os.path -from io import StringIO +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 +from pygments.util import get_bool_opt, get_int_opt, get_list_opt try: import ctags @@ -27,11 +27,11 @@ __all__ = ['HtmlFormatter'] _escape_html_table = { - ord('&'): '&', - ord('<'): '<', - ord('>'): '>', - ord('"'): '"', - ord("'"): ''', + ord('&'): '&', + ord('<'): '<', + ord('>'): '>', + ord('"'): '"', + ord("'"): ''', } @@ -39,14 +39,14 @@ def escape_html(text, table=_escape_html_table): """Escape &, <, > as well as single and double quotes for HTML.""" 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: @@ -61,8 +61,8 @@ def _get_ttype_class(ttype): CSSFILE_TEMPLATE = '''\ /* -generated by Pygments <https://pygments.org/> -Copyright 2006-2021 by the Pygments team. +generated by Pygments <https://pygments.org/> +Copyright 2006-2021 by the Pygments team. Licensed under the BSD license, see LICENSE for details. */ %(styledefs)s @@ -72,8 +72,8 @@ 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-2021 by the Pygments team. +generated by Pygments <https://pygments.org/> +Copyright 2006-2021 by the Pygments team. Licensed under the BSD license, see LICENSE for details. --> <html> @@ -205,10 +205,10 @@ class HtmlFormatter(Formatter): `cssfile` exists. `noclasses` - If set to true, token ``<span>`` tags (as well as line number elements) - will not use CSS classes, but inline styles. This is not recommended - for larger pieces of code since it increases output size by quite a bit - (default: ``False``). + If set to true, token ``<span>`` tags (as well as line number elements) + will not use CSS classes, but inline styles. This is not recommended + for larger pieces of code since it increases output size by quite a bit + (default: ``False``). `classprefix` Since the token types use relatively short class names, they may clash @@ -272,9 +272,9 @@ class HtmlFormatter(Formatter): 125%``). `hl_lines` - Specify a list of lines to be highlighted. The line numbers are always - relative to the input (i.e. the first line is line 1) and are - independent of `linenostart`. + Specify a list of lines to be highlighted. The line numbers are always + relative to the input (i.e. the first line is line 1) and are + independent of `linenostart`. .. versionadded:: 0.11 @@ -305,7 +305,7 @@ class HtmlFormatter(Formatter): `lineanchors` If set to a nonempty string, e.g. ``foo``, the formatter will wrap each - output line in an anchor tag with an ``id`` (and `name`) of ``foo-linenumber``. + output line in an anchor tag with an ``id`` (and `name`) of ``foo-linenumber``. This allows easy linking to certain lines. .. versionadded:: 0.9 @@ -337,9 +337,9 @@ class HtmlFormatter(Formatter): `filename` A string used to generate a filename when rendering ``<pre>`` blocks, - for example if displaying source code. If `linenos` is set to - ``'table'`` then the filename will be rendered in an initial row - containing a single `<th>` which spans both columns. + for example if displaying source code. If `linenos` is set to + ``'table'`` then the filename will be rendered in an initial row + containing a single `<th>` which spans both columns. .. versionadded:: 2.1 @@ -424,7 +424,7 @@ class HtmlFormatter(Formatter): self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', '')) self.filename = self._decodeifneeded(options.get('filename', '')) self.wrapcode = get_bool_opt(options, 'wrapcode', False) - self.span_element_openers = {} + self.span_element_openers = {} self.debug_token_types = get_bool_opt(options, 'debug_token_types', False) if self.tagsfile: @@ -445,10 +445,10 @@ class HtmlFormatter(Formatter): 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', '\n') self.lineanchors = options.get('lineanchors', '') self.linespans = options.get('linespans', '') - self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False) + self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False) self.hl_lines = set() for lineno in get_list_opt(options, 'hl_lines', []): try: @@ -467,21 +467,21 @@ class HtmlFormatter(Formatter): return '' def _get_css_classes(self, ttype): - """Return the CSS classes of this token type prefixed with the classprefix option.""" + """Return the CSS classes of this token type prefixed with the classprefix option.""" cls = self._get_css_class(ttype) while ttype not in STANDARD_TYPES: ttype = ttype.parent cls = self._get_css_class(ttype) + ' ' + cls - return cls or '' - - def _get_css_inline_styles(self, ttype): - """Return the inline CSS styles for this token type.""" - cclass = self.ttype2class.get(ttype) - while cclass is None: - ttype = ttype.parent - cclass = self.ttype2class.get(ttype) - return cclass or '' - + return cls or '' + + def _get_css_inline_styles(self, ttype): + """Return the inline CSS styles for this token type.""" + cclass = self.ttype2class.get(ttype) + while cclass is None: + ttype = ttype.parent + cclass = self.ttype2class.get(ttype) + return cclass or '' + def _create_stylesheet(self): t2c = self.ttype2class = {Token: ''} c2s = self.class2style = {} @@ -512,69 +512,69 @@ class HtmlFormatter(Formatter): highlighting style. ``arg`` can be a string or list of selectors to insert before the token type classes. """ - style_lines = [] - - style_lines.extend(self.get_linenos_style_defs()) - style_lines.extend(self.get_background_style_defs(arg)) - style_lines.extend(self.get_token_style_defs(arg)) - - return '\n'.join(style_lines) - - def get_token_style_defs(self, arg=None): - prefix = self.get_css_prefix(arg) - - styles = [ - (level, ttype, cls, style) - 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:]) - for (level, ttype, cls, style) in styles - ] - - return lines - - def get_background_style_defs(self, arg=None): - prefix = self.get_css_prefix(arg) - bg_color = self.style.background_color - hl_color = self.style.highlight_color - - lines = [] - - if arg and not self.nobackground and bg_color is not None: - text_style = '' - if Text in self.ttype2class: - text_style = ' ' + self.class2style[self.ttype2class[Text]][0] - lines.insert( - 0, '%s{ background: %s;%s }' % ( - prefix(''), bg_color, text_style - ) - ) - if hl_color is not None: - lines.insert( - 0, '%s { background-color: %s }' % (prefix('hll'), hl_color) - ) - - return lines - - def get_linenos_style_defs(self): - lines = [ - 'pre { %s }' % self._pre_style, - 'td.linenos .normal { %s }' % self._linenos_style, - 'span.linenos { %s }' % self._linenos_style, - 'td.linenos .special { %s }' % self._linenos_special_style, - 'span.linenos.special { %s }' % self._linenos_special_style, - ] - - return lines - - def get_css_prefix(self, arg): + style_lines = [] + + style_lines.extend(self.get_linenos_style_defs()) + style_lines.extend(self.get_background_style_defs(arg)) + style_lines.extend(self.get_token_style_defs(arg)) + + return '\n'.join(style_lines) + + def get_token_style_defs(self, arg=None): + prefix = self.get_css_prefix(arg) + + styles = [ + (level, ttype, cls, style) + 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:]) + for (level, ttype, cls, style) in styles + ] + + return lines + + def get_background_style_defs(self, arg=None): + prefix = self.get_css_prefix(arg) + bg_color = self.style.background_color + hl_color = self.style.highlight_color + + lines = [] + + if arg and not self.nobackground and bg_color is not None: + text_style = '' + if Text in self.ttype2class: + text_style = ' ' + self.class2style[self.ttype2class[Text]][0] + lines.insert( + 0, '%s{ background: %s;%s }' % ( + prefix(''), bg_color, text_style + ) + ) + if hl_color is not None: + lines.insert( + 0, '%s { background-color: %s }' % (prefix('hll'), hl_color) + ) + + return lines + + def get_linenos_style_defs(self): + lines = [ + 'pre { %s }' % self._pre_style, + 'td.linenos .normal { %s }' % self._linenos_style, + 'span.linenos { %s }' % self._linenos_style, + 'td.linenos .special { %s }' % self._linenos_special_style, + 'span.linenos.special { %s }' % self._linenos_special_style, + ] + + return lines + + def get_css_prefix(self, arg): if arg is None: arg = ('cssclass' in self.options and '.'+self.cssclass or '') - if isinstance(arg, str): + if isinstance(arg, str): args = [arg] else: args = list(arg) @@ -587,26 +587,26 @@ class HtmlFormatter(Formatter): tmp.append((arg and arg + ' ' or '') + cls) return ', '.join(tmp) - return prefix - - @property - def _pre_style(self): - return 'line-height: 125%;' - - @property - def _linenos_style(self): - return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % ( - self.style.line_number_color, - self.style.line_number_background_color - ) - - @property - def _linenos_special_style(self): - return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % ( - self.style.line_number_special_color, - self.style.line_number_special_background_color - ) - + return prefix + + @property + def _pre_style(self): + return 'line-height: 125%;' + + @property + def _linenos_style(self): + return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % ( + self.style.line_number_color, + self.style.line_number_background_color + ) + + @property + def _linenos_special_style(self): + return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % ( + self.style.line_number_special_color, + self.style.line_number_special_background_color + ) + def _decodeifneeded(self, value): if isinstance(value, bytes): if self.encoding: @@ -638,7 +638,7 @@ class HtmlFormatter(Formatter): with open(cssfilename, "w") as cf: cf.write(CSSFILE_TEMPLATE % {'styledefs': self.get_style_defs('body')}) - except OSError as err: + except OSError as err: err.strerror = 'Error writing CSS file: ' + err.strerror raise @@ -652,7 +652,7 @@ class HtmlFormatter(Formatter): styledefs=self.get_style_defs('body'), encoding=self.encoding)) - yield from inner + yield from inner yield 0, DOC_FOOTER def _wrap_tablelinenos(self, inner): @@ -671,100 +671,100 @@ class HtmlFormatter(Formatter): aln = self.anchorlinenos nocls = self.noclasses - lines = [] - - for i in range(fl, fl+lncount): - print_line = i % st == 0 - special_line = sp and i % sp == 0 - - if print_line: - line = '%*d' % (mw, i) - if aln: - line = '<a href="#%s-%d">%s</a>' % (la, i, line) - else: - line = ' ' * mw - - if nocls: - if special_line: - style = ' style="%s"' % self._linenos_special_style + lines = [] + + for i in range(fl, fl+lncount): + print_line = i % st == 0 + special_line = sp and i % sp == 0 + + if print_line: + line = '%*d' % (mw, i) + if aln: + line = '<a href="#%s-%d">%s</a>' % (la, i, line) + else: + line = ' ' * mw + + if nocls: + if special_line: + style = ' style="%s"' % self._linenos_special_style else: - style = ' style="%s"' % self._linenos_style - else: - if special_line: - style = ' class="special"' + style = ' style="%s"' % self._linenos_style + else: + if special_line: + style = ' class="special"' else: - style = ' class="normal"' - - if style: - line = '<span%s>%s</span>' % (style, line) - - lines.append(line) - - ls = '\n'.join(lines) - - # If a filename was specified, we can't put it into the code table as it - # would misalign the line numbers. Hence we emit a separate row for it. - filename_tr = "" - if self.filename: - filename_tr = ( - '<tr><th colspan="2" class="filename"><div class="highlight">' - '<span class="filename">' + self.filename + '</span></div>' - '</th></tr>') - + style = ' class="normal"' + + if style: + line = '<span%s>%s</span>' % (style, line) + + lines.append(line) + + ls = '\n'.join(lines) + + # If a filename was specified, we can't put it into the code table as it + # would misalign the line numbers. Hence we emit a separate row for it. + filename_tr = "" + if self.filename: + filename_tr = ( + '<tr><th colspan="2" class="filename"><div class="highlight">' + '<span class="filename">' + self.filename + '</span></div>' + '</th></tr>') + # in case you wonder about the seemingly redundant <div> here: since the # content in the other cell also is wrapped in a div, some browsers in # some configurations seem to mess up the formatting... - yield 0, ( - '<table class="%stable">' % self.cssclass + filename_tr + - '<tr><td class="linenos"><div class="linenodiv"><pre>' + - ls + '</pre></div></td><td class="code">' - ) + yield 0, ( + '<table class="%stable">' % self.cssclass + filename_tr + + '<tr><td class="linenos"><div class="linenodiv"><pre>' + + ls + '</pre></div></td><td class="code">' + ) yield 0, dummyoutfile.getvalue() yield 0, '</td></tr></table>' def _wrap_inlinelinenos(self, inner): # need a list of lines since we need the width of a single number :( - inner_lines = list(inner) + inner_lines = list(inner) sp = self.linenospecial st = self.linenostep num = self.linenostart - mw = len(str(len(inner_lines) + num - 1)) - la = self.lineanchors - aln = self.anchorlinenos - nocls = self.noclasses - - for _, inner_line in inner_lines: - print_line = num % st == 0 - special_line = sp and num % sp == 0 - - if print_line: - line = '%*d' % (mw, num) + mw = len(str(len(inner_lines) + num - 1)) + la = self.lineanchors + aln = self.anchorlinenos + nocls = self.noclasses + + for _, inner_line in inner_lines: + print_line = num % st == 0 + special_line = sp and num % sp == 0 + + if print_line: + line = '%*d' % (mw, num) + else: + line = ' ' * mw + + if nocls: + if special_line: + style = ' style="%s"' % self._linenos_special_style + else: + style = ' style="%s"' % self._linenos_style + else: + if special_line: + style = ' class="linenos special"' + else: + style = ' class="linenos"' + + if style: + linenos = '<span%s>%s</span>' % (style, line) + else: + linenos = line + + if aln: + yield 1, ('<a href="#%s-%d">%s</a>' % (la, num, linenos) + + inner_line) else: - line = ' ' * mw - - if nocls: - if special_line: - style = ' style="%s"' % self._linenos_special_style - else: - style = ' style="%s"' % self._linenos_style - else: - if special_line: - style = ' class="linenos special"' - else: - style = ' class="linenos"' - - if style: - linenos = '<span%s>%s</span>' % (style, line) - else: - linenos = line - - if aln: - yield 1, ('<a href="#%s-%d">%s</a>' % (la, num, linenos) + - inner_line) - else: - yield 1, linenos + inner_line - num += 1 - + yield 1, linenos + inner_line + num += 1 + def _wrap_lineanchors(self, inner): s = self.lineanchors # subtract 1 since we have to increment i *before* yielding @@ -798,7 +798,7 @@ class HtmlFormatter(Formatter): yield 0, ('<div' + (self.cssclass and ' class="%s"' % self.cssclass) + (style and (' style="%s"' % style)) + '>') - yield from inner + yield from inner yield 0, '</div>\n' def _wrap_pre(self, inner): @@ -806,28 +806,28 @@ class HtmlFormatter(Formatter): if self.prestyles: style.append(self.prestyles) if self.noclasses: - style.append(self._pre_style) + style.append(self._pre_style) style = '; '.join(style) - if self.filename and self.linenos != 1: + if self.filename and self.linenos != 1: yield 0, ('<span class="filename">' + self.filename + '</span>') # the empty span here is to keep leading empty lines from being # ignored by HTML parsers yield 0, ('<pre' + (style and ' style="%s"' % style) + '><span></span>') - yield from inner + yield from inner yield 0, '</pre>' def _wrap_code(self, inner): yield 0, '<code>' - yield from inner + yield from inner yield 0, '</code>' - @functools.lru_cache(maxsize=100) - def _translate_parts(self, value): - """HTML-escape a value and split it by newlines.""" - return value.translate(_escape_html_table).split('\n') - + @functools.lru_cache(maxsize=100) + def _translate_parts(self, value): + """HTML-escape a value and split it by newlines.""" + return value.translate(_escape_html_table).split('\n') + def _format_lines(self, tokensource): """ Just format the tokens, without any wrapping tags. @@ -840,26 +840,26 @@ class HtmlFormatter(Formatter): lspan = '' line = [] for ttype, value in tokensource: - try: - cspan = self.span_element_openers[ttype] - except KeyError: + try: + cspan = self.span_element_openers[ttype] + except KeyError: title = ' title="%s"' % '.'.join(ttype) if self.debug_token_types else '' - if nocls: - css_style = self._get_css_inline_styles(ttype) + if nocls: + css_style = self._get_css_inline_styles(ttype) if css_style: css_style = self.class2style[css_style][0] cspan = '<span style="%s"%s>' % (css_style, title) else: cspan = '' - else: - css_class = self._get_css_classes(ttype) + else: + css_class = self._get_css_classes(ttype) if css_class: cspan = '<span class="%s"%s>' % (css_class, title) else: cspan = '' - self.span_element_openers[ttype] = cspan + self.span_element_openers[ttype] = cspan - parts = self._translate_parts(value) + parts = self._translate_parts(value) if tagsfile and ttype in Token.Name: filename, linenumber = self._lookup_ctag(value) @@ -906,7 +906,7 @@ class HtmlFormatter(Formatter): def _lookup_ctag(self, token): entry = ctags.TagEntry() - if self._ctags.find(entry, token.encode(), 0): + if self._ctags.find(entry, token.encode(), 0): return entry['file'], entry['lineNumber'] else: return None, None @@ -959,15 +959,15 @@ class HtmlFormatter(Formatter): linewise, e.g. line number generators. """ source = self._format_lines(tokensource) - - # As a special case, we wrap line numbers before line highlighting - # so the line numbers get wrapped in the highlighting tag. - if not self.nowrap and self.linenos == 2: - source = self._wrap_inlinelinenos(source) - + + # As a special case, we wrap line numbers before line highlighting + # so the line numbers get wrapped in the highlighting tag. + if not self.nowrap and self.linenos == 2: + source = self._wrap_inlinelinenos(source) + if self.hl_lines: source = self._highlight_lines(source) - + if not self.nowrap: if self.lineanchors: source = self._wrap_lineanchors(source) |