Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unneeded _format_unencoded_with_lineno function from IRCFormatter #2270

Merged
merged 1 commit into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()