Skip to content

Commit a6a4abb

Browse files
committed
Keep any trailing whitespace passed into SyntaxHighlighter
1 parent f7ce40a commit a6a4abb

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.4.3 (2024-07-15)
4+
5+
- Keep any trailing whitespace passed into SyntaxHighlighter
6+
37
## 0.4.2 (2024-07-15)
48

59
- Fix typing import unavailable on 3.8

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
setup(
1717
name="structlog-pretty",
18-
version="0.4.2",
18+
version="0.4.3",
1919
url="https://github.com/underyx/structlog-pretty",
2020
author="Bence Nagy",
2121
author_email="bence@underyx.me",

structlog_pretty/processors.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import, print_function
22

33
from pathlib import Path
4+
import re
45
import sys
56
import json
67

@@ -178,9 +179,19 @@ def __call__(self, _, __, event_dict):
178179
code = event_dict[field]
179180
except KeyError:
180181
continue
182+
181183
if not code:
182184
continue
183-
event_dict[field] = highlight(code, lexer, TerminalFormatter()).rstrip()
185+
186+
trailing_whitespace_match = re.search(r"\s*$", code)
187+
trailing_whitespace = (
188+
trailing_whitespace_match.group(0) if trailing_whitespace_match else ""
189+
)
190+
191+
event_dict[field] = (
192+
highlight(code, lexer, TerminalFormatter()).rstrip()
193+
+ trailing_whitespace
194+
)
184195

185196
return event_dict
186197

test/test_SyntaxHighlighter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from structlog_pretty.processors import SyntaxHighlighter as uut
23

34

@@ -10,6 +11,14 @@ def test_json():
1011
), "should not have trailing newline added"
1112

1213

14+
def test_retain_whitespace():
15+
processor = uut(field_map={"body": "json"})
16+
event_dict = processor(None, None, {"body": '{"ping": true}\n\n'})
17+
match = re.search(r"\s*$", event_dict["body"])
18+
assert match is not None
19+
assert match.group() == "\n\n"
20+
21+
1322
def test_missing_json():
1423
processor = uut(field_map={"body": "json"})
1524
event_dict = processor(None, None, {"not_body": '{"ping": true}'})

0 commit comments

Comments
 (0)