mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-16 21:40:24 +00:00
feat(config): added option for custom configuration file path
This can be used for a global .djlintrc file. closes #170
This commit is contained in:
parent
8a3c60c72b
commit
83297bd15b
7 changed files with 55 additions and 3 deletions
|
|
@ -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.
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -114,6 +114,14 @@ from .src import get_src
|
|||
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],
|
||||
|
|
@ -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(
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
@ -195,6 +205,7 @@ class Config:
|
|||
preserve_blank_lines: bool = False,
|
||||
format_css: bool = False,
|
||||
format_js: bool = False,
|
||||
configuration: Optional[str] = None,
|
||||
):
|
||||
|
||||
self.reformat = reformat
|
||||
|
|
@ -205,7 +216,7 @@ class Config:
|
|||
|
||||
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
|
||||
|
|
|
|||
5
tests/test_config/test_json/.djlint-cust
Normal file
5
tests/test_config/test_json/.djlint-cust
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"profile": "django",
|
||||
"indent": 3,
|
||||
"custom_blocks": "toc,example"
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ run::
|
|||
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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue