Skip to content

Commit bbd92f2

Browse files
committed
Nicer URLs, refs simonw#34
1 parent 5db7166 commit bbd92f2

12 files changed

+107
-25
lines changed

.github/workflows/build.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- name: Download previous database unless REBUILD in commit message
4848
if: |-
4949
!contains(github.event.head_commit.message, 'REBUILD')
50-
run: curl --fail -o main/til.db https://raw.githubusercontent.com/simonw/til-db/main/til.db
50+
run: curl --fail -o main/tils.db https://raw.githubusercontent.com/simonw/til-db/main/tils.db
5151
continue-on-error: true
5252
- name: Build database
5353
run: |-
@@ -74,11 +74,11 @@ jobs:
7474
git config --global user.name "README-bot"
7575
git diff --quiet || (git add README.md && git commit -m "Updated README")
7676
git push
77-
- name: Save til.db to simonw/til-db
77+
- name: Save tils.db to simonw/til-db
7878
run: |-
7979
cd til-db
80-
cp ../main/til.db .
81-
git add til.db
80+
cp ../main/tils.db .
81+
git add tils.db
8282
git commit --amend --no-edit
8383
git push --force
8484
- name: Setup Node.js
@@ -90,7 +90,7 @@ jobs:
9090
NOW_TOKEN: ${{ secrets.NOW_TOKEN }}
9191
run: |-
9292
cd main
93-
datasette publish now til.db \
93+
datasette publish now tils.db \
9494
--token $NOW_TOKEN \
9595
--branch main \
9696
--project simon-til \

build_database.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ def created_changed_times(repo_path, ref="main"):
3535

3636
def build_database(repo_path):
3737
all_times = created_changed_times(repo_path)
38-
db = sqlite_utils.Database(repo_path / "til.db")
38+
db = sqlite_utils.Database(repo_path / "tils.db")
3939
table = db.table("til", pk="path")
4040
for filepath in root.glob("*/*.md"):
4141
fp = filepath.open()
4242
title = fp.readline().lstrip("#").strip()
4343
body = fp.read().strip()
4444
path = str(filepath.relative_to(root))
45+
slug = filepath.stem
4546
url = "https://github.com/simonw/til/blob/main/{}".format(path)
4647
# Do we need to render the markdown?
4748
path_slug = path.replace("/", "_")
@@ -54,13 +55,15 @@ def build_database(repo_path):
5455
previous_html = None
5556
record = {
5657
"path": path_slug,
58+
"slug": slug,
5759
"topic": path.split("/")[0],
5860
"title": title,
5961
"url": url,
6062
"body": body,
6163
}
6264
if (body != previous_body) or not previous_html:
6365
retries = 0
66+
response = None
6467
while retries < 3:
6568
headers = {}
6669
if os.environ.get("GITHUB_TOKEN"):

generate_screenshots.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
(root / "templates" / "til_base.html"),
1212
]
1313

14+
1415
def png_for_path(path):
1516
page_html = str(TMP_PATH / "generate-screenshots-page.html")
1617
# Use datasette to generate HTML
@@ -33,7 +34,7 @@ def png_for_path(path):
3334

3435

3536
def generate_screenshots(root):
36-
db = sqlite_utils.Database(root / "til.db")
37+
db = sqlite_utils.Database(root / "tils.db")
3738

3839
# The shot_hash incorporates a hash of all of row.html
3940

