Skip to content

Commit

Permalink
Keep any trailing whitespace passed into SyntaxHighlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
underyx committed Jul 15, 2024
1 parent f7ce40a commit a6a4abb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.4.3 (2024-07-15)

- Keep any trailing whitespace passed into SyntaxHighlighter

## 0.4.2 (2024-07-15)

- Fix typing import unavailable on 3.8
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="structlog-pretty",
version="0.4.2",
version="0.4.3",
url="https://github.com/underyx/structlog-pretty",
author="Bence Nagy",
author_email="[email protected]",
Expand Down
13 changes: 12 additions & 1 deletion structlog_pretty/processors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, print_function

from pathlib import Path
import re
import sys
import json

Expand Down Expand Up @@ -178,9 +179,19 @@ def __call__(self, _, __, event_dict):
code = event_dict[field]
except KeyError:
continue

if not code:
continue
event_dict[field] = highlight(code, lexer, TerminalFormatter()).rstrip()

trailing_whitespace_match = re.search(r"\s*$", code)
trailing_whitespace = (
trailing_whitespace_match.group(0) if trailing_whitespace_match else ""
)

event_dict[field] = (
highlight(code, lexer, TerminalFormatter()).rstrip()
+ trailing_whitespace
)

return event_dict

Expand Down
9 changes: 9 additions & 0 deletions test/test_SyntaxHighlighter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from structlog_pretty.processors import SyntaxHighlighter as uut


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


def test_retain_whitespace():
processor = uut(field_map={"body": "json"})
event_dict = processor(None, None, {"body": '{"ping": true}\n\n'})
match = re.search(r"\s*$", event_dict["body"])
assert match is not None
assert match.group() == "\n\n"


def test_missing_json():
processor = uut(field_map={"body": "json"})
event_dict = processor(None, None, {"not_body": '{"ping": true}'})
Expand Down

0 comments on commit a6a4abb

Please sign in to comment.