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
Add ability to pretty-print HTML (lib only). #3428
Conversation
Automated message from Dropbox CLA bot @adnrs96, it looks like you've already signed the Dropbox CLA. Thanks! |
Hmmm...I'm not sure the two-pass approach will work for html/handlebars. I believe you need to check for indentation in one single pass and treat both tags the same way. |
@showell Thank you for taking a look. |
Huh, I misread the original code. But I still think you'd be better off having |
yeah that sounds more pythonic, I will go with that. |
I made the changes suggested. Please take a look. |
I think structurally, we'll want to merge the tooling before merging migrations for individual templates, but it's probably useful to do a couple handlebars templates to test the procedure. |
50de6cf
to
c2da4b5
Compare
tools/lib/pretty_print.py
Outdated
else: | ||
for line_num in range(start_line + 1, end_line): | ||
if line_num not in offsets: | ||
offsets[line_num] = offset |
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.
Where does offset
get set for this line to work?
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.
My bad it should have been
info['offset'] here.
Will correct it.
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.
Pretty good! Just a few comments.
Also just for having consistent styling, can you have 2 spaces before inline comments?
# Now that we have all of our offsets calculated, we can just | ||
# join all our lines together, fixing up offsets as needed. | ||
formatted_lines = [] | ||
for i, line in enumerate(html.split('\n')): |
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.
What if there is a literal \n
in the HTML? Like:
<div class='codeblock'>
print('\n')
</div>
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.
will need to see into this
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.
no that won't be a problem i guess.
\n
within a file is read as \\n\
self.compare(pretty_print_html(BAD_HTML2), GOOD_HTML2) | ||
self.compare(pretty_print_html(BAD_HTML3), GOOD_HTML3) | ||
self.compare(pretty_print_html(BAD_HTML4), GOOD_HTML4) | ||
self.compare(pretty_print_html(BAD_HTML5), GOOD_HTML5) |
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.
Maybe add a testcase for a normal HTML file with correct indentation to check if it changes anything.
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.
Also need trailing new 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.
ohk that test makes sense. I will add it.
assert line.strip() == pretty_line.strip() | ||
formatted_lines.append(pretty_line) | ||
|
||
return '\n'.join(formatted_lines) |
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.
Need a trailing new line here.
</pre> | ||
<div class = "foo" | ||
id = "bar" | ||
role = "whatever">{{ bla }}</div> |
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.
Is there any reason for the extra space here?
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.
@tommyip We want the beautifier to be not overly aggressive about fixing every little quirk of the HTML, in case the original coders had some reason to format things the way we did. That decision may change, though; we might want to move toward a more opinionated formatter.
67d720f
to
ceaad09
Compare
fc0e117
to
0b0ffeb
Compare
In This commit we extend the work being done by @showell in PR#1778 to develop a tool to pretty print html and our handlebar templates in order to enforce our style convention of 4 Space indentation in templates. This commit introduces following changes: * Fix Py3 Compatibility. * Add ability to prettify in cases when html tags are not the starting of a line and addition of test cases for it. * Add ability to lint handlebar tags and add test cases for it. * Add {{else}} as special case of indent. * Add test cases in general to testing new tool. @showell Helped me throughout and reviewed this commit. Fixes zulip#1778
This PR extends the work being done here in PR#1778.
@showell has already implemented a tool to prettify HTML in above PR. I have extended it to deal with cases in which HTML tags are not the start of line and added support for handlebar tags.
This will add ability to check our handlebar templates for 4 Space Indentation and will work forward to enforce 4 space indents by tightening our linters. This has been discussed in this issue #1661.