From 7c0a2721504d489b92dbd252d42f4eea7744844b Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Thu, 19 Jan 2023 08:30:31 -0600 Subject: [PATCH 1/4] fix(formatter): preserve windows line endings Line ending from first line will now be preserved in the output. closes #502 --- src/djlint/__init__.py | 1 - src/djlint/reformat.py | 9 +++++++-- tests/test_djlint/test_djlint.py | 15 +++++++++++++++ windows.html | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 windows.html diff --git a/src/djlint/__init__.py b/src/djlint/__init__.py index 978401c..3c10356 100644 --- a/src/djlint/__init__.py +++ b/src/djlint/__init__.py @@ -180,7 +180,6 @@ def main( config.stdin = True stdin_stream = click.get_text_stream("stdin", encoding="utf8") stdin_text = stdin_stream.read() - temp_file = tempfile.NamedTemporaryFile(delete=False) temp_file.write(str.encode(stdin_text)) temp_file.seek(0) diff --git a/src/djlint/reformat.py b/src/djlint/reformat.py index 5d01dbc..47f8569 100644 --- a/src/djlint/reformat.py +++ b/src/djlint/reformat.py @@ -17,7 +17,7 @@ from .settings import Config def reformat_file(config: Config, this_file: Path) -> dict: """Reformat html file.""" - rawcode = this_file.read_text(encoding="utf8") + rawcode = this_file.read_bytes().decode("utf8") compressed = compress_html(rawcode, config) @@ -33,9 +33,14 @@ def reformat_file(config: Config, this_file: Path) -> dict: if config.format_js: beautified_code = format_js(beautified_code, config) + # preserve original line endings + line_ending = rawcode.find("\n") + if line_ending > -1 and rawcode[max(line_ending - 1, 0)] == "\r": + beautified_code = beautified_code.replace("\n", "\r\n") + if config.check is not True or config.stdin is True: # update the file - this_file.write_text(beautified_code, encoding="utf8") + this_file.write_text(beautified_code, encoding="utf8", newline="") out = { str(this_file): list( diff --git a/tests/test_djlint/test_djlint.py b/tests/test_djlint/test_djlint.py index 2bb5f5a..251713c 100644 --- a/tests/test_djlint/test_djlint.py +++ b/tests/test_djlint/test_djlint.py @@ -222,3 +222,18 @@ def test_python_call() -> None: ) assert b"python -m djlint [OPTIONS] SRC ..." in py_sub.stdout assert py_sub.returncode == 0 + + +def test_line_ending(runner: CliRunner, tmp_file: TextIO) -> None: + # write a windows line ending to file + text_in = "
\r\n" + with open(tmp_file.name, "w", encoding="utf8", newline="") as windows: + windows.write(text_in) + + # make sure line ending was still there + assert Path(tmp_file.name).read_bytes().decode("utf8") == text_in + + # check formatting + result = runner.invoke(djlint, [tmp_file.name, "--check", "--quiet"]) + + assert result.exit_code == 0 diff --git a/windows.html b/windows.html new file mode 100644 index 0000000..b6ea759 --- /dev/null +++ b/windows.html @@ -0,0 +1 @@ +
From 7a0a442fec8951484e38f44427d771f24093b1fc Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Thu, 19 Jan 2023 09:57:50 -0600 Subject: [PATCH 2/4] build(removed unused dep): removed metadata import closes #523 --- poetry.lock | 2 +- pyproject.toml | 1 - src/djlint/reformat.py | 3 +-- windows.html | 1 - 4 files changed, 2 insertions(+), 5 deletions(-) delete mode 100644 windows.html diff --git a/poetry.lock b/poetry.lock index 1d94b4f..4d60f32 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1153,4 +1153,4 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>= [metadata] lock-version = "2.0" python-versions = "^3.7.2,<4.0" -content-hash = "dcea2aee978ec2864760ff7e40d95b57f4c189e88d306bbee285c59ab43a6497" +content-hash = "eb10082144dd4998be8b75ccab144eb5791d06fedfc0c1b04bf430d3134744fa" diff --git a/pyproject.toml b/pyproject.toml index 5178496..e11d413 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,6 @@ regex = "^2022.1.18" tqdm = "^4.62.2" tomli = { version = "^2.0.1", python = "<3.11" } pathspec = "^0.10.0" -importlib-metadata = "^6.0.0" html-void-elements = "^0.1.0" html-tag-names = "^0.1.2" jsbeautifier = "^1.14.4" diff --git a/src/djlint/reformat.py b/src/djlint/reformat.py index 47f8569..a316b0b 100644 --- a/src/djlint/reformat.py +++ b/src/djlint/reformat.py @@ -39,8 +39,7 @@ def reformat_file(config: Config, this_file: Path) -> dict: beautified_code = beautified_code.replace("\n", "\r\n") if config.check is not True or config.stdin is True: - # update the file - this_file.write_text(beautified_code, encoding="utf8", newline="") + this_file.write_bytes(beautified_code.encode("utf8")) out = { str(this_file): list( diff --git a/windows.html b/windows.html deleted file mode 100644 index b6ea759..0000000 --- a/windows.html +++ /dev/null @@ -1 +0,0 @@ -
From 64eb26be3fab161a03b8118c35f1e50327a088c5 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Thu, 19 Jan 2023 10:24:37 -0600 Subject: [PATCH 3/4] fixed line ending conversion for windows --- src/djlint/reformat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/djlint/reformat.py b/src/djlint/reformat.py index a316b0b..fb5afd9 100644 --- a/src/djlint/reformat.py +++ b/src/djlint/reformat.py @@ -36,7 +36,8 @@ def reformat_file(config: Config, this_file: Path) -> dict: # preserve original line endings line_ending = rawcode.find("\n") if line_ending > -1 and rawcode[max(line_ending - 1, 0)] == "\r": - beautified_code = beautified_code.replace("\n", "\r\n") + # convert \r?\n to \r\n + beautified_code = beautified_code.replace("\r", "").replace("\n", "\r\n") if config.check is not True or config.stdin is True: this_file.write_bytes(beautified_code.encode("utf8")) From 70e4220c935be0903f6ff0bb0c8aca060c59c476 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Thu, 19 Jan 2023 11:53:56 -0600 Subject: [PATCH 4/4] naturalized line endings when beginning formatting --- src/djlint/reformat.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/djlint/reformat.py b/src/djlint/reformat.py index fb5afd9..9288f09 100644 --- a/src/djlint/reformat.py +++ b/src/djlint/reformat.py @@ -19,7 +19,8 @@ def reformat_file(config: Config, this_file: Path) -> dict: """Reformat html file.""" rawcode = this_file.read_bytes().decode("utf8") - compressed = compress_html(rawcode, config) + # naturalize the line breaks + compressed = compress_html(("\n").join(rawcode.splitlines()), config) expanded = expand_html(compressed, config)