forked from apjanco/borsht
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
72 lines (61 loc) · 2.45 KB
/
main.py
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
import srsly
import frontmatter
import markdown
from pathlib import Path
from fastapi import FastAPI, Request, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
app = FastAPI()
app.mount("/assets", StaticFiles(directory="assets"), name="assets")
templates = Jinja2Templates(directory="templates")
topics_path = Path.cwd() / 'data' / 'topics'
links_path = Path.cwd() / 'data' / 'links'
# and then Gwich'in Response to Trump's sale of land.
subtopics = [
'current-activists-and-their-stories',
'gwich’in-steering-committee-who-are-they-and-what-are-their-goals',
'gwichin-activism-through-education-on-current-issues',
'history-of-activism-and-case-against-ansca',
'gwich’in-response-to-trump’s-sale-of-land'
]
def load_topics(stem=None):
if stem:
try:
topic_files = [(a.stem,frontmatter.load(a)) for a in topics_path.iterdir() if a.stem == stem]
except IndexError:
raise HTTPException(status_code=404, detail="Topic not found")
else:
topic_files = [(a.stem,frontmatter.load(a)) for a in topics_path.iterdir() if a.stem not in subtopics]
topics = []
for t in topic_files:
stem = t[0]
md = t[1]
md.stem = stem
md.content = markdown.markdown(md.content)
topics.append(md)
return topics
def load_links(slug=None): #full title of topic
if slug:
try:
topic_title = [frontmatter.load(a)['title'] for a in topics_path.iterdir() if a.stem == slug][0]
links = [frontmatter.load(a).metadata for a in links_path.iterdir() if frontmatter.load(a).metadata['topic'] == topic_title]
except IndexError:
raise HTTPException(status_code=404, detail="Topic not found")
else:
links = [frontmatter.load(a).metadata for a in links_path.iterdir()]
return links
@app.get("/")
def index(request:Request):
context = {"request": request}
context['topics'] = load_topics()
return templates.TemplateResponse("index.html", context)
@app.get("/topic/{slug}")
def topics(request:Request, slug:str):
context = {"request": request}
context['topics'] = load_topics()
context['page'] = load_topics(stem=slug)[0]
context['links'] = load_links(slug=slug)
filters = []
[filters.extend(a['filters']) for a in context['links']]
context['filters'] = filters
return templates.TemplateResponse("topic.html", context)