Skip to content

Commit b5ea9aa

Browse files
committed
Split the html2htpy docs into how-to and reference.
This is following the Diataxis framework. This commit bootstraps the reference section. It also improves the help text provided by the tool itself.
1 parent 56cc031 commit b5ea9aa

File tree

4 files changed

+163
-85
lines changed

4 files changed

+163
-85
lines changed

docs/html2htpy.md

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1-
# Convert HTML to htpy Code
1+
# Convert HTML to htpy code
22

3-
Maybe you already have a bunch of HTML, or templates that you would like to migrate to htpy.
4-
We got you covered. The utility command `html2htpy` ships with `htpy`, and can be used to transform existing
5-
html into Python code (htpy!).
3+
Maybe you already have a bunch of HTML, or templates that you would like to migrate to htpy. We got you covered. This page describes how you can convert existing HTML to htpy code.
64

7-
```
8-
$ html2htpy -h
9-
usage: html2htpy [-h] [-f {auto,ruff,black,none}] [-i {yes,h,no}] [--no-shorthand] [input]
10-
11-
positional arguments:
12-
input input HTML from file or stdin
13-
14-
options:
15-
-h, --help show this help message and exit
16-
-f {auto,ruff,black,none}, --format {auto,ruff,black,none}
17-
Select one of the following formatting options: auto, ruff, black or none
18-
-i {yes,h,no}, --imports {yes,h,no}
19-
Output mode for imports of found htpy elements
20-
--no-shorthand Use explicit `id` and `class_` kwargs instead of the shorthand #id.class syntax
21-
```
5+
The utility command `html2htpy` ships with `htpy`, and can be used to transform existing HTML into Python code (htpy!).
226

237
Lets say you have an existing HTML file:
248

@@ -53,7 +37,7 @@ Lets say you have an existing HTML file:
5337
Now, if you run the command, it outputs the corresponding Python code (htpy).
5438

5539
```
56-
$ html2htpy index.html
40+
$ html2htpy index.html
5741
```
5842

5943
```py
@@ -78,94 +62,48 @@ html(lang="en")[
7862
]
7963
```
8064

81-
## Piping Input/Stdin Stream
82-
83-
You can also pipe input to htpy, for example `cat demo.html | html2htpy`.
65+
## Convert HTML snippets from the clipboard
8466

8567
This can be combined with other workflows in the way that you find most suitable.
8668
For example, you might pipe from your clipboard to htpy, and optionally direct the output to a file.
8769

88-
#### Linux
70+
### Linux
8971

9072
```
9173
xclip -o -selection clipboard | html2htpy > output.py
9274
```
9375

94-
#### Mac
76+
### Mac
9577

9678
```
9779
pbpaste | html2htpy > output.py
9880
```
9981

100-
#### Windows
82+
### Windows
10183

10284
```
10385
powershell Get-Clipboard | html2htpy > output.py
10486
```
10587

106-
## Formatting the Output
107-
108-
`html2htpy` can format the output Python code using `black` or `ruff`.
109-
Select the preferred formatter with the `-f`/`--format` flag. Options are `auto`, `ruff`, `black` and `none`.
110-
111-
By default, the selection will be `auto`, formatting if it finds a formatter on path, preferring `black` if it's available.
112-
If no formatters are available on path, the output will not be formatted.
113-
114-
## Import Options
88+
## Converting Django/Jinja templates
11589

116-
You have a couple of options regarding imports with the `-i`/`--imports` flag.
117-
Options are `yes` (default), `h`, `no`.
90+
`html2htpy` will convert Django/Jinja-style template variables to f-strings:
11891

119-
#### Module import of htpy: `--imports=h`
120-
121-
Some people prefer to `import htpy as h` instead of importing individual elements from htpy.
122-
If this is you, you can use the `--imports=h` option to get corresponding output when using `html2htpy`.
123-
124-
```py title="$ html2htpy --imports=h example.html"
125-
import htpy as h
126-
127-
h.section("#main-section.hero.is-link")[
128-
h.p(".subtitle.is-3.is-spaced")["Welcome"]
129-
]
92+
``` html title="input"
93+
<div>hi {{ name }}!</div>
13094
```
13195

132-
## Explicit ID and Class Kwargs
96+
``` py title="html2htpy output"
13397

