Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DjHTML Version 3 #77

Merged
merged 45 commits into from
Feb 23, 2023
Merged

DjHTML Version 3 #77

merged 45 commits into from
Feb 23, 2023

Conversation

JaapJoris
Copy link
Member

@JaapJoris JaapJoris commented Feb 13, 2023

Here is a non-exhaustive summary of all the things that have changed:

@JaapJoris JaapJoris force-pushed the release-candidate branch 2 times, most recently from 4ccab3b to ae01cb6 Compare February 13, 2023 18:26
For this release, a substantial part the codebase has been refactored.
Here is a non-exhaustive summary of all the things that have changed:

- Support for Python 3.6 and 3.7 has been dropped
- A Django middleware class to indent HTML responses has been added
- Ignore both opening and closing comment tags
- New handling off both relative and absolute offsets
- Return correct `repr()` strings with the `--debug` option
- New token class "OpenDouble" and a revised indentation algorithm
- Additional `line` argument to `create_token()` methods
- Changed return value of `create_token()` methods
- Changed constructor arguments of Token and Line classes
- Refactored all `create_token()` methods to get rid of return statements
- Simplified test suite runner that reindents the expected output
- Don't indent the contents of template variables (closes #75)
- Improved handling of Django tags inside HTML elements
- Improved JavaScript `case` indentation (closes #76)
- Improved JavaScript method chaining (closes #59)
- Improved CSS multiline statements (closes #74)
- New multiline HTML element indentation (closes #50)
- New multiline HTML attribute value indentation
- Extensive test coverage with lots of edge cases
djhtml/__main__.py Outdated Show resolved Hide resolved
djhtml/__main__.py Outdated Show resolved Hide resolved
djhtml/__main__.py Outdated Show resolved Hide resolved
djhtml/__main__.py Outdated Show resolved Hide resolved
djhtml/__main__.py Outdated Show resolved Hide resolved
djhtml/__main__.py Outdated Show resolved Hide resolved
tests/test_suite.py Outdated Show resolved Hide resolved
djhtml/middleware.py Outdated Show resolved Hide resolved
djhtml/middleware.py Outdated Show resolved Hide resolved
djhtml/middleware.py Outdated Show resolved Hide resolved
djhtml/middleware.py Outdated Show resolved Hide resolved
djhtml/middleware.py Outdated Show resolved Hide resolved
djhtml/lines.py Outdated Show resolved Hide resolved
djhtml/modes.py Outdated Show resolved Hide resolved
On the one hand, we'd like a source like this:

    <div>
    <p>
    {# ignore me #}
    </p>
    </div>

to be indented to

    <div>
        <p>
            {# ignore me #}
        </p>
    </div>

and not to

    <div>
        <p>
    {# ignore me #}
        </p>
    </div>

But on the other hand, we'd like a source like this:

    <div>
    <p>
    {# ╔═══════════════════════════════╗
       ║ Carefully laid out table ║
       ╚═══════════════════════════════╝ #}
    </p>
    </div>

_not_ to get reindented to this:

    <div>
        <p>
            {# ╔═══════════════════════════════╗
            ║ Carefully laid out table ║
            ╚═══════════════════════════════╝ #}
        </p>
    </div>

In version <3, DjHTML compromised by indenting the starting token but
not the ending token of comments. However, that didn't make it better:

    <div>
        <p>
            {# ╔═══════════════════════════════╗
       ║ Carefully laid out table ║
       ╚═══════════════════════════════╝ #}
        </p>
    </div>

So, the only consistent thing to do is to preserve the existing
indentation of both opening and closing comment tags in all modes.
Note that this doesn't affect lines that don't start with a comment,
so the following:

    <div>
    <p>text</p>{# ignore me #}
    </div>

will still get reindented as:

    <div>
        <p>text</p>{# ignore me #}
    </div>

The new policy was already implemented for all comment tags except
{# comment #}. This commit concludes the transition to the new
behavior.
There is still one broken case that is _very_ hard to fix:

    var re = x / y // comment

Here the string "/ y /" is recognized as a literal while it is not. This
means the comment is not tokenized as a comment, and if it contains any
opening tokens the next line will be incorrectly indented. A working example
is provided in the unittests.
This guarantees that DjHTML is exactly behaving as expected, even if a
tokenization mishap doesn't result in changed indentation. An added bonus is
that by tracking these files in Git, we will notice changes in tokenization
immediately.

The initial token files were generated by the supplied `generate_tokens.py`
script.
@JaapJoris JaapJoris merged commit 342c61f into main Feb 23, 2023
@JaapJoris JaapJoris deleted the release-candidate branch February 23, 2023 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment