Skip to content

Commit fb92574

Browse files
authored
Merge pull request #9 from jsickcodes/trim-trailing-whitespace
Trim trailing whitespace
2 parents d28c8ea + 1ce9908 commit fb92574

6 files changed

Lines changed: 45 additions & 1 deletion

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ repos:
66
- id: check-toml
77
- id: check-json
88
- id: trailing-whitespace
9+
exclude: tests/data/trailing-whitespace/input\.txt
910

1011
- repo: https://github.com/Lucas-C/pre-commit-hooks-markup
1112
rev: v1.0.1

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Change log
22
==========
33

4+
Unreleased
5+
----------
6+
7+
- Trim trailing whitespace from the output.
8+
49
0.2.0 (2021-03-23)
510
------------------
611

src/docxplain/converter.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def convert_file(
4646
if header:
4747
insert_header(plain_path, header, docx_path.name)
4848

49+
trim_trailing_whitespace(plain_path)
50+
4951
if exists:
5052
final_hash = get_hash(plain_path)
5153
return final_hash != initial_hash
@@ -61,6 +63,16 @@ def insert_header(path: Path, header: str, docx_name: str) -> None:
6163
path.write_text(content)
6264

6365

66+
def trim_trailing_whitespace(path: Path) -> None:
67+
"""Trim trailing whitespace from the plain text file, updating it
68+
in place.
69+
"""
70+
content = path.read_text()
71+
formatted_lines = [line.rstrip() for line in content.splitlines()]
72+
new_content = "\n".join(formatted_lines) + "\n"
73+
path.write_text(new_content)
74+
75+
6476
def get_hash(path: Path) -> str:
6577
"""Get the SHA256 hash digest of a file."""
6678
m = hashlib.sha256()

tests/converter_test.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
from pathlib import Path
55

6-
from docxplain.converter import convert_file
6+
from docxplain.converter import convert_file, trim_trailing_whitespace
77

88

99
def test_unchanged(tmp_path: Path) -> None:
@@ -72,3 +72,21 @@ def test_header_templating(tmp_path: Path) -> None:
7272
assert plain_path.is_file()
7373
content = plain_path.read_text().splitlines()
7474
assert content[0] == "This file is autogenerated from test_doc.docx."
75+
76+
77+
def test_trim_trailing_whitespace(tmp_path: Path) -> None:
78+
data_file = Path(__file__).parent.joinpath(
79+
"data/trailing-whitespace/input.txt"
80+
)
81+
working_path = tmp_path / "trailing_whitespace.txt"
82+
shutil.copyfile(data_file, working_path)
83+
trim_trailing_whitespace(working_path)
84+
85+
output = working_path.read_text()
86+
expected = (
87+
Path(__file__)
88+
.parent.joinpath("data/trailing-whitespace/output.txt")
89+
.read_text()
90+
)
91+
92+
assert output == expected
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hello world
2+
hello
3+
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
hello world
2+
hello
3+
4+

0 commit comments

Comments
 (0)