Skip to content

Commit a93ded4

Browse files
committed
Merge branch 'master' of https://github.com/pygments/pygments
2 parents 6f07530 + c2cf688 commit a93ded4

76 files changed

Lines changed: 6127 additions & 236 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,7 @@ Other contributors, listed alphabetically, are:
237237
* Hubert Gruniaux -- C and C++ lexer improvements
238238
* Thomas Symalla -- AMDGPU Lexer
239239
* 15b3 -- Image Formatter improvements
240+
* Fabian Neumann -- CDDL lexer
241+
* Thomas Duboucher -- CDDL lexer
240242

241243
Many thanks for all contributions!

CHANGES

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@ Version 2.8.0
3939
- Changed setuptools to use a declarative config through ``setup.cfg``.
4040
Building Pygments now requires setuptools 39.2+.
4141
- Added markdown to MarkdownLexer aliases (#1687)
42+
- Changed line number handling
43+
44+
* In ``<table>`` based output, the ``td.linenos`` element will have either a
45+
``normal`` or ``special`` class attached. Previously, only ``special`` line
46+
numbers got a class. This prevents styles from getting applied twice -
47+
once via ``<pre>``, once via ``<span class="special">``. This also means
48+
that ``td.linenos pre`` is no longer styled, instead, use
49+
``td.linenos .normal`` and ``td.linenos .special``.
50+
* In the "inline" style, the DOM element order was changed. The line number
51+
is added first, then the line is wrapped is wrapped by the highlighter.
52+
This fixes lines not being fully highlighted.
53+
* The visual output for inline and non-inline line numbers & highlighting,
54+
as well as class-based and inline styling is now consistent.
55+
* Line number styles are set to ``background-color: transparent`` and
56+
``color: inherit`` by default. This works much better with dark styles
57+
which don't have colors set for line numbers.
58+
4259
- Removed "raw" alias from RawTokenLexer, so that it cannot be
4360
selected by alias.
61+
- Fixed RawTokenLexer to work in Python 3 and handle exceptions.
4462
- Added prompt colors to the Solarized theme (#1529)
4563

4664
Version 2.7.4

doc/languages.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Other markup
220220
* BBCode
221221
* CapDL
222222
* `Cap'n Proto <https://capnproto.com>`_
223+
* `CDDL <https://datatracker.ietf.org/doc/rfc8610/>`_
223224
* CMake
224225
* `Csound <https://csound.com>`_ scores
225226
* CSS

pygments/formatters/html.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,9 @@ def get_background_style_defs(self, arg=None):
552552
def get_linenos_style_defs(self):
553553
lines = [
554554
'pre { %s }' % self._pre_style,
555-
'td.linenos pre { %s }' % self._linenos_style,
555+
'td.linenos .normal { %s }' % self._linenos_style,
556556
'span.linenos { %s }' % self._linenos_style,
557-
'td.linenos pre.special { %s }' % self._linenos_special_style,
557+
'td.linenos .special { %s }' % self._linenos_special_style,
558558
'span.linenos.special { %s }' % self._linenos_special_style,
559559
]
560560

@@ -682,7 +682,7 @@ def _wrap_tablelinenos(self, inner):
682682
if special_line:
683683
style = ' class="special"'
684684
else:
685-
style = ''
685+
style = ' class="normal"'
686686

687687
if style:
688688
line = '<span%s>%s</span>' % (style, line)
@@ -930,11 +930,16 @@ def format_unencoded(self, tokensource, outfile):
930930
linewise, e.g. line number generators.
931931
"""
932932
source = self._format_lines(tokensource)
933+
934+
# As a special case, we wrap line numbers before line highlighting
935+
# so the line numbers get wrapped in the highlighting tag.
936+
if not self.nowrap and self.linenos == 2:
937+
source = self._wrap_inlinelinenos(source)
938+
933939
if self.hl_lines:
934940
source = self._highlight_lines(source)
941+
935942
if not self.nowrap:
936-
if self.linenos == 2:
937-
source = self._wrap_inlinelinenos(source)
938943
if self.lineanchors:
939944
source = self._wrap_lineanchors(source)
940945
if self.linespans:

pygments/formatters/img.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ def _get_text_color(self, style):
452452
fill = '#000'
453453
return fill
454454

455+
def _get_text_bg_color(self, style):
456+
"""
457+
Get the correct background color for the token from the style.
458+
"""
459+
if style['bgcolor'] is not None:
460+
bg_color = '#' + style['bgcolor']
461+
else:
462+
bg_color = None
463+
return bg_color
464+
455465
def _get_style_font(self, style):
456466
"""
457467
Get the correct font for the style.
@@ -474,14 +484,15 @@ def _draw_linenumber(self, posno, lineno):
474484
str(lineno).rjust(self.line_number_chars),
475485
font=self.fonts.get_font(self.line_number_bold,
476486
self.line_number_italic),
477-
fill=self.line_number_fg,
487+
text_fg=self.line_number_fg,
488+
text_bg=None,
478489
)
479490

480-
def _draw_text(self, pos, text, font, **kw):
491+
def _draw_text(self, pos, text, font, text_fg, text_bg):
481492
"""
482493
Remember a single drawable tuple to paint later.
483494
"""
484-
self.drawables.append((pos, text, font, kw))
495+
self.drawables.append((pos, text, font, text_fg, text_bg))
485496

486497
def _create_drawables(self, tokensource):
487498
"""
@@ -506,7 +517,8 @@ def _create_drawables(self, tokensource):
506517
self._get_text_pos(linelength, lineno),
507518
temp,
508519
font = self._get_style_font(style),
509-
fill = self._get_text_color(style)
520+
text_fg = self._get_text_color(style),
521+
text_bg = self._get_text_bg_color(style),
510522
)
511523
temp_width, temp_hight = self.fonts.get_text_size(temp)
512524
linelength += temp_width
@@ -576,8 +588,11 @@ def format(self, tokensource, outfile):
576588
y = self._get_line_y(linenumber - 1)
577589
draw.rectangle([(x, y), (x + rectw, y + recth)],
578590
fill=self.hl_color)
579-
for pos, value, font, kw in self.drawables:
580-
draw.text(pos, value, font=font, **kw)
591+
for pos, value, font, text_fg, text_bg in self.drawables:
592+
if text_bg:
593+
text_size = draw.textsize(text=value, font=font)
594+
draw.rectangle([pos[0], pos[1], pos[0] + text_size[0], pos[1] + text_size[1]], fill=text_bg)
595+
draw.text(pos, value, font=font, fill=text_fg)
581596
im.save(outfile, self.image_format.upper())
582597

583598

pygments/formatters/latex.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ def rgbcolor(col):
299299
cmndef += (r'\def\$$@tc##1{\textcolor[rgb]{%s}{##1}}' %
300300
rgbcolor(ndef['color']))
301301
if ndef['border']:
302-
cmndef += (r'\def\$$@bc##1{\setlength{\fboxsep}{0pt}'
303-
r'\fcolorbox[rgb]{%s}{%s}{\strut ##1}}' %
302+
cmndef += (r'\def\$$@bc##1{{\setlength{\fboxsep}{-\fboxrule}'
303+
r'\fcolorbox[rgb]{%s}{%s}{\strut ##1}}}' %
304304
(rgbcolor(ndef['border']),
305305
rgbcolor(ndef['bgcolor'])))
306306
elif ndef['bgcolor']:
307-
cmndef += (r'\def\$$@bc##1{\setlength{\fboxsep}{0pt}'
308-
r'\colorbox[rgb]{%s}{\strut ##1}}' %
307+
cmndef += (r'\def\$$@bc##1{{\setlength{\fboxsep}{0pt}'
308+
r'\colorbox[rgb]{%s}{\strut ##1}}}' %
309309
rgbcolor(ndef['bgcolor']))
310310
if cmndef == '':
311311
continue
@@ -321,8 +321,7 @@ def get_style_defs(self, arg=''):
321321
cp = self.commandprefix
322322
styles = []
323323
for name, definition in self.cmd2def.items():
324-
styles.append(r'\expandafter\def\csname %s@tok@%s\endcsname{%s}' %
325-
(cp, name, definition))
324+
styles.append(r'\@namedef{%s@tok@%s}{%s}' % (cp, name, definition))
326325
return STYLE_TEMPLATE % {'cp': self.commandprefix,
327326
'styles': '\n'.join(styles)}
328327

@@ -342,7 +341,8 @@ def format_unencoded(self, tokensource, outfile):
342341
(start and ',firstnumber=%d' % start or '') +
343342
(step and ',stepnumber=%d' % step or ''))
344343
if self.mathescape or self.texcomments or self.escapeinside:
345-
outfile.write(',codes={\\catcode`\\$=3\\catcode`\\^=7\\catcode`\\_=8}')
344+
outfile.write(',codes={\\catcode`\\$=3\\catcode`\\^=7'
345+
'\\catcode`\\_=8\\relax}')
346346
if self.verboptions:
347347
outfile.write(',' + self.verboptions)
348348
outfile.write(']\n')

pygments/formatters/other.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,32 @@ def format(self, tokensource, outfile):
8787
import gzip
8888
outfile = gzip.GzipFile('', 'wb', 9, outfile)
8989

90-
def write(text):
91-
outfile.write(text.encode())
92-
flush = outfile.flush
90+
write = outfile.write
91+
flush = outfile.close
9392
elif self.compress == 'bz2':
9493
import bz2
9594
compressor = bz2.BZ2Compressor(9)
9695

9796
def write(text):
98-
outfile.write(compressor.compress(text.encode()))
97+
outfile.write(compressor.compress(text))
9998

10099
def flush():
101100
outfile.write(compressor.flush())
102101
outfile.flush()
103102
else:
104-
def write(text):
105-
outfile.write(text.encode())
103+
write = outfile.write
106104
flush = outfile.flush
107105

108106
if self.error_color:
109107
for ttype, value in tokensource:
110-
line = "%s\t%r\n" % (ttype, value)
108+
line = b"%r\t%r\n" % (ttype, value)
111109
if ttype is Token.Error:
112110
write(colorize(self.error_color, line))
113111
else:
114112
write(line)
115113
else:
116114
for ttype, value in tokensource:
117-
write("%s\t%r\n" % (ttype, value))
115+
write(b"%r\t%r\n" % (ttype, value))
118116
flush()
119117

120118

pygments/lexers/_cocoa_builtins.py

Lines changed: 15 additions & 10 deletions
Large diffs are not rendered by default.

pygments/lexers/_mapping.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
'CapDLLexer': ('pygments.lexers.esoteric', 'CapDL', ('capdl',), ('*.cdl',), ()),
7979
'CapnProtoLexer': ('pygments.lexers.capnproto', "Cap'n Proto", ('capnp',), ('*.capnp',), ()),
8080
'CbmBasicV2Lexer': ('pygments.lexers.basic', 'CBM BASIC V2', ('cbmbas',), ('*.bas',), ()),
81+
'CddlLexer': ('pygments.lexers.cddl', 'CDDL', ('cddl',), ('*.cddl',), ('text/x-cddl',)),
8182
'CeylonLexer': ('pygments.lexers.jvm', 'Ceylon', ('ceylon',), ('*.ceylon',), ('text/x-ceylon',)),
8283
'Cfengine3Lexer': ('pygments.lexers.configs', 'CFEngine3', ('cfengine3', 'cf3'), ('*.cf',), ()),
8384
'ChaiscriptLexer': ('pygments.lexers.scripting', 'ChaiScript', ('chai', 'chaiscript'), ('*.chai',), ('text/x-chaiscript', 'application/x-chaiscript')),

pygments/lexers/basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ class CbmBasicV2Lexer(RegexLexer):
351351
]
352352
}
353353

354-
def analyse_text(self, text):
354+
def analyse_text(text):
355355
# if it starts with a line number, it shouldn't be a "modern" Basic
356356
# like VB.net
357-
if re.match(r'\d+', text):
357+
if re.match(r'^\d+', text):
358358
return 0.2
359359

360360

0 commit comments

Comments
 (0)