diff options
| author | Paweł Fertyk <pfertyk@users.noreply.github.com> | 2020-07-04 18:10:03 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-04 18:10:03 +0200 |
| commit | 728fd19ebcb69ddb2c7af159939e1a19ae53a892 (patch) | |
| tree | ed0c18f61887b61a9c4f41cf021a202dc92c3ad1 /pygments/formatters | |
| parent | b5577a68a9c286f4bce157d63e8c83ed8d70e704 (diff) | |
| download | pygments-git-728fd19ebcb69ddb2c7af159939e1a19ae53a892.tar.gz | |
Fix Solarized line number colors (#1477)
* Add font and background colors to Style
* Move all styles to get_style_defs, add tests
* Remove hardcoded styles, add special lineno style
* Add styles for special line numbers in tables
* Update noclasses documentation
* Refactor linenos elements and styles, add tests
* Update AUTHORS
* Fix multiple CSS prefixes, add tests
Diffstat (limited to 'pygments/formatters')
| -rw-r--r-- | pygments/formatters/html.py | 233 |
1 files changed, 142 insertions, 91 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index c5ae774d..c3100967 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -65,9 +65,6 @@ 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%%; } %(styledefs)s ''' @@ -208,9 +205,10 @@ class HtmlFormatter(Formatter): `cssfile` exists. `noclasses` - If set to true, token ``<span>`` tags 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 @@ -438,7 +436,7 @@ class HtmlFormatter(Formatter): self.lineseparator = options.get('lineseparator', u'\n') self.lineanchors = options.get('lineanchors', '') self.linespans = options.get('linespans', '') - self.anchorlinenos = options.get('anchorlinenos', False) + self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False) self.hl_lines = set() for lineno in get_list_opt(options, 'hl_lines', []): try: @@ -495,6 +493,66 @@ 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 pre { %s }' % self._linenos_style, + 'span.linenos { %s }' % self._linenos_style, + 'td.linenos pre.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): @@ -510,23 +568,25 @@ class HtmlFormatter(Formatter): tmp.append((arg and arg + ' ' or '') + cls) return ', '.join(tmp) - 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] - if arg and not self.nobackground and \ - self.style.background_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(''), self.style.background_color, text_style)) - if self.style.highlight_color is not None: - lines.insert(0, '%s.hll { background-color: %s }' % - (prefix(''), self.style.highlight_color)) - return '\n'.join(lines) + return prefix + + @property + def _pre_style(self): + return 'line-height: 125%; margin: 0;' + + @property + def _linenos_style(self): + return 'color: %s; background-color: %s; padding: 0 5px 0 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: 0 5px 0 5px;' % ( + self.style.line_number_special_color, + self.style.line_number_special_background_color + ) def _decodeifneeded(self, value): if isinstance(value, bytes): @@ -592,88 +652,79 @@ class HtmlFormatter(Formatter): la = self.lineanchors aln = self.anchorlinenos nocls = self.noclasses - if sp: - lines = [] - - for i in range(fl, fl+lncount): - if i % st == 0: - if i % sp == 0: - if aln: - lines.append('<a href="#%s-%d" class="special">%*d</a>' % - (la, i, mw, i)) - else: - lines.append('<span class="special">%*d</span>' % (mw, i)) - else: - if aln: - lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i)) - else: - lines.append('%*d' % (mw, i)) + + 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: - lines.append('') - ls = '\n'.join(lines) - else: - lines = [] - for i in range(fl, fl+lncount): - if i % st == 0: - if aln: - lines.append('<a href="#%s-%d">%*d</a>' % (la, i, mw, i)) - else: - lines.append('%*d' % (mw, i)) + style = ' style="%s"' % self._linenos_style + else: + if special_line: + style = ' class="special"' else: - lines.append('') - ls = '\n'.join(lines) + style = '' + + line = '<pre%s>%s</pre>' % (style, line) + + lines.append(line) + + ls = '\n'.join(lines) # 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... - if nocls: - yield 0, ('<table class="%stable">' % self.cssclass + - '<tr><td><div class="linenodiv" ' - 'style="background-color: #f0f0f0; padding-right: 10px">' - '<pre style="line-height: 125%">' + - ls + '</pre></div></td><td class="code">') - else: - yield 0, ('<table class="%stable">' % self.cssclass + - '<tr><td class="linenos"><div class="linenodiv"><pre>' + - ls + '</pre></div></td><td class="code">') + yield 0, ( + '<table class="%stable">' % self.cssclass + + '<tr><td class="linenos"><div class="linenodiv">' + + ls + '</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 :( - lines = list(inner) + inner_lines = list(inner) sp = self.linenospecial st = self.linenostep num = self.linenostart - mw = len(str(len(lines) + num - 1)) + mw = len(str(len(inner_lines) + num - 1)) + nocls = self.noclasses - if self.noclasses: - if sp: - for t, line in lines: - if num % sp == 0: - style = 'background-color: #ffffc0; padding: 0 5px 0 5px' - else: - style = 'background-color: #f0f0f0; padding: 0 5px 0 5px' - yield 1, '<span style="%s">%*s </span>' % ( - style, mw, (num % st and ' ' or num)) + line - num += 1 + 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: - for t, line in lines: - yield 1, ('<span style="background-color: #f0f0f0; ' - 'padding: 0 5px 0 5px">%*s </span>' % ( - mw, (num % st and ' ' or num)) + line) - num += 1 - elif sp: - for t, line in lines: - yield 1, '<span class="lineno%s">%*s </span>' % ( - num % sp == 0 and ' special' or '', mw, - (num % st and ' ' or num)) + line - num += 1 - else: - for t, line in lines: - yield 1, '<span class="lineno">%*s </span>' % ( - mw, (num % st and ' ' or num)) + line - num += 1 + 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"' + + yield 1, '<span%s>%s</span>' % (style, line) + inner_line + num += 1 def _wrap_lineanchors(self, inner): s = self.lineanchors @@ -716,7 +767,7 @@ class HtmlFormatter(Formatter): if self.prestyles: style.append(self.prestyles) if self.noclasses: - style.append('line-height: 125%') + style.append(self._pre_style) style = '; '.join(style) if self.filename: |
