Skip to content

Commit

Permalink
Merge pull request #2270 from Ayowel/fix/ircformatter
Browse files Browse the repository at this point in the history
Remove unneeded _format_unencoded_with_lineno function from IRCFormatter
  • Loading branch information
Anteru committed Nov 13, 2022
2 parents c297baa + e61134b commit 8abbd68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
35 changes: 5 additions & 30 deletions pygments/formatters/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,38 +128,12 @@ def __init__(self, **options):
self._lineno = 0

def _write_lineno(self, outfile):
self._lineno += 1
outfile.write("\n%04d: " % self._lineno)

def _format_unencoded_with_lineno(self, tokensource, outfile):
self._write_lineno(outfile)

for ttype, value in tokensource:
if value.endswith("\n"):
self._write_lineno(outfile)
value = value[:-1]
color = self.colorscheme.get(ttype)
while color is None:
ttype = ttype.parent
color = self.colorscheme.get(ttype)
if color:
color = color[self.darkbg]
spl = value.split('\n')
for line in spl[:-1]:
self._write_lineno(outfile)
if line:
outfile.write(ircformat(color, line[:-1]))
if spl[-1]:
outfile.write(ircformat(color, spl[-1]))
else:
outfile.write(value)

outfile.write("\n")
if self.linenos:
self._lineno += 1
outfile.write("%04d: " % self._lineno)

def format_unencoded(self, tokensource, outfile):
if self.linenos:
self._format_unencoded_with_lineno(tokensource, outfile)
return
self._write_lineno(outfile)

for ttype, value in tokensource:
color = self.colorscheme.get(ttype)
Expand All @@ -173,6 +147,7 @@ def format_unencoded(self, tokensource, outfile):
if line:
outfile.write(ircformat(color, line))
outfile.write('\n')
self._write_lineno(outfile)
if spl[-1]:
outfile.write(ircformat(color, spl[-1]))
else:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_irc_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
from pygments.formatters import IRCFormatter

tokensource = list(PythonLexer().get_tokens("lambda x: 123"))

newlinetokensource = list(PythonLexer().get_tokens("from \\\n\\\n os import path\n"))

def test_correct_output():
hfmt = IRCFormatter()
houtfile = StringIO()
hfmt.format(tokensource, houtfile)

assert '\x0302lambda\x03 x: \x0302123\x03\n' == houtfile.getvalue()

def test_linecount_output():
hfmt = IRCFormatter(linenos = True)
houtfile = StringIO()
hfmt.format(newlinetokensource, houtfile)

expected_out = '0001: \x0302from\x03 \\\n0002: \\\n0003: \x1d\x0310os\x03\x1d \x0302import\x03 path\n0004: '
assert expected_out == houtfile.getvalue()

0 comments on commit 8abbd68

Please sign in to comment.