@@ -47,7 +48,7 @@ def generate_screenshots(root):
4748
html = row["html"]
4849
shot_hash = hashlib.md5((shot_html_hash + html).encode("utf-8")).hexdigest()
4950
if shot_hash != row.get("shot_hash"):
50-
png = png_for_path("/til/til/{}".format(path))
51+
png = png_for_path("/tils/til/{}".format(path))
5152
db["til"].update(path, {"shot": png, "shot_hash": shot_hash}, alter=True)
5253
print(
5354
"Got {} byte PNG for {} shot hash {}".format(len(png), path, shot_hash)

github-actions/continue-on-error.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ I wanted to have a GitHub Action step run that might fail, but if it failed the
66

77
```yaml
88
- name: Download previous database
9-
run: curl --fail -o til.db https://til.simonwillison.net/til.db
9+
run: curl --fail -o tils.db https://til.simonwillison.net/tils.db
1010
continue-on-error: true
1111
- name: Build database
1212
run: python build_database.py
1313
```
1414
1515
[From this workflow](https://github.com/simonw/til/blob/7d799a24921f66e585b8a6b8756b7f8040c899df/.github/workflows/build.yml#L32-L36)
1616
17-
I'm using `curl --fail` here which returns an error code if the file download files (without `--fail` it was writing out a two line error message to a file called `til.db` which is not what I wanted). Then `continue-on-error: true` to keep on going even if the download failed.
17+
I'm using `curl --fail` here which returns an error code if the file download files (without `--fail` it was writing out a two line error message to a file called `tils.db` which is not what I wanted). Then `continue-on-error: true` to keep on going even if the download failed.
1818

19-
My `build_database.py` script updates the `til.db` database file if it exists and creates it from scratch if it doesn't.
19+
My `build_database.py` script updates the `tils.db` database file if it exists and creates it from scratch if it doesn't.

metadata.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins:
88
screenshot:
99
"sql": "select shot as content, 'image/png' as content_type from til where path=:key"
1010
databases:
11-
til:
11+
tils:
1212
queries:
1313
search: |
1414
select
@@ -21,7 +21,7 @@ databases:
2121
where
2222
til_fts match case
2323
:q
24-
when '' then '*'
24+
when '' then 'nomatchforthisterm'
2525
else escape_fts(:q)
2626
end
2727
order by
@@ -32,7 +32,7 @@ databases:
3232
SELECT
3333
'tag:til.simonwillison.net,2020-04-30:' || path as atom_id,
3434
title as atom_title,
35-
'https://til.simonwillison.net/til/til/' || path as atom_link,
35+
'https://til.simonwillison.net/' || topic || '/' || slug as atom_link,
3636
created_utc as atom_updated,
3737
html as atom_content_html,
3838
'Simon Willison' as atom_author_name,

plugins/redirects.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from datasette import hookimpl
2+
from datasette.utils.asgi import Response
3+
4+
5+
@hookimpl
6+
def register_routes():
7+
return (
8+
("^/til/feed.atom$", lambda: Response.redirect("/tils/feed.atom", status=301)),
9+
(
10+
"^/til/search$",
11+
lambda request: Response.redirect(
12+
"/tils/search"
13+
+ (("?" + request.query_string) if request.query_string else ""),
14+
status=301,
15+
),
16+
),
17+
)

requirements.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
GitPython
22
sqlite-utils>=2.19
33
beautifulsoup4
4-
datasette
4+
datasette>=0.51.1
55
datasette-atom>=0.7
66
datasette-publish-now>=0.6
77
datasette-template-sql>=1.0
88
datasette-graphql
99
datasette-media
10-
httpx

templates/index.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{% block extra_head %}
66
<link href="https://github.com/simonw" rel="me">
7-
<link rel="alternate" type="application/atom+xml" title="Atom" href="/til/feed.atom">
7+
<link rel="alternate" type="application/atom+xml" title="Atom" href="/tils/feed.atom">
88
<style>
99
input[type=search] {
1010
padding: .25em;
@@ -35,7 +35,7 @@
3535
<h1>Simon Willison: TIL</h1>
3636
<p>Things I've learned, collected in <a href="https://github.com/simonw/til">simonw/til</a>.</p>
3737

38-
<p><a style="text-decoration: none" href="/til/feed.atom" title="Atom feed"><svg xmlns="http://www.w3.org/2000/svg" width="14px" height="14px" viewBox="0 0 256 256">
38+
<p><a style="text-decoration: none" href="/tils/feed.atom" title="Atom feed"><svg xmlns="http://www.w3.org/2000/svg" width="14px" height="14px" viewBox="0 0 256 256">
3939
<defs>
4040
<linearGradient id="a" x1=".1" x2=".9" y1=".1" y2=".9">
4141
<stop offset="0" stop-color="#E3702D" />
@@ -55,9 +55,9 @@ <h1>Simon Willison: TIL</h1>
5555
<path fill="#FFF" d="M184 213A140 140 0 0 0 44 73V38a175 175 0 0 1 175 175z" />
5656
</svg> Atom feed</a></p>
5757

58-
<p><strong>Recently added:</strong> {% for row in sql("select * from til order by created_utc desc limit 5") %}<a href="/til/til/{{ row.path }}">{{ row.title }}</a>{% if not loop.last %}, {% endif %}{% endfor %}</p>
58+
<p><strong>Recently added:</strong> {% for row in sql("select * from til order by created_utc desc limit 5") %}<a href="/{{ row.topic }}/{{ row.slug }}">{{ row.title }}</a>{% if not loop.last %}, {% endif %}{% endfor %}</p>
5959

60-
<form action="/til/search">
60+
<form action="/tils/search">
6161
<p>
6262
<input type="search" name="q" placeholder="Search {{ sql("select count(*) from TIL")[0][0] }} TILs">
6363
<input type="submit" value="Search">
@@ -68,7 +68,7 @@ <h1>Simon Willison: TIL</h1>
6868
<h2>{{ row.topic }}</h2>
6969
<ul>
7070
{% for til in sql("select * from til where topic = :topic order by created_utc desc", {"topic": row.topic}) %}
71-
<li><a href="/til/til/{{ til.path }}">{{ til.title }}</a> - {{ til.created[:10] }}</li>
71+
<li><a href="/{{ til.topic }}/{{ til.slug }}">{{ til.title }}</a> - {{ til.created[:10] }}</li>
7272
{% endfor %}
7373
</ul>
7474
{% endfor %}

templates/pages/{topic}.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% extends "til_base.html" %}
2+
3+
{% set tils = sql("""
4+
select * from til where topic = :topic order by created_utc
5+
""", {"topic": topic})
6+
%}
7+
8+
{% block title %}Simon Willison: TILs on {{ topic }}{% endblock %}<!DOCTYPE html>
9+
10+
{% block body %}
11+
12+
{% if not tils %}
13+
{{ raise_404("No TILs found") }}
14+
{% endif %}
15+
16+
<h1>Simon Willison: TILs on {{ topic }}</h1>
17+
18+
<ul>
19+
{% for til in tils %}
20+
<li><a href="/{{ til.topic }}/{{ til.slug }}">{{ til.title }}</a> - {{ til.created[:10] }}</li>
21+
{% endfor %}
22+
</ul>
23+
24+
{% endblock %}

templates/pages/{topic}/{slug}.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{% extends "til_base.html" %}
2+
3+
{% set tils = sql("""
4+
select * from til where path = :topic || '_' || :slug || '.md'
5+
""", {"topic": topic, "slug": slug})
6+
%}
7+
{% set til = tils[0] %}
8+
{% if not tils %}
9+
{% set raised = raise_404("TIL not found") %}
10+
{% endif %}
11+
12+
{% block title %}{{ til.title }} | Simon Willison’s TILs{% endblock %}
13+
14+
{% block extra_head %}
15+
<link rel="stylesheet" href="/static/github-light.css">
16+
<meta name="twitter:card" content="summary_large_image">
17+
<meta name="twitter:creator" content="@simonw">
18+
<meta name="twitter:title" content="{{ til.title }}">
19+
<meta name="twitter:description" content="{{ til.body }}">
20+
<meta name="twitter:image" content="https://til.simonwillison.net/-/media/screenshot/{{ til.path }}">
21+
<meta name="twitter:image:alt" content="Screenshot of this article">
22+
<meta property="og:url" content="https://til.simonwillison.net/til/til/{{ til.path }}">
23+
<meta property="og:type" content="article">
24+
<meta property="og:title" content="{{ til.title }}">
25+
<meta property="og:description" content="{{ til.body }}">
26+
<meta property="og:image" content="https://til.simonwillison.net/-/media/screenshot/{{ til.path }}">
27+
<meta property="og:image:alt" content="Screenshot of this article">
28+
<meta property="og:image:width" content="800">
29+
<meta property="og:image:height" content="400">
30+
{% endblock %}
31+
32+
{% block body %}
33+
<h1>{{ til.title }}</h1>
34+
35+
{{ til.html|safe }}
36+
37+
<p class="created">Created {{ til.created }}{% if til.created != til.updated %}, updated {{ til.updated }} &middot; <a href="{{ til.url|replace("https://github.com/simonw/til/blob/", "https://github.com/simonw/til/commits/") }}">History</a>{% endif %} &middot; <a href="{{ til.url }}">Edit</a></p>
38+
{% endblock %}

templates/query-til-search.html renamed to templates/query-tils-search.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
{% block body %}
4646
<h1>TIL search: {{ q }}</h1>
4747

48-
<form action="/til/search">
48+
<form action="/tils/search">
4949
<p>
5050
<input type="search" name="q" value="{{ q }}">
5151
<input type="submit" value="Search">
@@ -57,7 +57,7 @@ <h1>TIL search: {{ q }}</h1>
5757
{% endif %}
5858

5959
{% for row in rows %}
60-
<p><span class="topic">{{ row.topic }}</span> <a href="/til/til/{{ row.path }}">{{ row.title }}</a> - {{ row.created[:10] }}</p>
60+
<p><span class="topic">{{ row.topic }}</span> <a href="/{{ row.topic }}/{{ row.slug }}">{{ row.title }}</a> - {{ row.created[:10] }}</p>
6161
<pre>{{ highlight(row.snippet)|safe }}</pre>
6262
{% endfor %}
6363

update_readme.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"Run this after build_database.py - it needs til.db"
1+
"Run this after build_database.py - it needs tils.db"
22
import pathlib
33
import sqlite_utils
44
import sys
@@ -12,7 +12,7 @@
1212
COUNT_TEMPLATE = "<!-- count starts -->{}<!-- count ends -->"
1313

1414
if __name__ == "__main__":
15-
db = sqlite_utils.Database(root / "til.db")
15+
db = sqlite_utils.Database(root / "tils.db")
1616
by_topic = {}
1717
for row in db["til"].rows_where(order_by="created_utc"):
1818
by_topic.setdefault(row["topic"], []).append(row)

0 commit comments

Comments
 (0)