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/__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..9288f09 100644 --- a/src/djlint/reformat.py +++ b/src/djlint/reformat.py @@ -17,9 +17,10 @@ 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) + # naturalize the line breaks + compressed = compress_html(("\n").join(rawcode.splitlines()), config) expanded = expand_html(compressed, config) @@ -33,9 +34,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": + # 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: - # update the file - this_file.write_text(beautified_code, encoding="utf8") + this_file.write_bytes(beautified_code.encode("utf8")) 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