Merge pull request #525 from Riverside-Healthcare/dev

fix(formatter): preserve windows line endings
This commit is contained in:
sur.la.route 2023-01-19 12:04:46 -06:00 committed by GitHub
commit 3183585d40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 7 deletions

2
poetry.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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)

View file

@ -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(

View file

@ -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 = "<div></div>\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