Skip to content

Commit 4a1d5dc

Browse files
committed
feat: add links to view and edit pages in github
1 parent 2b6f103 commit 4a1d5dc

10 files changed

Lines changed: 67 additions & 3 deletions

File tree

src/pallets/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def create_app() -> Flask:
2020
SERVER_NAME="127.0.0.1:5000",
2121
SQLALCHEMY_ENGINES={"default": "sqlite://"},
2222
FORWARDED=dict(FOR=0, PROTO=0, HOST=0, PORT=0, PREFIX=0),
23+
GITHUB_REPO="https://github.com/pallets/website",
2324
)
2425
app.config.from_prefixed_env()
2526

src/pallets/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sqlalchemy as sa
99
import sqlalchemy.orm as orm
1010
from feedgen.feed import FeedGenerator
11+
from flask import current_app
1112
from flask import url_for
1213

1314
from . import db
@@ -17,6 +18,7 @@
1718

1819
class BasePage(Model):
1920
content_prefix: str = ""
21+
content_ext: str = ".md"
2022
__abstract__ = True
2123
path: orm.Mapped[str] = orm.mapped_column(primary_key=True)
2224
is_dir: orm.Mapped[bool] = orm.mapped_column(default=False)
@@ -31,13 +33,31 @@ def __init__(self, **kwargs: t.Any) -> None:
3133
path = posixpath.join(self.content_prefix, posixpath.dirname(self.path))
3234
self.content_html = render_content(self.content, path)
3335

36+
@property
37+
def content_file(self) -> str:
38+
prefix = posixpath.join("content", self.content_prefix, "")
39+
if self.is_dir:
40+
return f"{prefix}{self.path}/index{self.content_ext}"
41+
return f"{prefix}{self.path}{self.content_ext}"
42+
43+
@property
44+
def github_edit_url(self) -> str:
45+
repo = current_app.config["GITHUB_REPO"]
46+
return f"{repo}/edit/main/{self.content_file}"
47+
48+
@property
49+
def github_view_url(self) -> str:
50+
repo = current_app.config["GITHUB_REPO"]
51+
return f"{repo}/blob/main/{self.content_file}"
52+
3453

3554
class Page(BasePage):
3655
__tablename__ = "page"
3756

3857

3958
class Person(BasePage):
4059
content_prefix = "people"
60+
content_ext = ".toml"
4161
__tablename__ = "person"
4262
name: orm.Mapped[str]
4363
nickname: orm.Mapped[str | None]

src/pallets/static/style.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ footer {
2828
align-items: start;
2929
}
3030

31+
.edit-this-page {
32+
float: right;
33+
display: inline-flex;
34+
align-items: center;
35+
font-size: 1rem;
36+
color: var(--pico-muted-color);
37+
margin-top: 0.3em;
38+
padding: 0.25em 0.6em;
39+
border-radius: var(--pico-border-radius);
40+
transition: color 0.2s, background-color 0.2s;
41+
vertical-align: middle;
42+
}
43+
44+
.edit-this-page:hover {
45+
color: var(--pico-primary);
46+
background-color: var(--pico-muted-background);
47+
}
48+
49+
.edit-this-page svg {
50+
width: 1em;
51+
height: 1em;
52+
fill: currentColor;
53+
}
54+
3155
footer ul {
3256
display: grid;
3357
}

src/pallets/templates/blog/post.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{% extends "layout.html" %}
2+
{% from "macros.html" import page_links %}
23

34
{% block page %}
5+
{{ page_links(page.github_view_url, page.github_edit_url) }}
46
<h1>{{ page.title }}</h1>
57
Posted by {{ page.author_name }} on {{ page.published.strftime("%Y-%m-%d") }}
68
<hr>
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

src/pallets/templates/macros.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% macro page_links(view_url, edit_url) %}
2+
<a href="{{ edit_url }}" class="edit-this-page" title="Edit this page on GitHub" aria-label="Edit this page on GitHub">
3+
{% include "icons/pencil.svg" %}
4+
</a>
5+
<a href="{{ view_url }}" class="edit-this-page" title="View this page on GitHub" aria-label="View this page on GitHub">
6+
{% include "icons/eye.svg" %}
7+
</a>
8+
{% endmacro %}

src/pallets/templates/page.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{% extends "layout.html" %}
2+
{% from "macros.html" import page_links %}
23

3-
{% block page %}{{ page.content_html | safe }}{% endblock %}
4+
{% block page %}
5+
{{ page_links(page.github_view_url, page.github_edit_url) }}
6+
{{ page.content_html | safe }}
7+
{% endblock %}

src/pallets/templates/person.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{% extends "layout.html" %}
2+
{% from "macros.html" import page_links %}
23

34
{% block page %}
45
<hgroup>
56
{% if page.name %}
6-
<h1>{{ page.name }}</h1>
7+
<h1>{{ page.name }} {{ page_links(page.github_view_url, page.github_edit_url) }}</h1>
78
<p>{{ page.nickname }}</p>
89
{% else %}
9-
<h1>{{ page.nickname }}</h1>
10+
<h1>{{ page.nickname }} {{ page_links(page.github_view_url, page.github_edit_url) }}</h1>
1011
{% endif %}
1112
</hgroup>
1213
<nav>

src/pallets/templates/project.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "layout.html" %}
2+
{% from "macros.html" import page_links %}
23

34
{% block page %}
45
<section style="text-align: center;">
@@ -15,6 +16,7 @@ <h1 class="title-replace">{{ page.name }}</h1>
1516
<span></span>
1617
</nav>
1718
<section>
19+
{{ page_links(page.github_view_url, page.github_edit_url) }}
1820
{{ page.content_html | safe }}
1921
</section>
2022
{% endblock %}

0 commit comments

Comments
 (0)