-
Notifications
You must be signed in to change notification settings - Fork 32
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
Relative multi-line HTML elements #83
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,10 +286,21 @@ def create_token(self, raw_token, src, line): | |
mode = self | ||
|
||
if raw_token == "<": | ||
if tag := re.match(r"([\w\-\.:]+)", src): | ||
token, mode = Token.Text(raw_token, mode=DjHTML), InsideHTMLTag( | ||
tag[1], line, self | ||
if match := re.match(r"([\w\-\.:]+)(\s*)", src): | ||
tagname = match[1] | ||
following_spaces = match[2] | ||
absolute = True | ||
token = Token.Text(raw_token, mode=DjHTML) | ||
offsets = dict( | ||
relative=-1 if line.indents else 0, | ||
absolute=len(line) + len(tagname) + 2, | ||
) | ||
if "\n" in following_spaces: | ||
# Use "relative" multi-line indendation instead | ||
absolute = False | ||
token.indents = True | ||
offsets = dict(relative=0, absolute=0) | ||
mode = InsideHTMLTag(tagname, line, self, absolute, offsets) | ||
else: | ||
token = Token.Text(raw_token, mode=DjHTML) | ||
elif raw_token == "<!--": | ||
|
@@ -513,17 +524,14 @@ class InsideHTMLTag(DjTXT): | |
|
||
RAW_TOKENS = DjTXT.RAW_TOKENS + [r"/?>", r"[^ ='\">/\n]+=", r'"', r"'"] | ||
|
||
def __init__(self, tagname, line, return_mode): | ||
def __init__(self, tagname, line, return_mode, absolute, offsets): | ||
JaapJoris marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.tagname = tagname | ||
self.return_mode = return_mode | ||
self.absolute = absolute | ||
self.offsets = offsets | ||
self.token_re = compile_re(self.RAW_TOKENS) | ||
self.inside_attr = False | ||
self.offsets = dict( | ||
relative=-1 if line.indents else 0, absolute=len(line) + len(tagname) + 2 | ||
) | ||
|
||
# Pff... | ||
self.additional_offset = -len(tagname) - 1 | ||
self.additional_offset = -len(tagname) - 1 if absolute else 0 | ||
|
||
def create_token(self, raw_token, src, line): | ||
mode = self | ||
|
@@ -549,6 +557,8 @@ def create_token(self, raw_token, src, line): | |
token = Token.Text(raw_token, mode=InsideHTMLTag, **self.offsets) | ||
elif not self.inside_attr and raw_token == "/>": | ||
token, mode = Token.Text(raw_token, mode=DjHTML), self.return_mode | ||
if not self.absolute: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is replaceable by an if that return |
||
token.dedents = True | ||
elif not self.inside_attr and raw_token == ">": | ||
if self.tagname.lower() in DjHTML.IGNORE_TAGS: | ||
token, mode = Token.Text(raw_token, mode=DjHTML), self.return_mode | ||
|
@@ -565,6 +575,8 @@ def create_token(self, raw_token, src, line): | |
Token.Open(raw_token, mode=DjHTML), | ||
self.return_mode, | ||
) | ||
if not self.absolute: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This particular case is indeed problematic without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mind "cheating" with tokens this way. It's actually quite elegant, and if we did it everywhere we wouldn't need the separate Open, Close, and CloseAndOpen classes at all. So I'll leave it in just to remind my future self that this is also a possibility. Thanks for reviewing! |
||
token.dedents = True | ||
else: | ||
token, mode = super().create_token(raw_token, src, line) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Admittedly, this is "cheating" a bit because it changes an "internal" attribute of a token has already been created. It saves a whole bunch of if/else statements though, especially further down where the
>
token is created.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's avoidable too :).
Edit: after reviewing the handling of
>
, it still is avoidable here, just not there.