-
Notifications
You must be signed in to change notification settings - Fork 12
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 utilities to convert from html to htpy #26
Conversation
Might be a better idea to have it parse files than to paste into the terminal, bit of jank but atleast it works. |
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.
This is GREAT! Thanks a lot for this nice command! Really looking forward to be using 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.
I had another look and left some comments. I think we should have a separate page in the docs with some examples too :)
Alright, I think I'm happy with the current state at this point. I've taken the liberty to resolve some of the comment threads (don't know if that is bad practice of me to do..?), maybe you'll do a final sweep and clarify if there is anything else you want changed? Ill add a simple docs page |
One thing I thought about might be an option to add imports for used elements in the output. Might be nice to save even a couple of more seconds. That should be quick to add if you want it, but might also be another PR perhaps? |
Also added some docs for review! |
I updated the formatting to use subprocess as per your suggestion. Seems to work great. Also removed the |
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.
Thanks a lot for putting in the effort to finalize this. The quality of docs and tests are really nice. This feels like a very nice addition and it works really smooth!
Alright, I think I've addressed the comments. Thank you for all the thorough feedback and timely followup by the way. |
There was this little thing I wanted to add: Detect htpy importsIf you pass the
from htpy import p, section
section("#main-section.hero.is-link")[
p(".subtitle.is-3.is-spaced")["Welcome"]
] In the end, I couldn't help myself, so I added it now. Ill stop adding new things now, promise. |
I like this a lot!! I also think that the imports should be added by default. Most people will probably Lately I have been using What do you think about We can add |
I added it! Import optionsYou have a couple of options regarding imports with the Module import of htpy:
|
fyi, in addition to the unit tests, I did some manual testing with a bunch of large jinja templates that I had laying around, and it seems to behave well even for fairly ugly templates with lots of weird jinja shit. |
I also did some proof-reading of the docs. Think I'm pretty much good from my side now. |
Thanks A LOT for this and all the good discussions! I opened a PR with some small changes, please have a look #27 ! |
Convert HTML to htpy code
Maybe you already have a bunch of HTML, or templates that you would like to migrate to htpy.
We got you covered. The utility command
html2htpy
ships withhtpy
, and can be used to transform existinghtml into Python code (htpy!).
Lets say you have an existing HTML file:
Now, if you run the command, it outputs the corresponding Python code (htpy).
Piping input/stdin stream
You can also pipe input to htpy, for example
cat demo.html | html2htpy
.This can be combined with other workflows in the way that you find most suitable.
For example, you might pipe from your clipboard to htpy, and optionally direct the output to a file.
Linux
Mac
Windows
Formatting the output
html2htpy
can format the output Python code usingblack
orruff
.Select the preferred formatter with the
-f
/--format
flag. Options areauto
,ruff
,black
andnone
.By default, the selection will be
auto
, formatting if it finds a formatter on path, prefferingruff
if it's available.If no formatters are available on path, the output will not be formatted.
Import options
You have a couple of options regarding imports with the
-i
/--imports
flag.Options are
yes
(default),h
,no
.Module import of htpy:
--imports=h
Some people prefer to
import htpy as h
instead of importing individual elements from htpy.If this is you, you can use the
--imports=h
option to get corresponding output when usinghtml2htpy
.Explicit id and class kwargs
If you prefer the explicit
id="id", class_="class"
kwargs syntax over the default htpy shorthand#id.class
syntax, you can get it by passing the--no-shorthand
flag.Default shorthand yield
#id.class
No shorthand yields kwargs
id
,class_
Template interpolation to f-strings
html2htpy
will try to convert template variables to pythonic f-strings:template {{ variables }}
->f"template { variables }"
Note that other typical template syntax, such as loops
{% for x in y %}
, can not be transformed this way,so you will often have to clean up a bit after
html2htpy
is done with its thing.See the example below: