Skip to content

Commit

Permalink
feat(config): added option for custom configuration file path
Browse files Browse the repository at this point in the history
This can be used for a global .djlintrc file.

closes #170
  • Loading branch information
christopherpickering committed Sep 7, 2022
1 parent 8a3c60c commit 83297bd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/src/docs/getting-started.md
Expand Up @@ -49,6 +49,7 @@ Options:
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration Path to global configuration file in .djlintrc format
-h, --help Show this message and exit.
```

Expand Down
1 change: 1 addition & 0 deletions docs/src/fr/docs/getting-started.md
Expand Up @@ -49,6 +49,7 @@ Options:
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration Path to global configuration file in .djlintrc format
-h, --help Show this message and exit.
```

Expand Down
1 change: 1 addition & 0 deletions docs/src/ru/docs/getting-started.md
Expand Up @@ -49,6 +49,7 @@ Options:
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration Path to global configuration file in .djlintrc format
-h, --help Show this message and exit.
```

Expand Down
10 changes: 10 additions & 0 deletions src/djlint/__init__.py
Expand Up @@ -114,6 +114,14 @@
is_flag=True,
help="Also format contents of <script> tags.",
)
@click.option(
"--configuration",
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
),
required=False,
help="Path to global configuration file in .djlintrc format",
)
@colorama_text(autoreset=True)
def main(
src: List[str],
Expand All @@ -132,6 +140,7 @@ def main(
preserve_blank_lines: bool,
format_css: bool,
format_js: bool,
configuration: Optional[str],
) -> None:
"""djLint · HTML template linter and formatter."""
config = Config(
Expand All @@ -151,6 +160,7 @@ def main(
preserve_blank_lines=preserve_blank_lines,
format_css=format_css,
format_js=format_js,
configuration=configuration,
)

temp_file = None
Expand Down
15 changes: 13 additions & 2 deletions src/djlint/settings.py
Expand Up @@ -96,10 +96,20 @@ def find_djlint_rules(root: Path) -> Optional[Path]:
return None


def load_project_settings(src: Path) -> Dict:
def load_project_settings(src: Path, config: Optional[str]) -> Dict:
"""Load djlint config from pyproject.toml."""

djlint_content: Dict = {}

if config:
try:
return json.loads(Path(config).resolve().read_text(encoding="utf8"))
# pylint: disable=broad-except
except BaseException:
logger.info(
"Failed to load config file. Ensure file exists and is in json format."
)

pyproject_file = find_pyproject(src)

if pyproject_file:
Expand Down Expand Up @@ -195,6 +205,7 @@ def __init__(
preserve_blank_lines: bool = False,
format_css: bool = False,
format_js: bool = False,
configuration: Optional[str] = None,
):

self.reformat = reformat
Expand All @@ -205,7 +216,7 @@ def __init__(

self.project_root = find_project_root(Path(src))

djlint_settings = load_project_settings(self.project_root)
djlint_settings = load_project_settings(self.project_root, configuration)

self.gitignore = load_gitignore(self.project_root)
# custom configuration options
Expand Down
5 changes: 5 additions & 0 deletions tests/test_config/test_json/.djlint-cust
@@ -0,0 +1,5 @@
{
"profile": "django",
"indent": 3,
"custom_blocks": "toc,example"
}
25 changes: 24 additions & 1 deletion tests/test_config/test_json/test_config.py
Expand Up @@ -5,7 +5,7 @@
pytest tests/test_config/test_json/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_config/test_json/test_config.py::test_config
pytest tests/test_config/test_json/test_config.py::test_custom_config
"""
# pylint: disable=C0116
Expand All @@ -29,3 +29,26 @@ def test_config(runner: CliRunner) -> None:
in result.output
)
assert result.exit_code == 1


def test_custom_config(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
"tests/test_config/test_json/html.html",
"--check",
"--configuration",
"tests/test_config/test_json/.djlint-cust",
],
)

assert (
"""-{% example stuff %}<p>this is a long paragraph</p>{% endexample %}
+{% example stuff %}
+ <p>this is a long paragraph</p>
+{% endexample %}
"""
in result.output
)

assert result.exit_code == 1

0 comments on commit 83297bd

Please sign in to comment.