deezer / template-remover

Template remover
Apache License 2.0
8 stars 3 forks source link

Jinja temlate cleaner should be smarter #1

Open sirex opened 9 years ago

sirex commented 9 years ago

Current Jinja cleaner implementation is not very smart, it just replaces all tamplate tags to speces.

But I thing it should be smarter and detect places where tags should be completely removed and where replaced to something.

My suggestions:

Remove whole line if tag takes whole line.

For example this:

1
{% if true %}
2
{% endif %}">
3
{% block name %}
4
{% endblock %}">
5

Should output:

1
2
3
4
5

Remove inline tags completely.

For example this:

<div class="form-group{% if provider.errors %} has-error{% endif %}">

Should be replaced to this:

<div class="form-group has-error">

But of course, this should depend on tag, for example, {% trans "" %} should always be replace to some content.

What do you think?

sirex commented 9 years ago

This issue is related to https://github.com/deezer/html-linter/issues/6

sk- commented 9 years ago

Currently it is being done in that way, so line numbers and positions remain the same. Or otherwise the error reported by html-linter won't be easy to find. In your first example the line number reported would be wrong and in the second, the column would be wrong.

Do you have any ideas on how we could address this, retaining the same line numbers and columns?

sirex commented 9 years ago

OK, now I understand why cleaner is implemented that way.

One possible solution that comes to my mind is that template cleaner could provide line and column transformation table.

For example, having following input:

{% if true %}
<p>{% if false %}2{% endif %}</p>
{% endif %}

template cleaner provides this output:

<p>2</p>

and also emits following transformation table:

table = {
    1: (2, {
        1: 1,    # <
        2: 2,    # p
        3: 3,    # >
        4: 18,   # 2  -> {% if false %}2
        5: 30,   # <  -> {% endif %}<
        6: 31,   # /
        7: 32,   # p
        8: 33,   # >
    }),
}

Then, when someone would need to get original position for example of first line and forth column (transform(table, 1, 4)) from output, he could do this:

def transform(table, line, column)
    line, cols = table[line]
    return line, cols[column]

# transform(table, 1, 4) -> (2, 18)

The only question is how difficult it is to generate such transformation table?