İçeriğe geç

Başlarken

djLint, Python 3.11 veya daha yenisini gerektirdiği PyPI üzerinde ve kendi Python’ıyla gelen npm üzerinde yayımlanır. Bir Python projesine uv ya da pip ile, Angular uygulaması, Eleventy sitesi veya Shopify teması gibi bir JavaScript projesine npm ile ekleyin.

uv ile projenize geliştirme bağımlılığı olarak ekleyin:

Terminal window
uv add --dev djlint

Ya da bağımsız bir araç olarak kurun:

Terminal window
uv tool install djlint

djLint’i düzenleyicinizde ya da her commit’te çalıştırmak için Entegrasyonlar sayfasına ve pre-commit bölümüne bakın.

djLint’e şablonlarınızın bulunduğu klasörü verin ve --profile ile şablon dilinizi belirtin. Hangi linter kurallarının çalışacağını ve şablon söz diziminin nasıl biçimlendirileceğini profil belirler. Bir Django projesi için:

Şablonları denetleyin (denetleme varsayılan mod olduğundan --lint yazmasanız da olur):

Terminal window
djlint templates --lint --profile=django

Biçimlendirmenin neyi değiştireceğine bakın. --check ile djLint bir diff yazdırır ve dosyalara dokunmaz:

Terminal window
djlint templates --check --profile=django

Dosyaları yerinde biçimlendirin:

Terminal window
djlint templates --reformat --profile=django

Tüm profiller profile seçeneğinde listelenir. Her şablon dilinin ayrıca kendi sayfası vardır, örneğin Django, Jinja ya da Handlebars. Profili her seferinde yazmamak için bir yapılandırma dosyasında nasıl ayarlayacağınız da orada gösterilir.

djLint’i projeye uv add --dev ile eklediyseniz projenin ortamındadır: komutların başına uv run ekleyin, örneğin uv run djlint templates --lint --profile=django. npm i -D ile eklediyseniz başlarına bunun yerine npx ekleyin, örneğin npx djlint templates --lint --profile=django.

djLint bir yolla ya da stdin ile çalışır.

Bir yolla çalıştırma:

Terminal window
djlint /path/to/templates --lint

Ya da belirli bir dosyayla:

Terminal window
djlint /path/to/this.mustache --lint

Ya da stdin ile:

Terminal window
echo "<div></div>" | djlint -

Stdin kodu yeniden biçimlendirmek için de kullanılabilir. Çıktı, mesajlar olmadan yalnızca biçimlendirilmiş koddan oluşur.

Terminal window
echo "<div></div>" | djlint - --reformat

Çıktı:

<div></div>

stdin’den okurken djLint’in per-file-ignores ile eşleştireceği ya da mesajlarda göstereceği gerçek bir yol yoktur; bu yüzden varsayılan olarak - kullanır. Aktarılan içeriğin gerçek yolunu djLint’e bildirmek için --stdin-filename verin; örneğin bir düzenleyici entegrasyonu kaydetme sırasında bir dosyanın içeriğini aktardığında.

Terminal window
echo "<div></div>" | djlint - --stdin-filename templates/index.html
Kod Anlamı
0 djLint’ten denetlemesi istenen her şey temiz ya da bulduğu her dosya yapılandırmanız tarafından atlandı.
1 djLint lint hataları ya da yeniden biçimlendirilmesi gereken dosyalar buldu. --warn bunları uyarı olarak bildirir ve bunun yerine 0 ile çıkar.
2 djLint istediğiniz şeyi denetlemedi: yollar hiçbir dosyayla eşleşmedi, bir dosya okunamadı (UTF-8 değil ya da izin yok), komut satırı ya da yapılandırma geçersizdi veya djLint başarısız oldu.

Yalnızca 1 kodu, djLint’in şablonlarınıza baktığı ve bulduklarını beğenmediği anlamına gelir. Böylece bir işlem hattı başarısız bir denetimi, hiçbir şeyi denetlemeyen bir çalıştırmadan ayırabilir; örneğin yanlış bir yol verilmiş ya da --extension artık eşleşmiyordur.

djLint’in bulduğu ve ardından exclude, extend_exclude, use_gitignore ya da require_pragma aracılığıyla bilerek atladığı dosyalar hata sayılmaz; exclude seçeneğinin pre-commit altında çalışmasını sağlayan da budur. İşlem hattınızda hiç şablon içermeyen bir yol olağansa 2 kodunu allow_empty_input ile kapatın:

