diff --git a/README.md b/README.md index bd29a3f..273dd85 100644 --- a/README.md +++ b/README.md @@ -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 "
" | djlint - + ``` ## Show your format diff --git a/docs/conf.py b/docs/conf.py index fb2118b..bf9550e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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 --------------------------------------------------- diff --git a/docs/djlint/changelog.rst b/docs/djlint/changelog.rst index ff07d95..21dd990 100644 --- a/docs/djlint/changelog.rst +++ b/docs/djlint/changelog.rst @@ -1,6 +1,10 @@ Changelog ========= +0.3.8 +----- +- Added support for stdin + 0.3.7 ----- - Fixed formatting on ``small``, ``dt``, and ``dd`` tags diff --git a/docs/djlint/usage.rst b/docs/djlint/usage.rst index c1a775e..210fe76 100644 --- a/docs/djlint/usage.rst +++ b/docs/djlint/usage.rst @@ -37,6 +37,15 @@ To format code run: djlint . --reformat +Stdin vs Path +------------- + +djLint also works with stdin. + +.. code:: sh + + echo "
" | djlint - + CLI Args -------- diff --git a/pyproject.toml b/pyproject.toml index f455b81..aa5e878 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] @@ -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" diff --git a/src/djlint/__init__.py b/src/djlint/__init__.py index 67b57aa..c52ed87 100644 --- a/src/djlint/__init__.py +++ b/src/djlint/__init__.py @@ -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) diff --git a/tests/test_djlint.py b/tests/test_djlint.py index 7c70d85..c59f08c 100644 --- a/tests/test_djlint.py +++ b/tests/test_djlint.py @@ -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="
") + assert result.exit_code == 0 + + def test_check(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"
") result = runner.invoke(djlint, [tmp_file.name, "--check"])