djlint 1.28.0 → 1.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.coveragerc +9 -0
- package/.editorconfig +21 -0
- package/.flake8 +0 -0
- package/.github/FUNDING.yml +2 -0
- package/.github/ISSUE_TEMPLATE/config.yml +6 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +19 -0
- package/.github/ISSUE_TEMPLATE/formatter_but_report.md +30 -0
- package/.github/ISSUE_TEMPLATE/linter_bug_report.md +34 -0
- package/.github/ISSUE_TEMPLATE/translation.md +14 -0
- package/.github/config.yml +4 -0
- package/.github/pull_request_template.md +10 -0
- package/.github/workflows/cancel.yml +17 -0
- package/.github/workflows/demo.yml +98 -0
- package/.github/workflows/issues.yml +23 -0
- package/.github/workflows/linkcheck.yaml +21 -0
- package/.github/workflows/lint.yml +25 -0
- package/.github/workflows/release.yml +57 -0
- package/.github/workflows/test.yml +85 -0
- package/.pre-commit-config.yaml +45 -0
- package/.pre-commit-hooks.yaml +71 -0
- package/.prettierignore +7 -0
- package/.prettierrc +11 -0
- package/.releaserc +16 -0
- package/CHANGELOG.md +13 -0
- package/MANIFEST.in +2 -0
- package/build_demo.sh +17 -0
- package/docs/.eleventy.js +425 -0
- package/docs/.eleventyignore +2 -0
- package/docs/.prettierignore +3 -0
- package/docs/.prettierrc +11 -0
- package/docs/package.json +73 -0
- package/docs/readme.md +17 -0
- package/docs/src/_data/configuration.json +646 -0
- package/docs/src/_data/eleventyComputed.js +22 -0
- package/docs/src/_data/i18n/index.js +159 -0
- package/docs/src/_data/js.js +7 -0
- package/docs/src/_data/locales.js +17 -0
- package/docs/src/_data/site.json +8 -0
- package/docs/src/_includes/demo.njk +157 -0
- package/docs/src/_includes/docs_layout.njk +34 -0
- package/docs/src/_includes/foot.njk +26 -0
- package/docs/src/_includes/index.njk +187 -0
- package/docs/src/_includes/languages_layout.njk +11 -0
- package/docs/src/_includes/layout.njk +111 -0
- package/docs/src/_includes/nav.njk +85 -0
- package/docs/src/_utils/minify-html.js +14 -0
- package/docs/src/_utils/rollupper.js +91 -0
- package/docs/src/_utils/scripts.11ty.js +27 -0
- package/docs/src/demo.njk +4 -0
- package/docs/src/docs/best-practices.md +78 -0
- package/docs/src/docs/changelog.md +452 -0
- package/docs/src/docs/configuration.md +145 -0
- package/docs/src/docs/docs.json +4 -0
- package/docs/src/docs/formatter.md +82 -0
- package/docs/src/docs/getting-started.md +126 -0
- package/docs/src/docs/ignoring-code.md +75 -0
- package/docs/src/docs/integrations.md +120 -0
- package/docs/src/docs/languages/angular.md +31 -0
- package/docs/src/docs/languages/django.md +40 -0
- package/docs/src/docs/languages/golang.md +31 -0
- package/docs/src/docs/languages/handlebars.md +31 -0
- package/docs/src/docs/languages/jinja.md +39 -0
- package/docs/src/docs/languages/languages.json +3 -0
- package/docs/src/docs/languages/mustache.md +31 -0
- package/docs/src/docs/languages/nunjucks.md +39 -0
- package/docs/src/docs/languages/twig.md +41 -0
- package/docs/src/docs/linter.md +199 -0
- package/docs/src/fr/demo.njk +4 -0
- package/docs/src/fr/docs/best-practices.md +78 -0
- package/docs/src/fr/docs/changelog.md +452 -0
- package/docs/src/fr/docs/configuration.md +144 -0
- package/docs/src/fr/docs/docs.json +3 -0
- package/docs/src/fr/docs/formatter.md +83 -0
- package/docs/src/fr/docs/getting-started.md +127 -0
- package/docs/src/fr/docs/ignoring-code.md +75 -0
- package/docs/src/fr/docs/integrations.md +100 -0
- package/docs/src/fr/docs/languages/angular.md +31 -0
- package/docs/src/fr/docs/languages/django.md +31 -0
- package/docs/src/fr/docs/languages/golang.md +31 -0
- package/docs/src/fr/docs/languages/handlebars.md +31 -0
- package/docs/src/fr/docs/languages/jinja.md +31 -0
- package/docs/src/fr/docs/languages/languages.json +3 -0
- package/docs/src/fr/docs/languages/mustache.md +31 -0
- package/docs/src/fr/docs/languages/nunjucks.md +31 -0
- package/docs/src/fr/docs/languages/twig.md +31 -0
- package/docs/src/fr/docs/linter.md +207 -0
- package/docs/src/fr/fr.json +4 -0
- package/docs/src/fr/index.njk +4 -0
- package/docs/src/index.njk +4 -0
- package/docs/src/lib/generate-content-hash.js +13 -0
- package/docs/src/robots.txt +4 -0
- package/docs/src/ru/demo.njk +4 -0
- package/docs/src/ru/docs/best-practices.md +78 -0
- package/docs/src/ru/docs/changelog.md +453 -0
- package/docs/src/ru/docs/configuration.md +142 -0
- package/docs/src/ru/docs/docs.json +3 -0
- package/docs/src/ru/docs/formatter.md +82 -0
- package/docs/src/ru/docs/getting-started.md +127 -0
- package/docs/src/ru/docs/ignoring-code.md +75 -0
- package/docs/src/ru/docs/integrations.md +100 -0
- package/docs/src/ru/docs/languages/angular.md +31 -0
- package/docs/src/ru/docs/languages/django.md +31 -0
- package/docs/src/ru/docs/languages/golang.md +31 -0
- package/docs/src/ru/docs/languages/handlebars.md +31 -0
- package/docs/src/ru/docs/languages/jinja.md +31 -0
- package/docs/src/ru/docs/languages/languages.json +3 -0
- package/docs/src/ru/docs/languages/mustache.md +31 -0
- package/docs/src/ru/docs/languages/nunjucks.md +31 -0
- package/docs/src/ru/docs/languages/twig.md +31 -0
- package/docs/src/ru/docs/linter.md +126 -0
- package/docs/src/ru/index.njk +4 -0
- package/docs/src/ru/ru.json +4 -0
- package/docs/src/sitemap.njk +16 -0
- package/docs/src/static/css/site.scss +467 -0
- package/docs/src/static/font/fontawesome/stylesheet.scss +78 -0
- package/docs/src/static/img/apple-touch-icon.png +0 -0
- package/docs/src/static/img/demo-min.gif +0 -0
- package/docs/src/static/img/demo.gif +0 -0
- package/docs/src/static/img/demo.webm +0 -0
- package/docs/src/static/img/favicon-16x16.png +0 -0
- package/docs/src/static/img/favicon-32x32.png +0 -0
- package/docs/src/static/img/favicon.ico +0 -0
- package/docs/src/static/img/icon-square.indd +0 -0
- package/docs/src/static/img/icon-square.png +0 -0
- package/docs/src/static/img/icon.png +0 -0
- package/docs/src/static/img/logo-192x192.png +0 -0
- package/docs/src/static/img/logo-512x512.png +0 -0
- package/docs/src/static/img/logos/angular.png +0 -0
- package/docs/src/static/img/logos/django.png +0 -0
- package/docs/src/static/img/logos/golang.png +0 -0
- package/docs/src/static/img/logos/handlebars.png +0 -0
- package/docs/src/static/img/logos/jinja.png +0 -0
- package/docs/src/static/img/logos/mustache.png +0 -0
- package/docs/src/static/img/logos/nunjucks.png +0 -0
- package/docs/src/static/img/logos/twig.png +0 -0
- package/docs/src/static/img/old_demo.gif +0 -0
- package/docs/src/static/img/style.css +122 -0
- package/docs/src/static/js/animate.js +69 -0
- package/docs/src/static/js/editor.js +171 -0
- package/docs/src/static/js/hamburger.js +19 -0
- package/docs/src/static/js/modal.js +47 -0
- package/docs/src/static/js/worker.js +142 -0
- package/docs/src/static/py/EditorConfig-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/PyYAML-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/click-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/colorama-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/cssbeautifier-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/djlint-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/html_tag_names-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/html_void_elements-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/jsbeautifier-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/json5-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/pathspec-99-py3-none-any.whl +0 -0
- package/docs/src/static/py/regex-2023.5.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl +0 -0
- package/docs/src/static/py/six-1.16.0-py2.py3-none-any.whl +0 -0
- package/docs/src/static/py/tomli-2.0.1-py3-none-any.whl +0 -0
- package/docs/src/static/py/tqdm-4.65.0-py3-none-any.whl +0 -0
- package/icon.xcf +0 -0
- package/package.json +4 -33
- package/poetry.lock +835 -0
- package/pyproject.toml +100 -0
- package/renovate.json +44 -0
- package/scripts/updateVersion.js +16 -0
- package/src/djlint/__init__.py +451 -0
- package/src/djlint/__main__.py +4 -0
- package/src/djlint/formatter/__init__.py +1 -0
- package/src/djlint/formatter/attributes.py +212 -0
- package/src/djlint/formatter/compress.py +64 -0
- package/src/djlint/formatter/condense.py +221 -0
- package/src/djlint/formatter/css.py +60 -0
- package/src/djlint/formatter/expand.py +111 -0
- package/src/djlint/formatter/indent.py +418 -0
- package/src/djlint/formatter/js.py +58 -0
- package/src/djlint/helpers.py +321 -0
- package/src/djlint/lint.py +128 -0
- package/src/djlint/output.py +220 -0
- package/src/djlint/reformat.py +63 -0
- package/src/djlint/rules/H025.py +65 -0
- package/src/djlint/rules.yaml +279 -0
- package/src/djlint/settings.py +969 -0
- package/src/djlint/src.py +91 -0
- package/tests/__init__.py +0 -0
- package/tests/conftest.py +239 -0
- package/tests/test_cli.py +71 -0
- package/tests/test_config/__init__.py +0 -0
- package/tests/test_config/test_blank_line_after_tag.py +135 -0
- package/tests/test_config/test_blank_line_before_tag.py +141 -0
- package/tests/test_config/test_close_void_tags.py +31 -0
- package/tests/test_config/test_custom_blocks.py +56 -0
- package/tests/test_config/test_custom_html.py +45 -0
- package/tests/test_config/test_djlintrc/.djlintrc +38 -0
- package/tests/test_config/test_djlintrc/__init__.py +0 -0
- package/tests/test_config/test_djlintrc/test_config.py +44 -0
- package/tests/test_config/test_djlintrc_custom/.djlint-cust +38 -0
- package/tests/test_config/test_djlintrc_custom/.djlintrc +2 -0
- package/tests/test_config/test_djlintrc_custom/__init__.py +0 -0
- package/tests/test_config/test_djlintrc_custom/test_config.py +48 -0
- package/tests/test_config/test_excludes/__init__.py +0 -0
- package/tests/test_config/test_excludes/excluded.html +1 -0
- package/tests/test_config/test_excludes/foo/excluded.html +1 -0
- package/tests/test_config/test_excludes/html.html +1 -0
- package/tests/test_config/test_excludes/pyproject.toml +3 -0
- package/tests/test_config/test_excludes/test_config.py +30 -0
- package/tests/test_config/test_extension/__init__.py +0 -0
- package/tests/test_config/test_extension/html.test +1 -0
- package/tests/test_config/test_extension/pyproject.toml +3 -0
- package/tests/test_config/test_extension/test_config.py +27 -0
- package/tests/test_config/test_files/.djlintrc_global +9 -0
- package/tests/test_config/test_files/test.html +3 -0
- package/tests/test_config/test_files/test_config.py +123 -0
- package/tests/test_config/test_files/test_two.html +1 -0
- package/tests/test_config/test_format_attribute_template_tags.py +280 -0
- package/tests/test_config/test_format_css.py +197 -0
- package/tests/test_config/test_format_js.py +220 -0
- package/tests/test_config/test_gitignore/__init__.py +0 -0
- package/tests/test_config/test_gitignore/html_one.html +1 -0
- package/tests/test_config/test_gitignore/html_two.html +1 -0
- package/tests/test_config/test_gitignore/test_config.py +142 -0
- package/tests/test_config/test_ignore/__init__.py +0 -0
- package/tests/test_config/test_ignore/html.html +1 -0
- package/tests/test_config/test_ignore/html_two.html +12 -0
- package/tests/test_config/test_ignore/pyproject.toml +3 -0
- package/tests/test_config/test_ignore/test_config.py +32 -0
- package/tests/test_config/test_ignore_blocks.py +45 -0
- package/tests/test_config/test_ignore_case.py +39 -0
- package/tests/test_config/test_indent.py +63 -0
- package/tests/test_config/test_linter_output_format/__init__.py +0 -0
- package/tests/test_config/test_linter_output_format/html-one.html +1 -0
- package/tests/test_config/test_linter_output_format/html-two.html +1 -0
- package/tests/test_config/test_linter_output_format/pyproject.toml +3 -0
- package/tests/test_config/test_linter_output_format/test_config.py +34 -0
- package/tests/test_config/test_max_attribute_length.py +100 -0
- package/tests/test_config/test_max_line_length.py +94 -0
- package/tests/test_config/test_per_file_ignores/__init__.py +0 -0
- package/tests/test_config/test_per_file_ignores/html.html +2 -0
- package/tests/test_config/test_per_file_ignores/pyproject.toml +2 -0
- package/tests/test_config/test_per_file_ignores/test_config.py +25 -0
- package/tests/test_config/test_pragmas/__init__.py +0 -0
- package/tests/test_config/test_pragmas/html_five.html +2 -0
- package/tests/test_config/test_pragmas/html_four.html +5 -0
- package/tests/test_config/test_pragmas/html_one.html +1 -0
- package/tests/test_config/test_pragmas/html_six.html +2 -0
- package/tests/test_config/test_pragmas/html_three.html +4 -0
- package/tests/test_config/test_pragmas/html_two.html +2 -0
- package/tests/test_config/test_pragmas/pyproject.toml +3 -0
- package/tests/test_config/test_pragmas/test_config.py +142 -0
- package/tests/test_config/test_preserve_blank_lines.py +109 -0
- package/tests/test_config/test_preserve_leading_space.py +159 -0
- package/tests/test_config/test_profile/__init__.py +0 -0
- package/tests/test_config/test_profile/html.html +2 -0
- package/tests/test_config/test_profile/pyproject.toml +3 -0
- package/tests/test_config/test_profile/test_config.py +64 -0
- package/tests/test_config/test_pyproject/__init__.py +0 -0
- package/tests/test_config/test_pyproject/blank.html +0 -0
- package/tests/test_config/test_pyproject/pyproject.toml +36 -0
- package/tests/test_config/test_pyproject/test_config.py +44 -0
- package/tests/test_django/__init__.py +0 -0
- package/tests/test_django/test_asset.py +32 -0
- package/tests/test_django/test_autoescape.py +25 -0
- package/tests/test_django/test_block.py +29 -0
- package/tests/test_django/test_blocktrans.py +236 -0
- package/tests/test_django/test_blocktrans_trimmed.py +283 -0
- package/tests/test_django/test_comments.py +164 -0
- package/tests/test_django/test_filter.py +30 -0
- package/tests/test_django/test_for.py +34 -0
- package/tests/test_django/test_if.py +34 -0
- package/tests/test_django/test_ifchanged.py +37 -0
- package/tests/test_django/test_include.py +24 -0
- package/tests/test_django/test_load.py +24 -0
- package/tests/test_django/test_quoted.py +34 -0
- package/tests/test_django/test_spaceless.py +30 -0
- package/tests/test_django/test_tag_spaces.py +35 -0
- package/tests/test_django/test_templatetag.py +40 -0
- package/tests/test_django/test_verbatim.py +24 -0
- package/tests/test_django/test_with.py +32 -0
- package/tests/test_djlint/-.html +1 -0
- package/tests/test_djlint/__init__.py +0 -0
- package/tests/test_djlint/bad.html +6 -0
- package/tests/test_djlint/bad.html.dj +23 -0
- package/tests/test_djlint/bad.html.dj2 +2 -0
- package/tests/test_djlint/multiple_files/a/a.html +1 -0
- package/tests/test_djlint/multiple_files/b/b1.html +1 -0
- package/tests/test_djlint/multiple_files/b/b2.html +1 -0
- package/tests/test_djlint/test_djlint.py +242 -0
- package/tests/test_golang/__init__.py +0 -0
- package/tests/test_golang/test_if.py +24 -0
- package/tests/test_golang/test_range.py +24 -0
- package/tests/test_handlebars/__init__.py +0 -0
- package/tests/test_handlebars/test_each.py +34 -0
- package/tests/test_handlebars/test_else.py +24 -0
- package/tests/test_handlebars/test_with.py +24 -0
- package/tests/test_html/__init__.py +0 -0
- package/tests/test_html/test_alpinejs.py +65 -0
- package/tests/test_html/test_attributes.py +935 -0
- package/tests/test_html/test_aurelia.py +24 -0
- package/tests/test_html/test_basics.py +905 -0
- package/tests/test_html/test_bracket_same_line.py +177 -0
- package/tests/test_html/test_case.py +81 -0
- package/tests/test_html/test_cdata.py +34 -0
- package/tests/test_html/test_comments.py +333 -0
- package/tests/test_html/test_css.py +193 -0
- package/tests/test_html/test_doctype_declarations.py +202 -0
- package/tests/test_html/test_ignored.py +193 -0
- package/tests/test_html/test_interpolation.py +56 -0
- package/tests/test_html/test_line_break_after_multiline_tag.py +95 -0
- package/tests/test_html/test_next_line_empty.py +53 -0
- package/tests/test_html/test_selfclosing.py +55 -0
- package/tests/test_html/test_single_attribute_per_line.py +87 -0
- package/tests/test_html/test_srcset.py +43 -0
- package/tests/test_html/test_svg.py +104 -0
- package/tests/test_html/test_symbol_entities.py +34 -0
- package/tests/test_html/test_tag_code.py +24 -0
- package/tests/test_html/test_tag_dd.py +24 -0
- package/tests/test_html/test_tag_details_summary.py +24 -0
- package/tests/test_html/test_tag_dt.py +24 -0
- package/tests/test_html/test_tag_fig_caption.py +29 -0
- package/tests/test_html/test_tag_hr.py +29 -0
- package/tests/test_html/test_tag_picture.py +32 -0
- package/tests/test_html/test_tag_pre.py +45 -0
- package/tests/test_html/test_tag_script.py +516 -0
- package/tests/test_html/test_tag_small.py +24 -0
- package/tests/test_html/test_tag_span.py +119 -0
- package/tests/test_html/test_tag_textarea.py +82 -0
- package/tests/test_html/test_tags.py +842 -0
- package/tests/test_html/test_text.py +34 -0
- package/tests/test_html/test_whitespace.py +477 -0
- package/tests/test_html/test_yaml.py +156 -0
- package/tests/test_jinja/__init__.py +0 -0
- package/tests/test_jinja/test_call.py +34 -0
- package/tests/test_jinja/test_parenthesis.py +24 -0
- package/tests/test_linter/__init__.py +0 -0
- package/tests/test_linter/test_custom_rules/.djlint_rules.yaml +6 -0
- package/tests/test_linter/test_custom_rules/__init__.py +0 -0
- package/tests/test_linter/test_custom_rules/html.html +1 -0
- package/tests/test_linter/test_custom_rules/pyproject.toml +1 -0
- package/tests/test_linter/test_custom_rules/test_linter.py +29 -0
- package/tests/test_linter/test_custom_rules_bad/.djlint_rules.yaml +24 -0
- package/tests/test_linter/test_custom_rules_bad/__init__.py +0 -0
- package/tests/test_linter/test_custom_rules_bad/html.html +1 -0
- package/tests/test_linter/test_custom_rules_bad/pyproject.toml +1 -0
- package/tests/test_linter/test_custom_rules_bad/test_linter.py +29 -0
- package/tests/test_linter/test_django_linter.py +476 -0
- package/tests/test_linter/test_h005.py +45 -0
- package/tests/test_linter/test_h006.py +81 -0
- package/tests/test_linter/test_h007.py +45 -0
- package/tests/test_linter/test_h008.py +60 -0
- package/tests/test_linter/test_h009.py +45 -0
- package/tests/test_linter/test_h010.py +44 -0
- package/tests/test_linter/test_h037.py +150 -0
- package/tests/test_linter/test_ignore_rules.py +45 -0
- package/tests/test_linter/test_jinja_linter.py +227 -0
- package/tests/test_linter/test_linter.py +691 -0
- package/tests/test_linter/test_nunjucks_linter.py +59 -0
- package/tests/test_linter/test_python_module_rules/.djlint_rules.yaml +4 -0
- package/tests/test_linter/test_python_module_rules/__init__.py +0 -0
- package/tests/test_linter/test_python_module_rules/html_bad.html +3 -0
- package/tests/test_linter/test_python_module_rules/html_ok.html +3 -0
- package/tests/test_linter/test_python_module_rules/my_module.py +32 -0
- package/tests/test_linter/test_python_module_rules/pyproject.toml +1 -0
- package/tests/test_linter/test_python_module_rules/test_linter.py +26 -0
- package/tests/test_nunjucks/__init__.py +0 -0
- package/tests/test_nunjucks/test_functions.py +94 -0
- package/tests/test_nunjucks/test_macros.py +41 -0
- package/tests/test_nunjucks/test_set.py +206 -0
- package/tests/test_nunjucks/test_spaceless.py +24 -0
- package/tests/test_twig/__init__.py +0 -0
- package/tests/test_twig/test_comments.py +51 -0
- package/tox.ini +48 -0
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
"""djLint add indentation to html."""
|
|
2
|
+
|
|
3
|
+
from functools import partial
|
|
4
|
+
|
|
5
|
+
import json5 as json
|
|
6
|
+
import regex as re
|
|
7
|
+
|
|
8
|
+
from ..helpers import (
|
|
9
|
+
inside_ignored_block,
|
|
10
|
+
is_ignored_block_closing,
|
|
11
|
+
is_ignored_block_opening,
|
|
12
|
+
is_safe_closing_tag,
|
|
13
|
+
)
|
|
14
|
+
from ..settings import Config
|
|
15
|
+
from .attributes import format_attributes
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def indent_html(rawcode: str, config: Config) -> str:
|
|
19
|
+
"""Indent raw code."""
|
|
20
|
+
if config.profile not in ["handlebars", "golang"]:
|
|
21
|
+
# we can try to fix template tags. ignore handlebars
|
|
22
|
+
# this should be done before indenting to line length
|
|
23
|
+
# calc is preserved.
|
|
24
|
+
|
|
25
|
+
def fix_tag_spacing(html: str, match: re.Match) -> str:
|
|
26
|
+
if inside_ignored_block(config, html, match):
|
|
27
|
+
return match.group()
|
|
28
|
+
|
|
29
|
+
return f"{match.group(1)} {match.group(2)} {match.group(3)}"
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
We should have tags like this:
|
|
33
|
+
{{ tag }}
|
|
34
|
+
{%- tag atrib -%}
|
|
35
|
+
"""
|
|
36
|
+
func = partial(fix_tag_spacing, rawcode)
|
|
37
|
+
|
|
38
|
+
rawcode = re.sub(
|
|
39
|
+
r"({%-?\+?)[ ]*?(\w(?:(?!%}).)*?)[ ]*?(\+?-?%})", func, rawcode
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
rawcode = re.sub(r"({{)[ ]*?(\w(?:(?!}}).)*?)[ ]*?(\+?-?}})", func, rawcode)
|
|
43
|
+
|
|
44
|
+
elif config.profile == "handlebars":
|
|
45
|
+
|
|
46
|
+
def fix_handlebars_template_tags(html: str, match: re.Match) -> str:
|
|
47
|
+
if inside_ignored_block(config, html, match):
|
|
48
|
+
return match.group()
|
|
49
|
+
|
|
50
|
+
return f"{match.group(1)} {match.group(2)}"
|
|
51
|
+
|
|
52
|
+
func = partial(fix_handlebars_template_tags, rawcode)
|
|
53
|
+
# handlebars templates
|
|
54
|
+
rawcode = re.sub(r"({{#(?:each|if).+?[^ ])(}})", func, rawcode)
|
|
55
|
+
|
|
56
|
+
rawcode_flat_list = re.split("\n", rawcode)
|
|
57
|
+
|
|
58
|
+
indent = config.indent
|
|
59
|
+
|
|
60
|
+
beautified_code = ""
|
|
61
|
+
indent_level = 0
|
|
62
|
+
in_set_tag = False
|
|
63
|
+
is_raw_first_line = False
|
|
64
|
+
is_block_raw = False
|
|
65
|
+
|
|
66
|
+
slt_html = config.indent_html_tags
|
|
67
|
+
|
|
68
|
+
# here using all tags cause we allow empty tags on one line
|
|
69
|
+
always_self_closing_html = config.always_self_closing_html_tags
|
|
70
|
+
|
|
71
|
+
# here using all tags cause we allow empty tags on one line
|
|
72
|
+
slt_template = config.optional_single_line_template_tags
|
|
73
|
+
|
|
74
|
+
# nested ignored blocks..
|
|
75
|
+
ignored_level = 0
|
|
76
|
+
|
|
77
|
+
for item in rawcode_flat_list:
|
|
78
|
+
# if a raw tag first line
|
|
79
|
+
if not is_block_raw and is_ignored_block_opening(config, item):
|
|
80
|
+
is_raw_first_line = True
|
|
81
|
+
|
|
82
|
+
# if a raw tag then start ignoring
|
|
83
|
+
if is_ignored_block_opening(config, item):
|
|
84
|
+
is_block_raw = True
|
|
85
|
+
ignored_level += 1
|
|
86
|
+
|
|
87
|
+
if is_safe_closing_tag(config, item):
|
|
88
|
+
ignored_level -= 1
|
|
89
|
+
ignored_level = max(ignored_level, 0)
|
|
90
|
+
if is_block_raw is True and ignored_level == 0:
|
|
91
|
+
is_block_raw = False
|
|
92
|
+
|
|
93
|
+
if (
|
|
94
|
+
re.findall(
|
|
95
|
+
rf"^\s*?(?:{config.ignored_inline_blocks})",
|
|
96
|
+
item,
|
|
97
|
+
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
|
98
|
+
)
|
|
99
|
+
and is_block_raw is False
|
|
100
|
+
):
|
|
101
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
102
|
+
|
|
103
|
+
# if a one-line, inline tag, just process it, only if line starts w/ it
|
|
104
|
+
# or if it is trailing text
|
|
105
|
+
|
|
106
|
+
elif (
|
|
107
|
+
(
|
|
108
|
+
re.findall(
|
|
109
|
+
re.compile(
|
|
110
|
+
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text
|
|
111
|
+
(?:
|
|
112
|
+
(?:<({slt_html})>)(?:.*?)(?:</(?:\1)>) # <span>stuff</span> >>>> match 1
|
|
113
|
+
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\2)>) # <span stuff>stuff</span> >>> match 2
|
|
114
|
+
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|
|
115
|
+
|(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|
|
116
|
+
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\3)[ ]+?.*?%}}) # >>> match 3
|
|
117
|
+
|{config.ignored_inline_blocks}
|
|
118
|
+
)[ \t]*?
|
|
119
|
+
(?:
|
|
120
|
+
.*? # anything
|
|
121
|
+
(?: # followed by another slt
|
|
122
|
+
(?:<({slt_html})>)(?:.*?)(?:</(?:\4)>) # <span>stuff</span> >>>> match 1
|
|
123
|
+
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\5)>) # <span stuff>stuff</span> >>> match 2
|
|
124
|
+
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|
|
125
|
+
|(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|
|
126
|
+
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\6)[ ]+?.*?%}}) # >>> match 3
|
|
127
|
+
|{config.ignored_inline_blocks}
|
|
128
|
+
)[ \t]*?
|
|
129
|
+
)*? # optional of course
|
|
130
|
+
[^<]*?$ # with no other tags following until end of line
|
|
131
|
+
""",
|
|
132
|
+
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
|
133
|
+
),
|
|
134
|
+
item,
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
and is_block_raw is False
|
|
138
|
+
):
|
|
139
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
140
|
+
|
|
141
|
+
# closing set tag
|
|
142
|
+
elif (
|
|
143
|
+
re.search(
|
|
144
|
+
re.compile(
|
|
145
|
+
r"^(?!.*\{\%).*%\}.*$",
|
|
146
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
147
|
+
),
|
|
148
|
+
item,
|
|
149
|
+
)
|
|
150
|
+
and is_block_raw is False
|
|
151
|
+
and in_set_tag is True
|
|
152
|
+
):
|
|
153
|
+
indent_level = max(indent_level - 1, 0)
|
|
154
|
+
in_set_tag = False
|
|
155
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
156
|
+
|
|
157
|
+
# closing curly brace inside a set tag
|
|
158
|
+
elif (
|
|
159
|
+
re.search(
|
|
160
|
+
re.compile(
|
|
161
|
+
r"^[ ]*}|^[ ]*]",
|
|
162
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
163
|
+
),
|
|
164
|
+
item,
|
|
165
|
+
)
|
|
166
|
+
and is_block_raw is False
|
|
167
|
+
and in_set_tag is True
|
|
168
|
+
):
|
|
169
|
+
indent_level = max(indent_level - 1, 0)
|
|
170
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
171
|
+
|
|
172
|
+
# if unindent, move left
|
|
173
|
+
elif (
|
|
174
|
+
re.search(
|
|
175
|
+
config.tag_unindent,
|
|
176
|
+
item,
|
|
177
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
178
|
+
)
|
|
179
|
+
and is_block_raw is False
|
|
180
|
+
and not is_safe_closing_tag(config, item)
|
|
181
|
+
# and not ending in a slt like <span><strong></strong>.
|
|
182
|
+
and not re.findall(
|
|
183
|
+
rf"(<({slt_html})>)(.*?)(</(\2)>[^<]*?$)",
|
|
184
|
+
item,
|
|
185
|
+
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
|
186
|
+
)
|
|
187
|
+
and not re.findall(
|
|
188
|
+
rf"(<({slt_html})\\b[^>]+?>)(.*?)(</(\2)>[^<]*?$)",
|
|
189
|
+
item,
|
|
190
|
+
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
|
191
|
+
)
|
|
192
|
+
):
|
|
193
|
+
# block to catch inline block followed by a non-break tag
|
|
194
|
+
if (
|
|
195
|
+
len(
|
|
196
|
+
re.findall(
|
|
197
|
+
rf"(^<({slt_html})>)(.*?)(</(\2)>)",
|
|
198
|
+
item,
|
|
199
|
+
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
|
200
|
+
)
|
|
201
|
+
or re.findall(
|
|
202
|
+
re.compile(
|
|
203
|
+
rf"(^<({slt_html})\b[^>]+?>)(.*?)(</(\2)>)",
|
|
204
|
+
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
|
205
|
+
),
|
|
206
|
+
item,
|
|
207
|
+
)
|
|
208
|
+
)
|
|
209
|
+
> 0
|
|
210
|
+
):
|
|
211
|
+
# unindent after instead of before
|
|
212
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
213
|
+
indent_level = max(indent_level - 1, 0)
|
|
214
|
+
else:
|
|
215
|
+
indent_level = max(indent_level - 1, 0)
|
|
216
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
217
|
+
|
|
218
|
+
elif (
|
|
219
|
+
re.search(
|
|
220
|
+
r"^" + str(config.tag_unindent_line),
|
|
221
|
+
item,
|
|
222
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
223
|
+
)
|
|
224
|
+
and is_block_raw is False
|
|
225
|
+
):
|
|
226
|
+
tmp = (indent * (indent_level - 1)) + item + "\n"
|
|
227
|
+
|
|
228
|
+
# if indent, move right
|
|
229
|
+
|
|
230
|
+
# opening set tag
|
|
231
|
+
elif (
|
|
232
|
+
re.search(
|
|
233
|
+
re.compile(
|
|
234
|
+
r"^([ ]*{%[ ]*?set)(?!.*%}).*$",
|
|
235
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
236
|
+
),
|
|
237
|
+
item,
|
|
238
|
+
)
|
|
239
|
+
and is_block_raw is False
|
|
240
|
+
and in_set_tag is False
|
|
241
|
+
):
|
|
242
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
243
|
+
indent_level = indent_level + 1
|
|
244
|
+
in_set_tag = True
|
|
245
|
+
|
|
246
|
+
# opening curly brace inside a set tag
|
|
247
|
+
elif (
|
|
248
|
+
re.search(
|
|
249
|
+
re.compile(
|
|
250
|
+
r"(\{(?![^{}]*%[}\s])(?=[^{}]*$)|\[(?=[^\]]*$))",
|
|
251
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
252
|
+
),
|
|
253
|
+
item,
|
|
254
|
+
)
|
|
255
|
+
and is_block_raw is False
|
|
256
|
+
and in_set_tag is True
|
|
257
|
+
):
|
|
258
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
259
|
+
indent_level = indent_level + 1
|
|
260
|
+
|
|
261
|
+
elif (
|
|
262
|
+
re.search(
|
|
263
|
+
re.compile(
|
|
264
|
+
r"^(?:" + str(config.tag_indent) + r")",
|
|
265
|
+
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
|
|
266
|
+
),
|
|
267
|
+
item,
|
|
268
|
+
)
|
|
269
|
+
and is_block_raw is False
|
|
270
|
+
):
|
|
271
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
272
|
+
indent_level = indent_level + 1
|
|
273
|
+
|
|
274
|
+
elif is_raw_first_line is True or (
|
|
275
|
+
is_safe_closing_tag(config, item) and is_block_raw is False
|
|
276
|
+
):
|
|
277
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
278
|
+
|
|
279
|
+
elif is_block_raw is True or not item.strip():
|
|
280
|
+
tmp = item + "\n"
|
|
281
|
+
|
|
282
|
+
# otherwise, just leave same level
|
|
283
|
+
elif not config.preserve_leading_space:
|
|
284
|
+
# if we are not trying to preserve indenting
|
|
285
|
+
# on text, the add it now.
|
|
286
|
+
tmp = (indent * indent_level) + item + "\n"
|
|
287
|
+
else:
|
|
288
|
+
tmp = item + "\n"
|
|
289
|
+
|
|
290
|
+
# if a opening raw tag then start ignoring.. only if there is no closing tag
|
|
291
|
+
# on the same line
|
|
292
|
+
if is_ignored_block_opening(config, item):
|
|
293
|
+
is_block_raw = True
|
|
294
|
+
is_raw_first_line = False
|
|
295
|
+
|
|
296
|
+
# if a normal tag, we can try to expand attributes
|
|
297
|
+
elif is_block_raw is False:
|
|
298
|
+
# get leading space, and attributes
|
|
299
|
+
|
|
300
|
+
func = partial(format_attributes, config, item)
|
|
301
|
+
|
|
302
|
+
tmp = re.sub(
|
|
303
|
+
re.compile(
|
|
304
|
+
rf"(\s*?)(<(?:{config.indent_html_tags})\b)((?:\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}\/])+?)(\s?/?>)",
|
|
305
|
+
re.VERBOSE | re.IGNORECASE,
|
|
306
|
+
),
|
|
307
|
+
func,
|
|
308
|
+
tmp,
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
# turn off raw block if we hit end - for one line raw blocks, but not an inline raw
|
|
312
|
+
if is_ignored_block_closing(config, item):
|
|
313
|
+
if not is_safe_closing_tag(config, item):
|
|
314
|
+
ignored_level -= 1
|
|
315
|
+
ignored_level = max(ignored_level, 0)
|
|
316
|
+
if ignored_level == 0:
|
|
317
|
+
is_block_raw = False
|
|
318
|
+
|
|
319
|
+
beautified_code = beautified_code + tmp
|
|
320
|
+
|
|
321
|
+
# try to fix internal formatting of set tag
|
|
322
|
+
def format_data(config: Config, contents: str, tag_size: int, leading_space) -> str:
|
|
323
|
+
try:
|
|
324
|
+
# try to format the contents as json
|
|
325
|
+
data = json.loads(contents)
|
|
326
|
+
contents = json.dumps(data, trailing_commas=False, ensure_ascii=False)
|
|
327
|
+
|
|
328
|
+
if tag_size + len(contents) >= config.max_line_length:
|
|
329
|
+
# if the line is too long we can indent the json
|
|
330
|
+
contents = json.dumps(
|
|
331
|
+
data,
|
|
332
|
+
indent=config.indent_size,
|
|
333
|
+
trailing_commas=False,
|
|
334
|
+
ensure_ascii=False,
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
except:
|
|
338
|
+
# was not json.. try to eval as set
|
|
339
|
+
try:
|
|
340
|
+
evaluated = str(eval(contents))
|
|
341
|
+
# need to unwrap the eval
|
|
342
|
+
contents = (
|
|
343
|
+
evaluated[1:-1]
|
|
344
|
+
if contents[:1] != "(" and evaluated[:1] == "("
|
|
345
|
+
else evaluated
|
|
346
|
+
)
|
|
347
|
+
except:
|
|
348
|
+
contents = contents.strip()
|
|
349
|
+
|
|
350
|
+
return (f"\n{leading_space}").join(contents.splitlines())
|
|
351
|
+
|
|
352
|
+
def format_set(config: Config, html: str, match: re.Match) -> str:
|
|
353
|
+
if inside_ignored_block(config, html, match):
|
|
354
|
+
return match.group()
|
|
355
|
+
|
|
356
|
+
leading_space = match.group(1)
|
|
357
|
+
open_bracket = match.group(2)
|
|
358
|
+
tag = match.group(3)
|
|
359
|
+
close_bracket = match.group(5)
|
|
360
|
+
contents = match.group(4).strip()
|
|
361
|
+
contents_split = contents.split("=", 1)
|
|
362
|
+
|
|
363
|
+
if len(contents_split) > 1:
|
|
364
|
+
contents = (
|
|
365
|
+
contents_split[0].strip()
|
|
366
|
+
+ " = "
|
|
367
|
+
+ format_data(
|
|
368
|
+
config,
|
|
369
|
+
contents_split[-1],
|
|
370
|
+
len(f"{open_bracket} {tag} {close_bracket}"),
|
|
371
|
+
leading_space,
|
|
372
|
+
)
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
return f"{leading_space}{open_bracket} {tag} {contents} {close_bracket}"
|
|
376
|
+
|
|
377
|
+
def format_function(config: Config, html: str, match: re.Match) -> str:
|
|
378
|
+
if inside_ignored_block(config, html, match):
|
|
379
|
+
return match.group()
|
|
380
|
+
leading_space = match.group(1)
|
|
381
|
+
open_bracket = match.group(2)
|
|
382
|
+
tag = match.group(3).strip()
|
|
383
|
+
close_bracket = match.group(5)
|
|
384
|
+
contents = format_data(
|
|
385
|
+
config,
|
|
386
|
+
match.group(4).strip()[1:-1],
|
|
387
|
+
len(f"{open_bracket} {tag}() {close_bracket}"),
|
|
388
|
+
leading_space,
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
return f"{leading_space}{open_bracket} {tag}({contents}) {close_bracket}"
|
|
392
|
+
|
|
393
|
+
func = partial(format_set, config, beautified_code)
|
|
394
|
+
# format set contents
|
|
395
|
+
beautified_code = re.sub(
|
|
396
|
+
re.compile(
|
|
397
|
+
r"([ ]*)({%-?)[ ]*(set)[ ]+?((?:(?!%}).)*?)(-?%})",
|
|
398
|
+
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE | re.DOTALL,
|
|
399
|
+
),
|
|
400
|
+
func,
|
|
401
|
+
beautified_code,
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
func = partial(format_function, config, beautified_code)
|
|
405
|
+
# format function contents
|
|
406
|
+
beautified_code = re.sub(
|
|
407
|
+
re.compile(
|
|
408
|
+
r"([ ]*)({{-?\+?)[ ]*?((?:(?!}}).)*?\w)(\([^\)]*?\)[ ]*)((?:(?!}}).)*?-?\+?}})",
|
|
409
|
+
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE | re.DOTALL,
|
|
410
|
+
),
|
|
411
|
+
func,
|
|
412
|
+
beautified_code,
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
if not config.preserve_blank_lines:
|
|
416
|
+
beautified_code = beautified_code.lstrip()
|
|
417
|
+
|
|
418
|
+
return beautified_code.rstrip() + "\n"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""djLint function to call jsbeautifier."""
|
|
2
|
+
from functools import partial
|
|
3
|
+
|
|
4
|
+
import jsbeautifier
|
|
5
|
+
import regex as re
|
|
6
|
+
from jsbeautifier.javascript.options import BeautifierOptions
|
|
7
|
+
|
|
8
|
+
from ..helpers import child_of_unformatted_block
|
|
9
|
+
from ..settings import Config
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def format_js(html: str, config: Config) -> str:
|
|
13
|
+
"""Format javascript inside <script> tags."""
|
|
14
|
+
|
|
15
|
+
def launch_formatter(config: Config, html: str, match: re.Match) -> str:
|
|
16
|
+
"""Add break after if not in ignored block."""
|
|
17
|
+
if child_of_unformatted_block(config, html, match):
|
|
18
|
+
return match.group()
|
|
19
|
+
|
|
20
|
+
if not match.group(3).strip():
|
|
21
|
+
return match.group()
|
|
22
|
+
|
|
23
|
+
indent = len(match.group(1)) * " "
|
|
24
|
+
|
|
25
|
+
# because of the param options for js-beautifier we cannot pass
|
|
26
|
+
# in a fixed space leading.
|
|
27
|
+
# so, call formatter twice, once with a fake indent.
|
|
28
|
+
# check which lines changed (these are the formattable lines)
|
|
29
|
+
# and add the leading space to them.
|
|
30
|
+
|
|
31
|
+
config.js_config["indent_level"] = 1
|
|
32
|
+
opts = BeautifierOptions(config.js_config)
|
|
33
|
+
beautified_lines = jsbeautifier.beautify(match.group(3), opts).splitlines()
|
|
34
|
+
|
|
35
|
+
config.js_config["indent_level"] = 2
|
|
36
|
+
opts = BeautifierOptions(config.js_config)
|
|
37
|
+
beautified_lines_test = jsbeautifier.beautify(match.group(3), opts).splitlines()
|
|
38
|
+
|
|
39
|
+
beautified = ""
|
|
40
|
+
for line, test in zip(beautified_lines, beautified_lines_test):
|
|
41
|
+
beautified += "\n"
|
|
42
|
+
if line == test:
|
|
43
|
+
beautified += line
|
|
44
|
+
continue
|
|
45
|
+
beautified += indent + line
|
|
46
|
+
|
|
47
|
+
return match.group(1) + match.group(2) + beautified + "\n" + indent
|
|
48
|
+
|
|
49
|
+
func = partial(launch_formatter, config, html)
|
|
50
|
+
|
|
51
|
+
return re.sub(
|
|
52
|
+
re.compile(
|
|
53
|
+
r"([ ]*?)(<(?:script)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*>)(.*?)(?=</script>)",
|
|
54
|
+
re.IGNORECASE | re.DOTALL,
|
|
55
|
+
),
|
|
56
|
+
func,
|
|
57
|
+
html,
|
|
58
|
+
)
|