134-
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.
98+
from htpy import div
13599

136-
```html title="example.html"
137-
<section id="main-section" class="hero is-link">
138-
<p class="subtitle is-3 is-spaced">Welcome</p>
139-
</section>
140-
```
100+
div[f"hi { name }!"]
141101

142-
#### Default Shorthand Yield `#id.class`
143-
144-
```py title="$ html2htpy example.html"
145-
from htpy import p, section
146-
147-
section("#main-section.hero.is-link")[
148-
p(".subtitle.is-3.is-spaced")["Welcome"]
149-
]
150-
```
151-
152-
#### No Shorthand Yields Kwargs `id`, `class_`
153-
154-
```py title="$ html2htpy --no-shorthand example.html"
155-
from htpy import p, section
156-
157-
section(id="main-section", class_="hero is-link")[
158-
p(class_="subtitle is-3 is-spaced")["Welcome"]
159-
]
160102
```
161103

162-
## Template Interpolation to f-strings
163-
164-
`html2htpy` will try to convert template variables to pythonic f-strings:
165-
166-
`template {{ variables }}` -> `f"template { variables }"`
104+
### Limitations
167105

168-
Note that other typical template syntax, such as loops `{% for x in y %}`, can not be transformed this way,
106+
Other typical template syntax, such as loops `{% for x in y %}`, can not be transformed this way,
169107
so you will often have to clean up a bit after `html2htpy` is done with its thing.
170108

