-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
232 lines (177 loc) · 7.29 KB
/
Copy pathapp.py
File metadata and controls
232 lines (177 loc) · 7.29 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
import logging
import flask
from os import path
from bs4 import BeautifulSoup
from flask import render_template
from markupsafe import escape, Markup
from werkzeug.middleware.proxy_fix import ProxyFix
from githubapp.app import get_all_repos, _get_quarto_metadata, get_repo, get_paper_version_list, repo_dir_name, \
get_next_prev_slug, get_commits
from processing.artefacts import host_artefacts
from processing.paper import list_papers, paper_content, gh_paper_content
app = flask.Flask(__name__)
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
# Tie in gunicorn logger
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_logger.handlers)
app.logger.setLevel(logging.DEBUG)
info = app.logger.info
@app.route('/')
def index():
return render_template('index.html', title='Live Publications', text='This is the home page')
@app.route('/papers')
def papers():
papers = list_papers()
list_view = [p['frontmatter'] for p in papers.values()]
for p in list_view:
p['slug'] = f'/papers/{p["slug"]}'
return render_template('papers.html',
title='Live Publications',
papers=list_view)
@app.route('/papers/<paper_slug>')
def paper(paper_slug):
papers = list_papers()
# Check paper exists
safe_slug = escape(paper_slug)
if safe_slug not in papers:
flask.abort(404)
# Host artefacts
host_artefacts(f'papers/{safe_slug}')
# Render paper
content = paper_content(f'papers/{safe_slug}')
return render_template('paper.html',
title='Live Publications',
metadata=papers[safe_slug]['frontmatter'],
content=Markup(content))
@app.route("/gh_papers")
def gh_papers():
"""List available papers, for each paper navigate to list of versions"""
repos = get_all_repos()
def slug(repo):
return f'/gh_papers/{repo.owner.name}/{repo.name}'
# Build list of metadata similar to the papers() function
list_view = []
for r in repos:
metadata = _get_quarto_metadata(r)
list_view.append({
'title': metadata['title'],
'authors': [a['name'] for a in metadata['authors']],
'year': metadata.get('year', 'unknown'),
'slug': slug(r)
})
return render_template('papers.html',
title='Live Publications (github)',
papers=list_view)
@app.route('/gh_papers/<owner>/<repo_name>')
def gh_paper_versions(owner, repo_name):
"""List available versions of a paper"""
repo = get_repo(owner, repo_name)
# Get versions
versions = get_paper_version_list(repo)
# TODO: convert tags to colored badges
for v in versions:
v['tags'] = ', '.join(v['tags'])
v['slug'] = f'{owner}/{repo_name}/{v["sha"]}'
return render_template('paper_versions.html',
title='Live Publications (github)',
versions=versions)
@app.route('/gh_papers/<owner>/<repo_name>/<sha>/')
def gh_paper(owner, repo_name, sha):
"""Render a paper version"""
_owner = escape(owner)
_repo_name = escape(repo_name)
_sha = escape(sha)
info(f'gh_paper - Paper request: {_owner}/{_repo_name}/{_sha}')
try:
html_src = gh_paper_content(_owner, _repo_name, _sha)
info(f'gh_paper - HTML source: {html_src}')
with open(html_src, 'r', encoding='utf-8') as f:
html = f.read()
# Extract html so that we can insert it correctly into templates
soup = BeautifulSoup(html, 'html.parser')
# for t in soup.findAll('section', 'level2'):
# new_tag = soup.new_tag('iframe')
# new_tag['src'] = f'/gh_papers/{_owner}/{_repo_name}/{_sha}/sections/{t["id"]}'
# t.replace_with(new_tag)
head = ''.join(str(c) for c in soup.head.contents)
header = ''.join(str(c) for c in soup.header.contents)
body = soup.find(id='quarto-content')
# Link to next/previous version if available
next_slug, prev_slug = get_next_prev_slug(_owner, _repo_name, _sha)
info(f'gh_paper - Returning HTML')
return render_template('quarto_paper.html',
title='Live Publications',
head=Markup(head),
header=Markup(header),
content=Markup(body),
next_slug=next_slug,
prev_slug=prev_slug)
except FileNotFoundError as e:
logging.error(e)
flask.abort(404)
@app.route('/gh_papers/<owner>/<repo_name>/<sha>/sections/<section>')
def gh_paper_section(owner, repo_name, sha, section):
_owner = escape(owner)
_repo_name = escape(repo_name)
_sha = escape(sha)
_section = escape(section)
info(f'gh_paper - Paper request: {_owner}/{_repo_name}/{_sha}')
try:
html_src = gh_paper_content(_owner, _repo_name, _sha)
info(f'gh_paper - HTML source: {html_src}')
with open(html_src, 'r', encoding='utf-8') as f:
html = f.read()
# Extract html so that we can insert it correctly into templates
soup = BeautifulSoup(html, 'html.parser')
head = ''.join(str(c) for c in soup.head.contents)
header = ''.join(str(c) for c in soup.header.contents)
body = soup.find(id='quarto-content')
sections = soup.findAll('section', 'level2')
section_dict = {
s['id']: s
for s in sections
}
if _section not in section_dict:
flask.abort(404)
# Links to next/prev version
next_slug, prev_slug = get_next_prev_slug(_owner, _repo_name, _sha)
if next_slug:
next_slug += f'/sections/{_section}'
if prev_slug:
prev_slug += f'/sections/{_section}'
# return section_dict[_section].prettify()
return render_template('section.html',
content=Markup(section_dict[_section]),
next_slug=next_slug,
prev_slug=prev_slug)
except FileNotFoundError as e:
logging.error(e)
flask.abort(404)
@app.route('/gh_papers/<owner>/<repo_name>/<sha>/<path:filespec>')
def paper_files(owner, repo_name, sha, filespec):
"""Serve files (libraries and artefacts) from a paper"""
_owner = escape(owner)
_repo_name = escape(repo_name)
_sha = escape(sha)
_filespec = escape(filespec)
try:
repo_dir = repo_dir_name(_owner, _repo_name, _sha)
return flask.send_from_directory(repo_dir, _filespec)
except FileNotFoundError:
flask.abort(404)
@app.route('/editor/')
def editor():
# TODO: hard coded repo for now
_owner = 'LivePublication'
_repo_name = 'LP_Pub_LID'
# Get the most recent commit for the manuscript
# TODO: this logic doesn't make sense - you'd want to be able to update the manuscript seperately
repo = get_repo(_owner, _repo_name)
versions = get_commits(repo)
latest = versions[-1]
# TODO: Get list of possible flows to execute
return flask.render_template('editor.html', title='Editor', text='test')
if __name__ == '__main__':
app.run(debug=False)