Skip to content

Commit 87770df

Browse files
committed
A route for authors and author.
1 parent 02b8894 commit 87770df

File tree

7 files changed

+102
-5
lines changed

7 files changed

+102
-5
lines changed

src/psc/app.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from psc.resources import Resources
2121
from psc.resources import get_resources
2222

23-
2423
templates = Jinja2Templates(directory=HERE / "templates")
2524

2625

@@ -61,6 +60,40 @@ async def gallery(request: Request) -> _TemplateResponse:
6160
)
6261

6362

63+
async def authors(request: Request) -> _TemplateResponse:
64+
"""Handle the author listing page."""
65+
these_authors: Iterator[Example] = request.app.state.resources.authors.values()
66+
root_path = ".."
67+
68+
return templates.TemplateResponse(
69+
"authors.jinja2",
70+
dict(
71+
title="Authors",
72+
authors=these_authors,
73+
root_path=root_path,
74+
request=request,
75+
),
76+
)
77+
78+
79+
async def author(request: Request) -> _TemplateResponse:
80+
"""Handle an author page."""
81+
author_path = PurePath(request.path_params["author_name"])
82+
resources: Resources = request.app.state.resources
83+
this_author = resources.authors[author_path]
84+
root_path = "../../.."
85+
86+
return templates.TemplateResponse(
87+
"example.jinja2",
88+
dict(
89+
title=this_author.title,
90+
body=this_author.body,
91+
request=request,
92+
root_path=root_path,
93+
),
94+
)
95+
96+
6497
async def example(request: Request) -> _TemplateResponse:
6598
"""Handle an example page."""
6699
example_path = PurePath(request.path_params["example_name"])
@@ -113,6 +146,9 @@ async def content_page(request: Request) -> _TemplateResponse:
113146
Route("/favicon.png", favicon),
114147
Route("/gallery/index.html", gallery),
115148
Route("/gallery", gallery),
149+
Route("/authors/index.html", authors),
150+
Route("/authors", authors),
151+
Route("/authors/{author_name}.html", author),
116152
Route("/gallery/examples/{example_name}/index.html", example),
117153
Route("/gallery/examples/{example_name}/", example),
118154
Route("/pages/{page_name}.html", content_page),

src/psc/resources.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,17 @@ class Example(Resource):
9090
Meaning, HERE / "examples" / name / "index.html".
9191
"""
9292

93-
description: str = ""
9493
subtitle: str = ""
9594

9695
def __post_init__(self) -> None:
9796
"""Extract most of the data from the HTML file."""
98-
# Title, subtitle, description come from the example's MD file.
97+
# Title, subtitle, body come from the example's MD file.
9998
index_md_file = HERE / "gallery/examples" / self.path / "index.md"
10099
md_fm = frontmatter.load(index_md_file)
101100
self.title = md_fm.get("title", "")
102101
self.subtitle = md_fm.get("subtitle", "")
103102
md = MarkdownIt()
104-
self.description = str(md.render(md_fm.content))
103+
self.body = str(md.render(md_fm.content))
105104

106105
# Main, extra head example's HTML file.
107106
index_html_file = HERE / "gallery/examples" / self.path / "index.html"

src/psc/templates/author.jinja2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends "layout.jinja2" %}
2+
{% block extra_head %}
3+
{% block main %}
4+
<main id="main_container" class="container">
5+
<h1 class="title">{{ title }}</h1>
6+
<div class="content">{{ body | safe }}</div>
7+
</main>
8+
{% endblock %}

src/psc/templates/authors.jinja2

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends "layout.jinja2" %}
2+
{% block main %}
3+
<section class="hero is-small is-dark">
4+
<div class="hero-body">
5+
<p class="title">
6+
PyScript Authors
7+
</p>
8+
<p class="subtitle">
9+
All the contributors to authors and more.
10+
</p>
11+
</div>
12+
</section>
13+
<main id="main_container" class="container">
14+
<div class="tile is-ancestor">
15+
{% for author in authors %}
16+
<div class="tile is-parent is-4">
17+
<article class="tile is-child box">
18+
<p class="title"><a
19+
href="{{ root_path }}/authors/{{ author.path.name }}.html/">{{ author.title }}</a>
20+
</p>
21+
<div class="content">
22+
{{ author.body | safe }}
23+
</div>
24+
</article>
25+
</div>
26+
{% endfor %}
27+
</div>
28+
</main>
29+
{% endblock %}

src/psc/templates/gallery.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</p>
2121
<p class="subtitle">{{ example.subtitle }}</p>
2222
<div class="content">
23-
{{ example.description | safe }}
23+
{{ example.body | safe }}
2424
</div>
2525
</article>
2626
</div>

src/psc/templates/layout.jinja2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
<a id="navbarGallery" class="navbar-item" href="{{ root_path }}/gallery">
2727
Gallery
2828
</a>
29+
<a id="navbarAuthors" class="navbar-item" href="{{ root_path }}/authors">
30+
Authors
31+
</a>
2932
<a id="navbarJoin" class="navbar-item" href="{{ root_path }}/pages/contributing.html">
3033
Join
3134
</a>

tests/test_author_pages.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Use the routes to render listing of authors and each one."""
2+
from psc.fixtures import PageT
3+
4+
5+
def test_authors_page(client_page: PageT) -> None:
6+
"""The listing of authors works."""
7+
soup = client_page("/authors/index.html")
8+
page_title = soup.select_one("title")
9+
assert page_title and "Authors | PyScript Collective" == page_title.text
10+
11+
authors = soup.select_one("article.tile p.title")
12+
assert "Margaret" == authors.text.strip()
13+
14+
15+
def test_author_page(client_page: PageT) -> None:
16+
"""The page for an author works."""
17+
soup = client_page("/authors/meg-1.html")
18+
page_title = soup.select_one("title")
19+
assert page_title and "Margaret | PyScript Collective" == page_title.text
20+
21+
author = soup.select_one("main h1")
22+
assert "Margaret" == author.text.strip()

0 commit comments

Comments
 (0)