Repository navigation
Expand file tree
/
Copy pathgenerate.py
More file actions
275 lines (224 loc) Β· 8.58 KB
/
Copy pathgenerate.py
File metadata and controls
275 lines (224 loc) Β· 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from __future__ import annotations
from datetime import datetime, timedelta
import json
from pathlib import Path
from fnmatch import fnmatch
import yaml
from jinja2 import Environment, FileSystemLoader
from jinja_markdown2 import MarkdownExtension # type: ignore[import-untyped]
from pydantic import BaseModel, ConfigDict, Field
ICONS = {
'github.com': 'fa-brands fa-github',
'gitlab.com': 'fa-brands fa-gitlab',
'instagram.com': 'fa-brands fa-instagram',
'www.instagram.com': 'fa-brands fa-instagram',
'codeberg.org': 'fa-brands fa-codeberg',
'*.sr.ht': 'fa-regular fa-circle',
'sr.ht': 'fa-regular fa-circle',
'*.itch.io': 'fa-brands fa-itch-io',
'home': 'fa-solid fa-home',
'homepage': 'fa-solid fa-home',
'mastodon': 'fa-brands fa-mastodon',
}
def get_icon(name: str, url: str) -> str | None:
icon = ICONS.get(name)
if icon is not None:
return icon
url = url.removeprefix('https://')
url = url.split('/')[0]
icon = ICONS.get(url)
if icon is not None:
return icon
for pattern, icon in ICONS.items():
if fnmatch(url, pattern):
return icon
return None
class Category(BaseModel):
group_slug: str = Field(pattern=r'^[a-z-]{3,}$')
slug: str = Field(pattern=r'^[a-zA-Z0-9-]{1,}$')
name: str = Field(min_length=1)
icon: str | None = Field(pattern=r'^fa.+$', default=None)
model_config = ConfigDict(extra='forbid')
@property
def full_slug(self) -> str:
return f'{self.group_slug}/{self.slug}'
class Author(BaseModel):
id: str = Field(pattern=r'^.{1,16}$')
name: str = Field(min_length=2, max_length=40)
pronouns: str | None = Field(
pattern=r'^[a-z]{1,8}/[a-z]{1,8}$', default=None,
)
links: dict[str, str] = Field(min_length=1, max_length=16)
short: str = Field(min_length=4, max_length=140)
about: str | None = Field(min_length=10, max_length=10_000, default=None)
model_config = ConfigDict(extra='forbid')
@property
def first_link(self) -> tuple[str, str]:
return list(self.links.items())[0]
@property
def github_username(self) -> str | None:
prefix = 'https://github.com/'
for link in self.links.values():
if not link.startswith(prefix):
continue
username = link.removeprefix(prefix)
username = username.strip('/')
if username and '/' not in username:
return username
return None
class Badge(BaseModel):
text: str
icon: str = Field(pattern=r'^fa-')
class App(BaseModel):
id: str = Field(pattern=r'^.{1,16}\..{1,16}$')
name: str = Field(min_length=2, max_length=40)
badge: Badge | None = None
author: Author
coauthor: Author | None = None
short: str = Field(min_length=4, max_length=140)
added: str = Field(pattern=r'^20[234][0-9]-[01][0-9]-[0123][0-9]$')
download: str = Field(pattern=r'^https://.+\.')
links: dict[str, str] | None = Field(max_length=16, default=None)
icon: str | None = Field(pattern=r'^fa-', default=None)
categories: list[str] = Field(min_length=1)
desc: str = Field(min_length=10, max_length=10_000)
model_config = ConfigDict(extra='forbid')
@property
def direct(self) -> bool:
"""True if the download link is a direct download for the file.
"""
return self.download.endswith('.zip')
@property
def visible(self) -> bool:
return self.author.id != 'sys'
@property
def new(self) -> bool:
month_ago = datetime.now() - timedelta(days=30)
return self.added >= month_ago.strftime('%Y-%m-%d')
@property
def splash(self) -> str | None:
path = Path(__file__).parent / 'splash' / f'{self.id}.png'
if path.exists():
return f'./splash/{self.id}.png'
return None
def authored_by(self, id: str) -> bool:
if self.coauthor and self.coauthor.id == id:
return True
return self.author.id == id
def short_dump(self) -> dict[str, object]:
"""Get a dictionary with the most basic attributes of the app.
Used to generate the JSON list of all apps.
"""
author_name = self.author.name
if self.coauthor:
author_name = f"{author_name} & {self.coauthor.name}"
return {
'id': self.id,
'name': self.name,
'author': author_name,
'short': self.short,
'added': self.added,
}
class Categories(BaseModel):
categories: list[Category] = Field(min_length=20)
def get(self, full_slug: str) -> Category:
for cat in self.categories:
if cat.full_slug == full_slug:
return cat
raise LookupError(f'invalid category: {full_slug}')
env = Environment(loader=FileSystemLoader('templates'))
env.add_extension(MarkdownExtension)
public_dir = Path('public')
public_dir.mkdir(exist_ok=True)
def load_apps() -> list[App]:
apps: list[App] = []
for app_path in Path('apps').iterdir():
author_id, _app_id, ext = app_path.name.split('.')
assert ext == 'yaml'
app_raw = yaml.safe_load(app_path.read_text())
author_path = Path('authors', f'{author_id}.yaml')
author_raw = yaml.safe_load(author_path.read_text())
author = Author(id=author_id, **author_raw)
coauthor: Author | None = None
coauthor_id = app_raw.pop('coauthor', None)
if coauthor_id:
coauthor_path = Path('authors', f'{coauthor_id}.yaml')
coauthor_raw = yaml.safe_load(coauthor_path.read_text())
coauthor = Author(id=coauthor_id, **coauthor_raw)
app = App(
id=app_path.stem,
author=author,
coauthor=coauthor,
**app_raw,
)
apps.append(app)
apps.sort(key=lambda a: a.name.lower())
apps.sort(key=lambda a: a.added, reverse=True)
return apps
def load_authors() -> list[Author]:
authors: list[Author] = []
for author_path in Path('authors').iterdir():
author_raw = yaml.safe_load(author_path.read_text())
author = Author(id=author_path.stem, **author_raw)
if author.id in ('index', 'apps', 'authors'):
raise AssertionError(f'Reserved author ID: {author.id}')
authors.append(author)
return authors
def load_categories() -> Categories:
categories = []
groups = yaml.safe_load(Path('categories.yaml').read_text())
for group_slug, group in groups.items():
for slug, category in group.items():
categories.append(Category(
group_slug=group_slug,
slug=slug,
**category,
))
return Categories(categories=categories)
def main() -> None:
apps = load_apps()
authors = load_authors()
categories = load_categories()
out_dir = Path('public')
# sort authors, put the authors with the most apps first
authors.sort(key=lambda author: (
-len([1 for app in apps if app.author.id == author.id]),
author.id,
))
# render list of apps and some other pages
for slug in ('index', '404', 'random'):
template = env.get_template(f'{slug}.html.j2')
content = template.render(apps=apps)
(out_dir / f'{slug}.html').write_text(content)
short_apps = [a.short_dump() for a in apps]
(out_dir / 'apps.json').write_text(json.dumps(short_apps, indent=1))
# render page for each app
template = env.get_template('app.html.j2')
for app in apps:
app_cats = [categories.get(slug) for slug in app.categories]
content = template.render(
app=app,
get_icon=get_icon,
categories=app_cats,
)
(out_dir / f'{app.id}.html').write_text(content)
(out_dir / f'{app.id}.json').write_text(app.model_dump_json())
# render list of authors
template = env.get_template('authors.html.j2')
content = template.render(authors=authors, get_icon=get_icon)
(out_dir / 'authors.html').write_text(content)
# render page for each author
template = env.get_template('author.html.j2')
for author in authors:
author_apps = [app for app in apps if app.authored_by(author.id)]
content = template.render(
author=author,
apps=author_apps,
get_icon=get_icon,
)
(out_dir / f'@{author.id}.html').write_text(content)
author_dict = author.model_dump()
author_dict["apps"] = [app.short_dump() for app in author_apps]
author_json = json.dumps(author_dict, separators=(",", ":"))
(out_dir / f'@{author.id}.json').write_text(author_json)
main()