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

Add ability to pretty-print HTML (lib only). #3428

Merged
merged 1 commit into from Feb 15, 2017
Merged

Conversation

adnrs96
Copy link
Member

@adnrs96 adnrs96 commented Jan 22, 2017

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.

@smarx
Copy link

smarx commented Jan 22, 2017

Automated message from Dropbox CLA bot

@adnrs96, it looks like you've already signed the Dropbox CLA. Thanks!

@showell
Copy link
Contributor

showell commented Jan 22, 2017

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.

@adnrs96
Copy link
Member Author

adnrs96 commented Jan 22, 2017

@showell Thank you for taking a look.
Actually I thought it this way that at a given time the token could be either a html tag or a handlebar tag.
Now if its a html tag then nothing would be done when we call using 'handlebar' and when we have a handlebar token nothing would happen when we call using 'html'. I actually was trying to reduce redundancy in code by doing it this way. Now when I think of it, a conditional call would have been better.
Wouldn't it be too much code redundancy if we make it in a linear if else fashion?
PS: I pushed another commit to add conditional call please take a look.

@showell
Copy link
Contributor

showell commented Jan 22, 2017

Huh, I misread the original code. But I still think you'd be better off having determine_offsets do something like if token in ('html_start', 'handlebars_start')

@adnrs96
Copy link
Member Author

adnrs96 commented Jan 22, 2017

yeah that sounds more pythonic, I will go with that.

@adnrs96
Copy link
Member Author

adnrs96 commented Jan 22, 2017

I made the changes suggested. Please take a look.
Also should I start cleaning up the handlebar templates as you suggested to do.

@timabbott
Copy link
Sponsor Member

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.

@adnrs96 adnrs96 force-pushed the FIX-1659 branch 6 times, most recently from 50de6cf to c2da4b5 Compare January 26, 2017 22:08
else:
for line_num in range(start_line + 1, end_line):
if line_num not in offsets:
offsets[line_num] = offset
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member

@tommyip tommyip left a 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')):
Copy link
Member

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>

Copy link
Member Author

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

Copy link
Member Author

@adnrs96 adnrs96 Feb 3, 2017

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)
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

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>
Copy link
Member

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?

Copy link
Contributor

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.

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
@showell showell merged commit e84cf7b into zulip:master Feb 15, 2017
@adnrs96 adnrs96 changed the title [WIP-1661] Add ability to pretty-print HTML (lib only). Add ability to pretty-print HTML (lib only). Feb 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants