pelme/htpy

HTML to HTPY converter for lazy people

aarcex3 opened this issue · 6 comments

Hi, basically the title is self explainable.
Wouldn't be nice a simple tool to convert existing html files into htpy elements, so that you dont have to write your elements all from scratch if you already have some html files with layouts and styles and so on?

Example
This

<section class="hero is-fullheight is-link">
  <div class="hero-body">
    <div class='container'>
      <p class="subtitle is-3 is-spaced">Welcome</p>
      <p class="title is-1 is-spaced">Student code: {{student_code}}</p>
    </div>
  </div>
</section>

becomes this after calling the converter.

section(".hero.is-fullheight.is-link")[
            div(".hero-body")[
              div(".container")[
                p(".subtitle.is-3.is-spaced")["Welcome"],
                p(".title.is-1.is-spaced")[f"Student code: {student_code}"],
              ]
            ]  
        ]

That would be nice! It would be nice to do this without adding another dependency to htpy. Maybe something like beatifulsoup would be a good idea to base it off. Maybe it could be a optional dependency and install with extras?

I would find this very useful

If anyone is interested to give it a shot, this would be a welcome contribution!

Python ships with the html.parser module that could handle the parsing without needing beatifulsoup: https://docs.python.org/3/library/html.parser.html

I also found html2breve that uses a syntax very similar to htpy that could be used as inspiration: https://github.com/cwells/breve/blob/master/tools/html2breve

html2breve uses Python 2 but could probably be ported/adjusted. We could also pipe the output via black/ruff-format for perfect formatting.

This is in fact very useful for what I was doing right now, so I'm playing around a bit with it a bit. Using the inbuilt HTMLParser

#26

Seems to work quite well, although implementation might be a bit messy, in need of refactoring and probably some documentation also. I'll put some more work in if this is close to the mark, @pelme

This is fixed via #26 and html2htpy is part of 24.6.0. The full docs for the command can be found at https://htpy.dev/html2htpy/.