added option for stdin. closes #13

This commit is contained in:
Christopher Pickering 2021-09-16 13:36:44 +02:00
parent 925ce46f12
commit 78fea8ed4d
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
7 changed files with 42 additions and 4 deletions

View file

@ -29,6 +29,9 @@ djlint /path/file.html.j2 --check
# to reformt a directory without printing the file diff
djlint /path --reformat --quiet
# using stdin
echo "<div></div>" | djlint -
```
## Show your format

View file

@ -12,7 +12,7 @@
project = "djlint"
copyright = "2021, Riverside Healthcare"
author = "Christopher Pickering"
release = "0.3.7"
release = "0.3.8"
version = release
# -- General configuration ---------------------------------------------------

View file

@ -1,6 +1,10 @@
Changelog
=========
0.3.8
-----
- Added support for stdin
0.3.7
-----
- Fixed formatting on ``small``, ``dt``, and ``dd`` tags

View file

@ -37,6 +37,15 @@ To format code run:
djlint . --reformat
Stdin vs Path
-------------
djLint also works with stdin.
.. code:: sh
echo "<div></div>" | djlint -
CLI Args
--------

View file

@ -7,7 +7,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name="djlint"
version="0.3.7"
version="0.3.8"
description="HTML Template Linter and Formatter"
license="GPL-3.0-or-later"
authors=["Christopher Pickering <cpickering@rhc.net>"]
@ -61,4 +61,4 @@ quiet = true
[tool.pylint.messages_control]
disable = "E1120, R0914, E0401, R0912, R0916, R0913, W0104, R0801, W1404, R0902, R0903"
disable = "E1120, R0914, E0401, R0912, R0916, R0913, W0104, R0801, W1404, R0902, R0903, R1732, R0915"

View file

@ -18,6 +18,7 @@ import os
import re
import shutil
import sys
import tempfile
from concurrent.futures import ProcessPoolExecutor, as_completed
from functools import partial
from pathlib import Path
@ -187,7 +188,20 @@ def main(
"""Djlint django template files."""
config = Config(src, extension=extension, ignore=ignore, quiet=quiet)
file_list = get_src(Path(src), config)
temp_file = None
if src == "-":
stdin_stream = click.get_text_stream("stdin")
stdin_text = stdin_stream.read()
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(str.encode(stdin_text))
temp_file.seek(0)
file_list = get_src(Path(temp_file.name), config)
else:
file_list = get_src(Path(src), config)
if len(file_list) == 0:
return
@ -311,6 +325,9 @@ def main(
echo(f"\n{success_color}{success_message}{Style.RESET_ALL}\n")
if temp_file:
temp_file.close()
if bool(error_count):
sys.exit(1)

View file

@ -85,6 +85,11 @@ def test_empty_file(runner: CliRunner, tmp_file: TextIO) -> None:
assert result.exit_code == 0
def test_stdin(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["-"], input="<div></div>")
assert result.exit_code == 0
def test_check(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<div></div>")
result = runner.invoke(djlint, [tmp_file.name, "--check"])