Skip to content

Commit e8d3e8a

Browse files
Merge pull request #110 from COMP1010UNSW/maddy-render-options
Implement rendering options
2 parents 97a0b3c + 7004c26 commit e8d3e8a

32 files changed

+1232
-1032
lines changed

.github/workflows/mkdocs-dryrun.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
id: python
2525
uses: actions/setup-python@v5
2626
with:
27-
python-version: '3.9'
27+
python-version: '3.10'
2828

2929
# Install dependencies using Poetry
3030
- uses: Gr1N/setup-poetry@v9

.github/workflows/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
id: python
3737
uses: actions/setup-python@v5
3838
with:
39-
python-version: '3.9'
39+
python-version: '3.10'
4040

4141
# Install dependencies using Poetry
4242
- uses: Gr1N/setup-poetry@v9

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set up Python
1515
uses: actions/setup-python@v5
1616
with:
17-
python-version: 3.9
17+
python-version: '3.10'
1818

1919
# Install dependencies using Poetry
2020
- uses: Gr1N/setup-poetry@v9

.github/workflows/python-app.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ jobs:
2626
- uses: actions/checkout@v4
2727
with:
2828
submodules: 'recursive'
29-
- name: Set up Python 3.9
29+
- name: Set up Python
3030
uses: actions/setup-python@v5
3131
with:
32-
python-version: 3.9
32+
python-version: '3.10'
3333
- uses: Gr1N/setup-poetry@v9
3434
- uses: actions/cache@v4
3535
with:
@@ -60,10 +60,10 @@ jobs:
6060
- uses: actions/checkout@v4
6161
with:
6262
submodules: 'recursive'
63-
- name: Set up Python 3.9
63+
- name: Set up Python
6464
uses: actions/setup-python@v5
6565
with:
66-
python-version: 3.9
66+
python-version: '3.10'
6767
- uses: Gr1N/setup-poetry@v9
6868
- uses: actions/cache@v4
6969
with:
@@ -81,10 +81,10 @@ jobs:
8181
- uses: actions/checkout@v4
8282
with:
8383
submodules: 'recursive'
84-
- name: Set up Python 3.9
84+
- name: Set up Python
8585
uses: actions/setup-python@v5
8686
with:
87-
python-version: 3.9
87+
python-version: '3.10'
8888
- uses: Gr1N/setup-poetry@v9
8989
- uses: actions/cache@v4
9090
with:

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ Learn more by reading [the documentation](https://comp1010unsw.github.io/pyhtml-
4646
<h1>
4747
Hello, world!
4848
</h1>
49-
<p>
50-
This is my amazing website!
51-
</p>
49+
<p>This is my amazing website!</p>
5250
</body>
5351
</html>
5452

docs/api.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
- attributes
1010
- "_get_tag_name"
1111
- "_get_default_attributes"
12+
- "_get_default_render_options"
1213
- "_get_tag_pre_content"
13-
- "_render"
1414
- "_escape_children"
15+
- "_render"
1516

1617
::: pyhtml.SelfClosingTag
1718
options:
@@ -20,3 +21,10 @@
2021
::: pyhtml.WhitespaceSensitiveTag
2122
options:
2223
members:
24+
25+
::: pyhtml.RenderOptions
26+
options:
27+
members:
28+
- indent
29+
- spacing
30+
- union

docs/compatibility.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,29 @@ modify the original instance, as I found the old behaviour confusing and
2929
bug-prone.
3030

3131
```py
32-
>>> para = p.p("Base paragraph")
33-
>>> para2 = para("Extra text")
34-
>>> para2
35-
<p>
32+
>>> span1 = p.span("Base paragraph")
33+
>>> span2 = span1("Extra text")
34+
>>> span2
35+
<span>
3636
Base paragraph
3737
Extra text
38-
</p>
39-
>>> para
40-
<p>
38+
</span>
39+
>>> span1
40+
<span>
4141
Base paragraph
42-
</p>
42+
</span>
43+
44+
```
45+
46+
## Spacing between tag instances
47+
48+
Certain tags have default spacing, which helps to avoid subtle rendering
49+
annoyances such as unwanted spaces between linked text and punctuation in
50+
paragraphs.
51+
52+
```py
53+
>>> print(str(p.p(p.a(href="https://example.com")("Example website"), "!")))
54+
<p><a href="https://example.com">Example website</a>!</p>
55+
>>> # Notice how there is no spacing between the link and the exclamation point
4356

4457
```

docs/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ Build HTML documents in Python with a simple and learnable syntax.
3232
<h1>
3333
Hello, world!
3434
</h1>
35-
<p>
36-
This is my amazing website!
37-
</p>
35+
<p>This is my amazing website!</p>
3836
</body>
3937
</html>
4038

docs/learn/advanced.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,29 @@ for person in staff_members:
5858
))
5959
```
6060

61-
### HTML comments
61+
## Rendering options
62+
63+
If you need more control over how PyHTML renders your output HTML, you can use
64+
the [`p.RenderOptions`][pyhtml.RenderOptions] class to specify these.
65+
66+
For example, if you don't want any whitespace between your HTML components,
67+
you can use `p.RenderOptions(spacing="")` to remove it.
68+
69+
```py
70+
>>> import pyhtml as p
71+
>>> print(str(p.div(p.RenderOptions(spacing=""))(
72+
... p.i("No"),
73+
... p.b("spaces"),
74+
... p.u("here!")
75+
... )))
76+
<div><i>No</i><b>spaces</b><u>here!</u></div>
77+
78+
```
79+
80+
For more information about the specific allowed properties, view
81+
[this documentation][pyhtml.RenderOptions].
82+
83+
## HTML comments
6284

6385
You can add comments to HTML by using the `Comment` tag. These will be included
6486
in the output HTML, which can be useful for debugging your server from your web
@@ -77,8 +99,8 @@ browser.
7799
## Embedding raw HTML
78100

79101
By default, PyHTML [escapes certain characters](https://www.w3schools.com/html/html_entities.asp)
80-
within strings passed to tags. This is done to avoid user content from taking
81-
control of your resultant webpages in what is called a
102+
within strings passed to tags. This is done to prevent user content from taking
103+
control of your webpages in what is called a
82104
[cross-site scripting (XSS) attack](https://owasp.org/www-community/attacks/xss/).
83105

84106
Sometimes, you may wish to embed an existing HTML string inside of PyHTML. You
@@ -94,13 +116,13 @@ can do this using the `p.DangerousRawHtml` tag.
94116

95117
***Be careful though!*** PyHTML escapes these sequences for good reason, so
96118
don't use `DangerousRawHtml` unless you have a *very good reason*, and are
97-
certain that your text is trusted.
119+
certain that the text you are passing it is trusted.
98120

99121
## Custom tags
100122

101123
Since this library includes all modern HTML tags, it is very unlikely that
102124
you'll need to do create a custom tag. However if you really need to, you can
103-
use the `create_tag` function
125+
use the `create_tag` function.
104126

105127
```py
106128
>>> import pyhtml as p

docs/stylesheets/extra.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
--md-primary-fg-color: #ffdf77 !important;
66
}
77

8+
/* Fix spacing around nested nav items */
9+
.md-nav__item--nested {
10+
padding-bottom: 8px !important;
11+
}
12+
813
.md-header {
914
/* Text colour on header */
1015
color: #000000 !important;

0 commit comments

Comments
 (0)