171109
See the example below:
@@ -202,4 +140,4 @@ body[
202140

203141
## VSCode Extension
204142

205-
If you are using VSCode, you can install the [html2htpy](https://marketplace.visualstudio.com/items?itemName=dunderrrrrr.html2htpy) extension to quickly convert HTML to htpy code.
143+
If you are using VSCode, you can install the [html2htpy](https://marketplace.visualstudio.com/items?itemName=dunderrrrrr.html2htpy) extension to quickly convert HTML to htpy code.

docs/reference/html2htpy.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
# The html2htpy CLI tool
3+
4+
The `html2htpy` command converts HTML code to htpy Python code.
5+
6+
```
7+
usage: html2htpy [-h] [-f {auto,ruff,black,none}] [-i {yes,h,no}] [--no-shorthand] [input]
8+
9+
positional arguments:
10+
input Input HTML file, e.g. home.html. Optional. If not specified, html2htpy will read from stdin.
11+
12+
options:
13+
-h, --help show this help message and exit
14+
-f, --format {auto,ruff,black,none}
15+
16+
Format the output code with a code formatter.
17+
18+
auto (default):
19+
- If black is installed (exists on PATH), use `black` for formatting.
20+
- If ruff is installed (exists on PATH): Use `ruff format` for formatting.
21+
- If neither black or ruff is installed, do not perform any formatting.
22+
23+
black:
24+
Use the black formatter (https://black.readthedocs.io/en/stable/).
25+
26+
ruff:
27+
Use the ruff formatter (https://docs.astral.sh/ruff/formatter/).
28+
29+
none:
30+
Do not format the output code at all.
31+
-i, --imports {yes,h,no}
32+
33+
Specify formatting for imports.
34+
35+
yes (default):
36+
Add `from htpy import div, span` for all found elements.
37+
h:
38+
Add a single `import htpy as h`.
39+
Reference elements with `h.div`, `h.span`.
40+
no:
41+
Do not add imports.
42+
--no-shorthand Use explicit `id` and `class_` kwargs instead of the shorthand #id.class syntax.
43+
```
44+
45+
## Import Options
46+
47+
You have a couple of options regarding imports with the `-i`/`--imports` flag.
48+
Options are `yes` (default), `h`, `no`.
49+
50+
#### Module import of htpy: `--imports=h`
51+
52+
This mode will use `import htpy as h` instead of importing individual elements from htpy.
53+
54+
```py title="$ html2htpy --imports=h example.html"
55+
import htpy as h
56+
57+
h.section("#main-section.hero.is-link")[
58+
h.p(".subtitle.is-3.is-spaced")["Welcome"]
59+
]
60+
```
61+
62+
## id/classes conversion
63+
64+
html2htpy supports multiple modes to handle id/class conversions:
65+
66+
```html title="example.html"
67+
<section id="main-section" class="hero is-link">
68+
<p class="subtitle is-3 is-spaced">Welcome</p>
69+
</section>
70+
```
71+
72+
### Shorthand
73+
By default, id/classes will be converted to the shorthand notation:
74+
75+
```py title="$ html2htpy example.html"
76+
from htpy import p, section
77+
78+
section("#main-section.hero.is-link")[
79+
p(".subtitle.is-3.is-spaced")["Welcome"]
80+
]
81+
```
82+
83+
### Explicit id/class attributes
84+
85+
Use the `--no-shorthand` flag to get explicit `id` and `class_` asttributes:
86+
87+
```py title="$ html2htpy --no-shorthand example.html"
88+
from htpy import p, section
89+
90+
section(id="main-section", class_="hero is-link")[
91+
p(class_="subtitle is-3 is-spaced")["Welcome"]
92+
]
93+
```
94+
95+
## Django/Jinja variable conversion
96+
97+
`html2htpy` will convert Django/Jinja-style template variables to f-strings:
98+
99+
```
100+
$ echo '<div>hi {{ name }}!</div>' | html2htpy
101+
102+
from htpy import div
103+
104+
div[f"hi { name }!"]
105+
```

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ nav:
3434
- html2htpy.md
3535
- faq.md
3636
- references.md
37+
- Reference:
38+
- reference/html2htpy.md
39+
3740
markdown_extensions:
3841
- admonition
3942
- pymdownx.highlight:

src/htpy/html2htpy.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import shutil
77
import subprocess
88
import sys
9+
import textwrap
910
import typing as t
1011
from abc import ABC, abstractmethod
1112
from html.parser import HTMLParser
@@ -432,33 +433,64 @@ def _is_command_available(command: str) -> bool:
432433

433434

434435
def main() -> None:
435-
parser = argparse.ArgumentParser(prog="html2htpy")
436+
parser = argparse.ArgumentParser(
437+
prog="html2htpy",
438+
formatter_class=argparse.RawTextHelpFormatter,
439+
)
436440

437441
parser.add_argument(
438442
"-f",
439443
"--format",
440444
choices=["auto", "ruff", "black", "none"],
441445
default="auto",
442-
help="Select one of the following formatting options: auto, ruff, black or none",
446+
help=textwrap.dedent(
447+
"""
448+
Format the output code with a code formatter.
449+
450+
auto (default):
451+
- If black is installed (exists on PATH), use `black` for formatting.
452+
- If ruff is installed (exists on PATH): Use `ruff format` for formatting.
453+
- If neither black or ruff is installed, do not perform any formatting.
454+
455+
black:
456+
Use the black formatter (https://black.readthedocs.io/en/stable/).
457+
458+
ruff:
459+
Use the ruff formatter (https://docs.astral.sh/ruff/formatter/).
460+
461+
none:
462+
Do not format the output code at all.
463+
""",
464+
),
443465
)
444466
parser.add_argument(
445467
"-i",
446468
"--imports",
447469
choices=["yes", "h", "no"],
448-
help="Output mode for imports of found htpy elements",
470+
help=textwrap.dedent("""
471+
Specify formatting for imports.
472+
473+
yes (default):
474+
Add `from htpy import div, span` for all found elements.
475+
h:
476+
Add a single `import htpy as h`.
477+
Reference elements with `h.div`, `h.span`.
478+
no:
479+
Do not add imports.
480+
"""),
449481
default="yes",
450482
)
451483
parser.add_argument(
452484
"--no-shorthand",
453-
help="Use explicit `id` and `class_` kwargs instead of the shorthand #id.class syntax",
485+
help="Use explicit `id` and `class_` kwargs instead of the shorthand #id.class syntax.",
454486
action="store_true",
455487
)
456488
parser.add_argument(
457489
"input",
458490
type=argparse.FileType("r"),
459491
nargs="?",
460492
default=sys.stdin,
461-
help="input HTML from file or stdin",
493+
help="Input HTML file, e.g. home.html. Optional. If not specified, html2htpy will read from stdin.",
462494
)
463495

464496
args = parser.parse_args()

0 commit comments

Comments
 (0)