Terminal window
djlint /path/to/templates --lint --allow-empty-input
Usage: djlint [OPTIONS] SRC ...
djLint · HTML template linter and formatter.
Modes:
--lint Lint the files. This is the default when
neither --reformat nor --check is given.
--reformat Format the files in place.
--check Show what formatting would change, without
changing the files.
--lsp Run as a language server over stdio, for
editors.
Files:
-e, --extension EXT Extension of the files to look for in folders.
[default: html]
--exclude PATHS Regexes of the paths to skip, comma-separated,
in place of the default list.
--extend-exclude PATHS Regexes of the paths to skip on top of the
exclude list, comma-separated.
--use-gitignore Also skip the paths that the .gitignore in the
project root lists.
--require-pragma Only format or lint files whose first line is
a djlint:on comment.
--stdin-filename PATH Path that stands for stdin in messages,
excludes and per-file ignores. [default: -]
--allow-empty-input Exit with 0 instead of 2 when the given paths
match no files.
Configuration:
--profile NAME Template language: html, django, jinja,
nunjucks, handlebars, liquid, golang, angular,
tera, askama, mako or all. It decides which
linter rules run and how template syntax is
formatted. Without one, formatting follows all
and the linter runs the rules of html.
--configuration FILE Global config file: a pyproject.toml,
djlint.toml or .djlintrc. The project's own
config file wins where both set an option.
--prefer-configuration Let the --configuration file win over the
project's own config file.
Linter:
-i, --ignore CODES Linter codes to skip, comma-separated, for
example H014,H017.
--include CODES Linter codes to turn on, comma-separated, for
example H017. Works for rules that are off by
default and for rules the profile leaves out.
--per-file-ignores <PATTERN CODES>...
Skip linter codes in the files whose path
matches a regex. Can be repeated.
--rules FILE Custom linter rules, in the .djlint_rules.yaml
format.
Output:
--quiet Do not print diffs, the progress bar or the
summary.
--warn Exit with 0 instead of 1 when there are lint
errors or files to reformat.
--statistics After linting, print how many times each code
was found.
--linter-output-format FORMAT Layout of each lint message, from {filename},
{line}, {end}, {code}, {message} and {match}.
[default: {code} {line} {message} {match}]
--github-output / --no-github-output
Print results as GitHub Actions annotations.
On by default inside GitHub Actions.
--sarif Print findings as a SARIF 2.1.0 JSON document,
for GitHub code scanning and other tools.
Layout:
--indent N Spaces per indent level. [default: 4, or
indent_size from .editorconfig]
--max-line-length N Join tags onto one line only when the line
fits within this length. [default: 120, or
max_line_length from .editorconfig]
--max-attribute-length N Spread a tag's attributes over lines once they
are longer than this. [default: 70]
--single-attribute-per-line When an opening tag wraps, put each attribute
on its own line.
--line-break-after-multiline-tag
When a tag's attributes are spread over lines,
start its content on a new line.
--no-indent-inner-html Do not indent <head> and <body> inside <html>.
--keep-br-inline Keep <br> on the line of the text it breaks.
--custom-blocks TAGS Names of the custom template blocks to indent,
comma-separated, for example toc.
--custom-html TAGS Names of the custom HTML tags to indent,
comma-separated, for example mjml.
--ignore-blocks TAGS Template blocks that do not indent their
contents, comma-separated.
--blank-line-after-tag TAGS Template tags to follow with a blank line,
comma-separated, for example load,extends.
--blank-line-before-tag TAGS Template tags to precede with a blank line,
comma-separated, for example load,extends.
--max-blank-lines N Keep at most this many blank lines in a row.
Has no effect with --preserve-blank-lines.
[default: 0]
--preserve-blank-lines Keep blank lines where possible.
--preserve-leading-space Keep the leading spaces of text lines where
possible.
--no-line-after-yaml Do not add a blank line after YAML front
matter.
Rewrites:
--close-void-tags Close void tags with a slash, so <img> becomes
<img />.
--ignore-case Do not fix the case of known HTML tags.
--sort-attributes Sort attributes by name, with id first and
class second.
--sort-load-tags Sort the libraries in {% load %} tags and drop
repeated ones.
--name-endblocks Name the block in its {% endblock %} when the
block spans lines.
--quote-style [double|single] Quotes for strings inside template tags.
[default: double]
--no-entity-formatting Do not rewrite entity references such as
&copy; as the characters they name.
--no-function-formatting Do not format the arguments of function calls
inside {{ }}.
--no-set-formatting Do not format the contents of {% set %} tags.
--preserve-class-newlines Keep the line breaks in class attributes
written across lines.
--format-attribute-template-tags
Format template syntax inside tag attributes.
CSS and JavaScript:
--format-css Also format the contents of <style> tags, with
css-beautify.
--indent-css N Indent width inside <style> tags, for
--format-css. [default: 4]
--format-js Also format JavaScript and JSON in <script>
tags, with js-beautify.
--indent-js N Indent width inside <script> tags, for
--format-js. [default: 4]
--format-attribute-js-json Also format JavaScript and JSON in attributes
such as onclick and x-data.
--format-attribute-js-json-pattern REGEX
Regex for the names of the attributes to
format as JavaScript or JSON. [default: on*
event handlers and Alpine, Vue, Angular and
Aurelia bindings]
--format-attribute-js-json-min-props N
Format an object only when it has at least
this many properties. [default: 2]
Other:
--version Show the version and exit.
-h, --help Show this message and exit.