Skip to content

Commit 06fc74b

Browse files
authored
Fix LatexTheme to escape "#" (#4138)
1 parent 619d1e4 commit 06fc74b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

scapy/packet.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,11 @@ def _show_or_dump(self,
14421442
fvalue = self.getfieldval(f.name)
14431443
if isinstance(fvalue, Packet) or (f.islist and f.holds_packets and isinstance(fvalue, list)): # noqa: E501
14441444
pad = max(0, 10 - len(f.name)) * " "
1445-
s += "%s \\%s%s\\\n" % (label_lvl + lvl, ncol(f.name), pad)
1445+
s += "%s %s%s%s%s\n" % (label_lvl + lvl,
1446+
ct.punct("\\"),
1447+
ncol(f.name),
1448+
pad,
1449+
ct.punct("\\"))
14461450
fvalue_gen = SetGen(
14471451
fvalue,
14481452
_iterpacket=0

scapy/themes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ def __getattr__(self, attr):
271271

272272

273273
class LatexTheme(FormatTheme):
274+
r"""
275+
You can prepend the output from this theme with
276+
\tt\obeyspaces\obeylines\tiny\noindent
277+
"""
274278
style_prompt = r"\textcolor{blue}{%s}"
275279
style_not_printable = r"\textcolor{gray}{%s}"
276280
style_layer_name = r"\textcolor{red}{\bf %s}"
@@ -289,6 +293,11 @@ class LatexTheme(FormatTheme):
289293
# style_odd = ""
290294
style_logo = r"\textcolor{green}{\bf %s}"
291295

296+
def __getattr__(self, attr: str) -> Callable[[Any], str]:
297+
from scapy.utils import tex_escape
298+
styler = super(LatexTheme, self).__getattr__(attr)
299+
return lambda x: styler(tex_escape(x))
300+
292301

293302
class LatexTheme2(FormatTheme):
294303
style_prompt = r"@`@textcolor@[@blue@]@@[@%s@]@"

test/regression.uts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ def test_list_contrib():
110110

111111
test_list_contrib()
112112

113+
= Test packet show() on LatexTheme
114+
% with LatexTheme
115+
116+
class SmallPacket(Packet):
117+
fields_desc = [ByteField("a", 0)]
118+
119+
conf_color_theme = conf.color_theme
120+
conf.color_theme = LatexTheme()
121+
pkt = SmallPacket()
122+
with ContextManagerCaptureOutput() as cmco:
123+
pkt.show()
124+
result = cmco.get_output().strip()
125+
126+
assert result == '\\#\\#\\#[ \\textcolor{red}{\\bf SmallPacket} ]\\#\\#\\# \n \\textcolor{blue}{a} = \\textcolor{purple}{0}'
127+
conf.color_theme = conf_color_theme
128+
129+
113130
= Test automatic doc generation
114131
~ command
115132

0 commit comments

Comments
 (0)