GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — webssh/scripts/prepare_pages.py at main · ega-d/webssh

Expand file tree

/

prepare_pages.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
287 lines (264 loc) · 13.5 KB
/

prepare_pages.py

Copy path

File metadata and controls

287 lines (264 loc) · 13.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
276
277
278
279
280
281
282
283
284
285
286
287
"""Build the static product site and its locally served code graph."""
import argparse
import base64
import hashlib
from html import escape
from html.parser import HTMLParser
import json
from pathlib import Path
import shutil
GRAPH_CDN = 'https://unpkg.com/[email protected]/standalone/umd/vis-network.min.js'
GRAPH_VENDOR = 'vendor/vis-network-9.1.6/vis-network.min.js'
SITE_URL = 'https://getwebssh.com/'
PUBLIC_PAGES = {
'index.html': 'Home',
'features.html': 'Features',
'demo.html': 'Demo',
'self-hosting.html': 'Self-hosting',
'security.html': 'Security',
'impressum.html': 'Legal notice',
'privacy.html': 'Privacy notice',
}
class SearchMetadata(HTMLParser):
"""Read the same title, description and canonical URL that visitors receive."""
def __init__(self, text):
super().__init__()
self.titles = []
self.descriptions = []
self.canonicals = []
self.policies = []
self._title = None
self.feed(text)
self.close()
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == 'title':
self._title = []
elif tag == 'meta' and attrs.get('name') == 'description':
self.descriptions.append(attrs.get('content', ''))
elif tag == 'meta' and attrs.get('http-equiv') == 'Content-Security-Policy':
self.policies.append(attrs.get('content', ''))
elif tag == 'link' and attrs.get('rel') == 'canonical':
self.canonicals.append(attrs.get('href', ''))
def handle_data(self, data):
if self._title is not None:
self._title.append(data)
def handle_endtag(self, tag):
if tag == 'title' and self._title is not None:
self.titles.append(''.join(self._title))
self._title = None
def add_search_metadata(output: Path) -> None:
"""Package consistent crawlable metadata without adding a browser dependency."""
if {path.name for path in output.glob('*.html')} != set(PUBLIC_PAGES):
raise ValueError('Authored pages and public page inventory differ')
urls = []
titles = set()
descriptions = set()
for filename, label in PUBLIC_PAGES.items():
path = output / filename
html = path.read_text(encoding='utf-8')
metadata = SearchMetadata(html)
url = SITE_URL + ('' if filename == 'index.html' else filename)
if (len(metadata.titles) != 1 or not metadata.titles[0]
or len(metadata.descriptions) != 1 or not metadata.descriptions[0]
or metadata.canonicals != [url] or len(metadata.policies) != 1
or html.count('</head>') != 1):
raise ValueError(f'Invalid search metadata in {filename}')
title, description = metadata.titles[0], metadata.descriptions[0]
if title in titles or description in descriptions:
raise ValueError(f'Duplicate search metadata in {filename}')
titles.add(title)
descriptions.add(description)
urls.append(url)
nodes = [{
'@type': 'WebPage', '@id': url + '#webpage', 'url': url,
'name': title, 'description': description, 'inLanguage': 'en',
'isPartOf': {'@id': SITE_URL + '#website'},
'about': {'@id': SITE_URL + '#software'},
}]
if filename == 'index.html':
nodes.extend([
{'@type': 'WebSite', '@id': SITE_URL + '#website', 'url': SITE_URL,
'name': 'WebSSH', 'inLanguage': 'en',
'publisher': {'@id': SITE_URL + '#maintainer'}},
{'@type': 'SoftwareSourceCode', '@id': SITE_URL + '#software',
'name': 'WebSSH', 'url': SITE_URL, 'description': description,
'codeRepository': 'https://github.com/bifrost0x/webssh',
'license': 'https://github.com/bifrost0x/webssh/blob/main/LICENSE',
'programmingLanguage': ['Python', 'JavaScript'],
'runtimePlatform': 'Python 3.11+',
'author': {'@id': SITE_URL + '#maintainer'},
'image': SITE_URL + 'assets/session-workspace.png'},
{'@type': 'Person', '@id': SITE_URL + '#maintainer',
'name': 'Marcel Graewer', 'url': SITE_URL + 'impressum.html'},
])
else:
nodes[0]['breadcrumb'] = {'@id': url + '#breadcrumb'}
nodes.append({
'@type': 'BreadcrumbList', '@id': url + '#breadcrumb',
'itemListElement': [
{'@type': 'ListItem', 'position': 1, 'name': 'Home', 'item': SITE_URL},
{'@type': 'ListItem', 'position': 2, 'name': label, 'item': url},
],
})
if filename == 'demo.html':
video_id = url + '#video'
nodes[0]['mainEntity'] = {'@id': video_id}
nodes.append({
'@type': 'VideoObject', '@id': video_id,
'name': 'WebSSH 3 visual tour',
'description': description,
'thumbnailUrl': [SITE_URL + 'assets/dashboard.png'],
'contentUrl': SITE_URL + 'assets/webssh-3-tour.webm',
'url': url,
'uploadDate': '2026-11-04T00:00:00+01:00',
'duration': 'PT20S', 'inLanguage': 'en',
'width': 1920, 'height': 1080,
'about': {'@id': SITE_URL + '#software'},
})
structured = json.dumps({'@context': 'https://schema.org', '@graph': nodes},
ensure_ascii=False, separators=(',', ':')).replace('<', '\\u003c')
digest = base64.b64encode(hashlib.sha256(structured.encode('utf-8')).digest()).decode('ascii')
policy = metadata.policies[0]
if policy.count("script-src 'self';") != 1:
raise ValueError(f'Unexpected authored page script policy in {filename}')
html = html.replace(policy, policy.replace("script-src 'self';", f"script-src 'self' 'sha256-{digest}';"), 1)
image = SITE_URL + 'assets/session-workspace.png'
image_alt = 'WebSSH browser workspace with an SSH terminal and SFTP files side by side'
if filename == 'demo.html':
image = SITE_URL + 'assets/dashboard.png'
image_alt = 'WebSSH product walkthrough with terminals, SFTP files and command sets'
tags = {
'og:type': 'website', 'og:site_name': 'WebSSH', 'og:locale': 'en_US',
'og:title': title, 'og:description': description, 'og:url': url,
'og:image': image, 'og:image:type': 'image/png',
'og:image:width': '2560',
'og:image:height': '1440', 'og:image:alt': image_alt,
}
social = ''.join(f' <meta property="{key}" content="{escape(value, quote=True)}">\n'
for key, value in tags.items())
twitter = {'twitter:card': 'summary_large_image', 'twitter:title': title,
'twitter:description': description, 'twitter:image': image,
'twitter:image:alt': image_alt}
social += ''.join(f' <meta name="{key}" content="{escape(value, quote=True)}">\n'
for key, value in twitter.items())
social += ' <meta name="robots" content="index, follow, max-image-preview:large">\n'
social += f' <script type="application/ld+json">{structured}</script>\n'
html = html.replace('</head>', social + '</head>', 1)
path.write_text(html, encoding='utf-8', newline='\n')
entries = ''.join(f' <url><loc>{escape(url)}</loc></url>\n' for url in urls)
(output / 'sitemap.xml').write_text(
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
+ entries + '</urlset>\n', encoding='utf-8', newline='\n')
(output / 'robots.txt').write_text(
'User-agent: *\nAllow: /\n\nSitemap: ' + SITE_URL + 'sitemap.xml\n',
encoding='utf-8', newline='\n')
class RuntimeAssets(HTMLParser):
def __init__(self):
super().__init__()
self.inline_scripts = []
self.vendor_tag = None
self._script_parts = None
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == 'script' and attrs.get('src') == '../' + GRAPH_VENDOR:
self.vendor_tag = (self.get_starttag_text(), attrs)
if any(name.startswith('on') for name in attrs):
raise ValueError('Unexpected inline event handler in Graphify output')
resource = attrs.get('src')
if tag == 'link' and attrs.get('rel') in {'stylesheet', 'preload', 'modulepreload'}:
resource = attrs.get('href')
if resource and resource.startswith(('https:', 'http:', '//')):
raise ValueError(f'External runtime asset remains: {resource}')
if tag == 'script' and 'src' not in attrs:
self._script_parts = []
def handle_data(self, data):
if self._script_parts is not None:
self._script_parts.append(data)
def handle_endtag(self, tag):
if tag == 'script' and self._script_parts is not None:
self.inline_scripts.append(''.join(self._script_parts))
self._script_parts = None
def close(self):
super().close()
if self._script_parts is not None:
raise ValueError('Unclosed inline script in Graphify output')
def prepare_pages(root: Path, graph_source: Path, output: Path) -> None:
graph_html = graph_source.read_text(encoding='utf-8')
if graph_html.count(GRAPH_CDN) != 1:
raise ValueError('Graphify vis-network URL was missing or appeared more than once')
if not (root / 'site' / GRAPH_VENDOR).is_file():
raise ValueError('Missing vendored vis-network dependency')
if graph_html.count('</head>') != 1 or graph_html.count('</body>') != 1:
raise ValueError('Unexpected Graphify document structure')
graph_html = graph_html.replace(GRAPH_CDN, '../' + GRAPH_VENDOR)
graph_html = graph_html.replace(' onchange="toggleAllCommunities(!this.checked)"', '')
assets = RuntimeAssets()
assets.feed(graph_html)
assets.close()
if assets.vendor_tag is None:
raise ValueError('Graphify vis-network script was missing')
runtime_bytes = (root / 'site' / GRAPH_VENDOR).read_bytes().replace(b'\r\n', b'\n')
runtime_digest = base64.b64encode(hashlib.sha384(runtime_bytes).digest()).decode('ascii')
original_tag, attributes = assets.vendor_tag
attributes['integrity'] = 'sha384-' + runtime_digest
replacement_tag = '<script ' + ' '.join(
key if value is None else f'{key}="{escape(value, quote=True)}"'
for key, value in attributes.items()
) + '>'
graph_html = graph_html.replace(original_tag, replacement_tag, 1)
script_hashes = []
for body in assets.inline_scripts:
digest = base64.b64encode(hashlib.sha256(body.encode('utf-8')).digest()).decode('ascii')
script_hashes.append(f"'sha256-{digest}'")
policy = (
"default-src 'none'; script-src 'self' " + ' '.join(script_hashes)
+ "; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; "
"connect-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'"
)
metadata = (
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
'<meta name="referrer" content="no-referrer">\n'
'<meta name="robots" content="noindex, follow">\n'
f'<meta http-equiv="Content-Security-Policy" content="{policy}">\n'
)
# The policy must precede all scripts, including the vendored graph runtime.
graph_html = graph_html.replace('<head>', '<head>\n' + metadata, 1)
graph_html = graph_html.replace('</head>', '<link rel="stylesheet" href="../graph.css">\n</head>', 1)
graph_html = graph_html.replace('</body>', '''<script src="../graph.js" defer></script>
<nav class="graph-site-nav" aria-label="Website and legal information">
<a href="../">WebSSH</a><a href="../impressum.html">Legal notice</a>
<a href="../privacy.html">Privacy notice</a>
</nav></body>''', 1)
# A fresh directory prevents files from an older build entering the artifact.
output.mkdir(parents=True, exist_ok=False)
shutil.copytree(root / 'site', output, dirs_exist_ok=True)
for name in ('LICENSE', 'THIRD-PARTY-NOTICES', 'NOTICE'):
shutil.copyfile(root / name, output / name)
add_search_metadata(output)
# SRI covers the packaged local runtime, including reviewed vendor patches.
runtime = output / GRAPH_VENDOR
runtime.write_bytes(runtime_bytes)
# assets/webssh-logo.svg has fixed capture-theme colors. The app logo takes
# its colors from theme variables, which a plain <img> does not have.
shutil.copytree(root / 'assets', output / 'assets', dirs_exist_ok=True)
resources = {
'static/images/theme-backgrounds/carbon-glass.webp': 'carbon-glass.webp',
'static/vendor/material-icons/material-icons.woff2': 'material-icons.woff2',
'static/vendor/material-icons/LICENSE': 'material-icons-LICENSE',
}
for source, target in resources.items():
shutil.copyfile(root / source, output / 'assets' / target)
(output / 'code-graph').mkdir()
(output / 'code-graph/index.html').write_text(graph_html, encoding='utf-8', newline='\n')
(output / '.nojekyll').touch()
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output', type=Path, default=Path('_site'))
args = parser.parse_args()
root = Path(__file__).resolve().parents[1]
prepare_pages(root, root / 'graphify-out/graph.html', args.output)
print(f'Pages artifact prepared at {args.output}')
if __name__ == '__main__':
main()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.