Repository navigation
Expand file tree
/
Copy pathsync_data.py
More file actions
1353 lines (1177 loc) · 48.9 KB
/
Copy pathsync_data.py
File metadata and controls
1353 lines (1177 loc) · 48.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Auto-discover NUSV projects and generate the website's project pages.
Discovery rule (self-service, no hardcoding):
A repository in the NUSV organization is treated as a project when it has
the GitHub topic "nusv-project". Infrastructure repos (.github,
NUSV.github.io, nusv-lite-sync, ...) simply never carry that topic.
Per-project conventions (all optional):
site.json in the repo root:
{
"name": "Syna", # display name (default: repo name, minus a trailing "-NUSV")
"tagline": "one-liner", # default: repository description
"platforms": ["Android", "Windows"], # default: []
"docs": "/docs/syna.html", # site-relative (leading /) or absolute URL
"icon": "logo.png" # path in the repo, default: auto-discovered
}
Icon auto-discovery: any *.png in the repo root named like the repo,
"logo*" or "icon*"; the last one alphabetically wins (Syna_logo_2.png
beats Syna_logo.png).
Screenshots: images under screenshots/ | docs/screenshots/ |
assets/screenshots/ are rendered as a gallery on the project page.
Generated outputs:
projects/<slug>.html - one page per discovered project
index.html - the "Featured projects" grid (kept in sync via markers)
warehouse.html - the "Everything we ship" grid (kept in sync via markers)
Run: python3 tools/sync_data.py [--token TOKEN]
Scheduled by .github/workflows/sync.yml (every 6h + manual dispatch).
"""
import argparse
import datetime
import html
import json
import os
import posixpath
import re
import sys
import time
import urllib.request
import urllib.error
API = "https://api.github.com"
RAW = "https://raw.githubusercontent.com"
ORG = "NUSV"
PROJECT_TOPIC = "nusv-project"
REPOS_PER_PAGE = 100
# Output files the generator rewrites between markers.
INDEX_FEATURED = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "index.html"
)
WAREHOUSE = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "warehouse.html"
)
DOCS_INDEX = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "docs.html"
)
FEAT_START = "<!-- FEATURED-GRID:start -->"
FEAT_END = "<!-- FEATURED-GRID:end -->"
WH_START = "<!-- WAREHOUSE-GRID:start -->"
WH_END = "<!-- WAREHOUSE-GRID:end -->"
MIRRORED_START = "<!-- MIRRORED-DOCS:start -->"
MIRRORED_END = "<!-- MIRRORED-DOCS:end -->"
# Icon names to look for in the repo root (lower-cased, prefix match).
ICON_PREFIXES = ("logo", "icon")
NAV_TMPL = """ <header class="nav">
<div class="wrap nav-inner">
<a class="brand" href="{p}index.html">
<span class="brand-mark"><img src="{p}assets/img/nusv-logo.png" alt="NUSV" loading="lazy"></span>
NUSV <small>United Science Vaca</small>
</a>
<nav class="nav-links">
<a href="{p}index.html" data-i18n="nav.home">Home</a>
<a href="{p}warehouse.html" data-i18n="nav.warehouse">Warehouse</a>
<a href="{p}docs.html" data-i18n="nav.docs">Docs</a>
<a href="{p}about.html" data-i18n="nav.about">About</a>
</nav>
<span class="nav-cta">
<button class="lang-toggle" id="lang-toggle" data-lang="en" title="中文版即将推出">EN</button>
</span>
</div>
</header>"""
FOOTER_TMPL = """ <footer class="footer">
<div class="wrap footer-inner">
<span>© <span class="year"></span> <span data-i18n="footer.copyright">United Science Vaca (NUSV). Open source.</span></span>
<span><a href="https://github.com/NUSV" target="_blank" rel="noopener" data-i18n="footer.github">GitHub</a> · <a href="{p}warehouse.html" data-i18n="footer.warehouse">Warehouse</a> · <a href="{p}docs.html" data-i18n="footer.docs">Docs</a></span>
</div>
</footer>"""
def nav(prefix=""):
return NAV_TMPL.replace("{p}", prefix)
def footer(prefix=""):
return FOOTER_TMPL.replace("{p}", prefix)
class HttpError(Exception):
pass
def _urlopen(req, timeout=30, attempts=4):
"""urlopen with retries for flaky networks (RemoteDisconnected etc.)."""
last = None
for k in range(attempts):
try:
return urllib.request.urlopen(req, timeout=timeout)
except urllib.error.HTTPError:
raise
except Exception as e: # noqa: BLE001 - transient network errors
last = e
time.sleep(1.5 * (k + 1))
raise last
def http_json(url, token):
req = urllib.request.Request(url)
if token:
req.add_header("Authorization", "Bearer " + token)
req.add_header("Accept", "application/vnd.github+json")
req.add_header("User-Agent", "nusv-site-sync")
try:
with _urlopen(req) as r:
return json.loads(r.read().decode("utf-8"))
except urllib.error.HTTPError as e:
if e.code in (404,):
return None
raise HttpError("%s -> %s" % (url, e))
def http_text(url, token):
req = urllib.request.Request(url)
if token:
req.add_header("Authorization", "Bearer " + token)
req.add_header("User-Agent", "nusv-site-sync")
try:
with _urlopen(req) as r:
return r.read().decode("utf-8")
except urllib.error.HTTPError as e:
if e.code in (404, 403):
return None
raise HttpError("%s -> %s" % (url, e))
# --------------------------------------------------------------------------
# GitHub data
# --------------------------------------------------------------------------
def list_org_repos(token):
out, page = [], 1
while True:
batch = http_json(
"%s/orgs/%s/repos?per_page=%d&page=%d&type=public"
% (API, ORG, REPOS_PER_PAGE, page),
token,
)
if not batch:
break
out.extend(batch)
if len(batch) < REPOS_PER_PAGE:
break
page += 1
return out
def get_topics(repo_name, token):
data = http_json(
"%s/repos/%s/%s/topics" % (API, ORG, repo_name),
token,
)
if not data:
return []
# The list endpoint can be served without the topics accept header value;
# topics arrive under .names.
return data.get("names", [])
def get_site_json(repo_name, branch, token):
data = http_json(
"%s/repos/%s/%s/contents/site.json?ref=%s" % (API, ORG, repo_name, branch),
token,
)
if not data or not data.get("content"):
return {}
try:
return json.loads(__import__("base64").b64decode(data["content"]).decode("utf-8"))
except Exception:
return {}
def get_root_files(repo_name, branch, token):
data = http_json(
"%s/repos/%s/%s/contents?ref=%s" % (API, ORG, repo_name, branch), token
)
return [f["name"] for f in data] if data else []
_TREE_CACHE = {}
def get_tree(repo_name, branch, token):
key = (repo_name, branch)
if key not in _TREE_CACHE:
data = http_json(
"%s/repos/%s/%s/git/trees/%s?recursive=1" % (API, ORG, repo_name, branch),
token,
)
_TREE_CACHE[key] = data.get("tree", []) if data else []
return _TREE_CACHE[key]
def get_screenshots(repo_name, branch, token):
"""Find screenshots anywhere under a known folder name."""
out = []
for t in get_tree(repo_name, branch, token):
p = t.get("path", "")
if not p.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".gif")):
continue
parts = p.lower().split("/")
if any(x in ("screenshots", "screenshot") for x in parts):
out.append(p)
out.sort()
return out
def get_doc_paths(repo_name, branch, token):
"""Important documents to mirror: root *.md (except README) + docs/**/*.md."""
out = []
for t in get_tree(repo_name, branch, token):
if t.get("type") != "blob":
continue
p = t.get("path", "")
if not p.lower().endswith(".md"):
continue
if "/" not in p:
if p.lower() == "readme.md":
continue
out.append(p)
elif p.lower().startswith("docs/"):
out.append(p)
out.sort(key=lambda x: (0 if "/" not in x else 1, x.lower()))
return out
def doc_slug(path):
"""Slug for a repo document path.
Generic basenames borrow the parent directory, so
docs/tutorials/en.md -> "tutorials-en" and docs/tutorials/index.md ->
"tutorials"; normal names stand alone (docs/security/shield-design.md ->
"shield-design"). Version suffixes are trimmed."""
parts = path.split("/")
base = re.sub(r"\.md$", "", parts[-1], flags=re.I).lower()
dirs = [p for p in parts[:-1] if p.lower() != "docs"]
if base in ("index", "readme") and dirs:
source = dirs[-1]
elif base in ("en", "zh", "cn", "ja", "ko", "es", "fr", "de", "ru") and dirs:
source = "%s-%s" % (dirs[-1], base)
else:
source = base
slug = slugify(source)
return re.sub(r"-v\d+(-\d+)+$", "", slug) or slug
def slugify(text):
s = re.sub(r"[^a-z0-9]+", "-", (text or "").lower()).strip("-")
return s or "doc"
def prettify_name(path):
base = re.sub(r"\.md$", "", os.path.basename(path), flags=re.I)
return base.replace("_", " ").replace("-", " ").strip()
def extract_h1(md):
m = re.search(r"^#\s+(.+?)\s*$", md, re.M)
return m.group(1).strip() if m else None
IMG_RE = re.compile(
r'src="(https://raw\.githubusercontent\.com/NUSV/([^/"]+)/([^/"]+)/([^"]+))"'
)
_IMG_CACHE = {}
_IMG_TOTAL = [0]
IMG_MAX_FILE = 8 * 1024 * 1024
IMG_MAX_TOTAL = 120 * 1024 * 1024
def localize_images(text, token, root):
"""Download referenced raw.githubusercontent images into
assets/mirror/<repo>/<path> and rewrite src to site-root paths.
Keeps the remote URL when a download fails or exceeds the caps, so the
page still works (just without the mirror benefit for that image)."""
def repl(m):
url, repo, branch, path = m.group(1), m.group(2), m.group(3), m.group(4)
if url in _IMG_CACHE:
return 'src="%s"' % _IMG_CACHE[url]
rel = os.path.join("assets", "mirror", repo, path)
dest = os.path.join(root, rel)
local = "/" + rel.replace(os.sep, "/")
if os.path.exists(dest):
_IMG_CACHE[url] = local
return 'src="%s"' % local
if _IMG_TOTAL[0] > IMG_MAX_TOTAL:
return m.group(0)
try:
req = urllib.request.Request(url)
if token:
req.add_header("Authorization", "Bearer " + token)
req.add_header("User-Agent", "nusv-site-sync")
with _urlopen(req, timeout=60) as r:
data = r.read(IMG_MAX_FILE + 1)
if len(data) > IMG_MAX_FILE:
print(" img too large, kept remote: %s" % url)
return m.group(0)
except Exception as e: # noqa: BLE001
print(" img failed, kept remote: %s (%s)" % (url, e))
return m.group(0)
os.makedirs(os.path.dirname(dest), exist_ok=True)
with open(dest, "wb") as f:
f.write(data)
_IMG_TOTAL[0] += len(data)
_IMG_CACHE[url] = local
return 'src="%s"' % local
return IMG_RE.sub(repl, text)
def discover_icon(repo_name, branch, token, override=None):
if override:
return "%s/%s/%s/%s/%s" % (RAW, ORG, repo_name, branch, override.lstrip("/"))
files = get_root_files(repo_name, branch, token)
low = repo_name.lower()
base = re.sub(r"-nusv$", "", low) # "Syna-NUSV" -> "syna"
cands = []
for f in files:
fl = f.lower()
if not fl.endswith(".png"):
continue
if fl.startswith(ICON_PREFIXES) or fl.startswith(base) or fl.startswith(low):
cands.append(f)
if not cands:
return None
# last alphabetically: Syna_logo_2.png > Syna_logo.png
best = max(cands)
return "%s/%s/%s/%s/%s" % (RAW, ORG, repo_name, branch, best)
def fetch_releases(repo_name, token):
data = http_json(
"%s/repos/%s/%s/releases?per_page=8" % (API, ORG, repo_name), token
)
out = []
for rel in data or []:
out.append(
{
"tag": rel.get("tag_name", ""),
"name": rel.get("name") or rel.get("tag_name", ""),
"published": rel.get("published_at", ""),
"html_url": rel.get("html_url", ""),
"assets": [
{
"name": a.get("name", ""),
"size": a.get("size", 0),
"count": a.get("download_count", 0),
"url": a.get("browser_download_url", ""),
}
for a in rel.get("assets", [])
],
}
)
return out
def fetch_repo_text(repo_name, branch, path, token):
"""Fetch a text file from a repo, preferring the contents API (fresh,
not CDN-cached) and falling back to raw for files over the API's
1 MB inline limit."""
data = http_json(
"%s/repos/%s/%s/contents/%s?ref=%s" % (API, ORG, repo_name, path, branch),
token,
)
if data and data.get("content"):
try:
return __import__("base64").b64decode(data["content"]).decode("utf-8")
except Exception:
pass
url = "%s/%s/%s/%s/%s" % (RAW, ORG, repo_name, branch, path)
req = urllib.request.Request(url)
if token:
req.add_header("Authorization", "Bearer " + token)
req.add_header("User-Agent", "nusv-site-sync")
try:
with _urlopen(req) as r:
return r.read().decode("utf-8")
except urllib.error.HTTPError as e:
if e.code in (404, 403):
return None
raise
def fetch_readme(repo_name, branch, token):
return fetch_repo_text(repo_name, branch, "README.md", token)
def get_mirror_manifest(token):
"""Manifest produced by NUSV/downloads (latest-release mirror).
The manifest is published on the `mirror` branch (the workflow keeps the
pipeline on `main` and force-pushes snapshots to `mirror`)."""
data = http_json(
"%s/repos/%s/downloads/contents/downloads.json?ref=mirror" % (API, ORG), token
)
if not data or not data.get("content"):
return {}
try:
return json.loads(__import__("base64").b64decode(data["content"]).decode("utf-8"))
except Exception:
return {}
# --------------------------------------------------------------------------
# Markdown subset renderer
# --------------------------------------------------------------------------
def resolve_ref(repo_name, branch, raw):
raw = raw.strip()
if raw.startswith(("http://", "https://", "#", "mailto:", "data:")):
return raw
raw = raw.lstrip("./")
return "https://github.com/%s/%s/%s/%s" % (ORG, repo_name, branch, raw)
def resolve_raw(repo_name, branch, raw):
raw = raw.strip()
if raw.startswith(("http://", "https://", "data:")):
return raw
raw = raw.lstrip("./")
return "%s/%s/%s/%s/%s" % (RAW, ORG, repo_name, branch, raw)
def md_inline(text, repo_name, branch, link_map=None, base_dir=""):
img_tokens = {}
def repo_path(ref):
ref = ref.strip().lstrip("./")
if base_dir:
return posixpath.normpath(posixpath.join(base_dir, ref))
return ref
def img_placeholder(m):
alt, src = m.group(1), m.group(2)
key = "IMGTOK%d" % len(img_tokens)
img_tokens[key] = (
'<img src="%s" alt="%s" loading="lazy" referrerpolicy="no-referrer">'
% (html.escape(resolve_raw(repo_name, branch, repo_path(src)), quote=True),
html.escape(alt or "", quote=True))
)
return key
def raw_img(m):
tag = m.group(0)
attrs, src = [], None
for am in re.finditer(
r"""\b(src|width|height|alt)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))""",
tag,
):
k = am.group(1)
val = am.group(2) if am.group(2) is not None else (
am.group(3) if am.group(3) is not None else am.group(4)
)
if k == "src":
src = html.escape(resolve_raw(repo_name, branch, repo_path(val)), quote=True)
else:
attrs.append('%s="%s"' % (k, html.escape(val, quote=True)))
if src is None:
return ""
return '<img src="%s" loading="lazy" referrerpolicy="no-referrer" %s>' % (
src,
" ".join(attrs),
)
text = re.sub(r"<img\b[^>]*/?>", raw_img, text)
text = re.sub(
r"!\[([^\]]*)\]\(([^)\s]+)(?:\s+\"[^\"]*\")?\)", img_placeholder, text
)
text = re.sub(r"`([^`]+)`", lambda m: "<code>%s</code>" % html.escape(m.group(1)), text)
text = re.sub(r"\*\*([^*]+)\*\*", lambda m: "<strong>%s</strong>" % m.group(1), text)
text = re.sub(r"__([^_]+)__", lambda m: "<strong>%s</strong>" % m.group(1), text)
text = re.sub(r"(?<!\*)\*([^*\n]+)\*(?!\*)", lambda m: "<em>%s</em>" % m.group(1), text)
def link(m):
lab, dst = m.group(1), m.group(2)
if not dst.startswith(("http", "#", "mailto:")):
key = repo_path(dst)
if link_map and key in link_map:
return '<a href="%s">%s</a>' % (
html.escape(link_map[key], quote=True),
lab,
)
return '<a href="%s">%s</a>' % (
html.escape(resolve_ref(repo_name, branch, key), quote=True),
lab,
)
return '<a href="%s" target="_blank" rel="noopener">%s</a>' % (
html.escape(dst, quote=True),
lab,
)
text = re.sub(r"\[([^\]]+)\]\(([^)\s]+)(?:\s+\"[^\"]*\")?\)", link, text)
for key, val in img_tokens.items():
text = text.replace(key, val)
return text
def _norm(s):
return re.sub(r"[^a-z0-9]", "", (s or "").lower())
def md_to_html(md, repo_name, branch, drop_h1_names=None, link_map=None, base_dir=""):
"""Render a pragmatic README subset to HTML.
If drop_h1_names is given (normalized repo/display names), a leading
"# <name>" heading is dropped because the page already shows that title.
Any other README heading is preserved."""
out = []
lines = md.replace("\r\n", "\n").split("\n")
i, n = 0, len(lines)
first_h1_dropped = False
drop_set = (
{_norm(x) for x in drop_h1_names} if drop_h1_names is not None else None
)
def inline(t):
return md_inline(t, repo_name, branch, link_map, base_dir)
while i < n:
line = lines[i]
# Optionally drop the very first "# <repo title>" heading and the
# badge row that follows it (both duplicate the generated page head).
m0 = re.match(r"^#\s+(.+?)\s*$", line)
if m0 and drop_set is not None and _norm(m0.group(1)) in drop_set:
first_h1_dropped = True
i += 1
while i < n and lines[i].strip() == "":
i += 1
# Skip consecutive centered <p> blocks that only contain links or
# images (badge rows / banners), plus surrounding blank lines.
while i < n:
if lines[i].strip() == "":
i += 1
continue
if lines[i].strip().startswith('<p align="center">'):
i += 1
while i < n and not lines[i].strip() == "</p>":
i += 1
i += 1
continue
break
continue
if line.startswith("```"):
fence = []
i += 1
while i < n and not lines[i].startswith("```"):
fence.append(lines[i])
i += 1
i += 1
out.append("<pre class=\"doc-code\">%s</pre>" % html.escape("\n".join(fence)))
continue
m = re.match(r"^(#{1,6})\s+(.*)$", line)
if m:
lvl = len(m.group(1))
out.append("<h%d>%s</h%d>" % (lvl, inline(m.group(2)), lvl))
i += 1
continue
if re.match(r"^\s*(---+|\*\*\*+)\s*$", line):
out.append("<hr>")
i += 1
continue
if line.strip().startswith(("> ", ">")):
quote = []
while i < n and lines[i].strip().startswith(">"):
quote.append(re.sub(r"^\s*>\s?", "", lines[i], count=1))
i += 1
inner = md_to_html(
"\n".join(quote), repo_name, branch,
drop_h1_names=list(drop_set) if drop_set is not None else None,
link_map=link_map,
base_dir=base_dir,
)[0]
out.append("<blockquote>%s</blockquote>" % inner)
continue
if re.match(r"^\s*[-*+]\s+", line) or re.match(r"^\s*\d+[.)]\s+", line):
items = []
ordered = bool(re.match(r"^\s*\d+[.)]\s+", line))
while i < n:
m2 = re.match(r"^\s*[-*+]\s+(.*)$", lines[i])
m3 = re.match(r"^\s*\d+[.)]\s+(.*)$", lines[i])
if ordered and m3:
items.append(m3.group(1))
elif not ordered and m2:
items.append(m2.group(1))
elif lines[i].strip() == "":
i += 1
continue
else:
break
i += 1
tag = "ol" if ordered else "ul"
out.append(
"<%s>%s</%s>"
% (
tag,
"".join("<li>%s</li>" % inline(it) for it in items),
tag,
)
)
continue
if line.startswith("|"):
rows = []
while i < n and lines[i].startswith("|"):
rows.append(lines[i])
i += 1
if len(rows) >= 2:
head = [c.strip() for c in rows[0].strip("|").split("|")]
body = rows[2:]
tbl = ["<table><thead><tr>"]
tbl.extend("<th>%s</th>" % inline(c) for c in head)
tbl.append("</tr></thead><tbody>")
for r in body:
cells = [c.strip() for c in r.strip("|").split("|")]
tbl.append("<tr>")
tbl.extend("<td>%s</td>" % inline(c) for c in cells)
tbl.append("</tr>")
tbl.append("</tbody></table>")
out.append("".join(tbl))
continue
out.append(inline(line))
i += 1
continue
if line.strip() == "":
i += 1
continue
para = []
while i < n and lines[i].strip() != "" and not lines[i].startswith(("#", "```", "|", "> ")):
if re.match(r"^\s*[-*+]\s+", lines[i]) or re.match(r"^\s*\d+[.)]\s+", lines[i]):
break
para.append(lines[i])
i += 1
joined = " ".join(x.strip() for x in para)
wrap = re.fullmatch(r"<p\b[^>]*>(.*)</p>", joined, re.S)
if wrap:
out.append('<div class="readme-img-block">%s</div>' % inline(wrap.group(1)))
else:
out.append("<p>%s</p>" % inline(joined))
return "\n".join(out), first_h1_dropped
# --------------------------------------------------------------------------
# Rendering
# --------------------------------------------------------------------------
def human_size(num):
num = num or 0
for unit in ("B", "KB", "MB", "GB"):
if num < 1024 or unit == "GB":
return ("%.0f %s" % (num, unit)) if unit == "B" else ("%.1f %s" % (num, unit))
num /= 1024.0
return str(num)
def fmt_iso(iso):
try:
dt = datetime.datetime.fromisoformat(iso.replace("Z", "+00:00")).astimezone(
datetime.timezone.utc
)
return dt.strftime("%Y-%m-%d")
except Exception:
return (iso or "")[:10]
PLATFORM_BY_EXT = {
".apk": ("Android APK", ""),
".dmg": ("macOS (.dmg)", ""),
".msi": ("Windows (.msi)", ""),
".exe": ("Windows (.exe)", ""),
".deb": ("Linux (.deb)", ""),
".jar": ("Server / other (.jar)", "muted"),
".ipa": ("iOS (.ipa)", "muted"),
}
def card_markup(proj, kind="featured"):
"""Markup for listing cards (home "featured" or warehouse)."""
icon = proj["icon_url"] or proj["fallback_icon"]
icon_html = (
'<span class="card-icon"><img src="%s" alt="%s icon" loading="lazy"></span>\n '
% (html.escape(icon, quote=True), html.escape(proj["name"]))
)
chips = "".join(
'<span class="chip chip-accent">%s</span>' % html.escape(p) for p in proj["platforms"]
)
chips += '<span class="chip">%s</span>' % html.escape(proj["license"] or "Open source")
links = []
if kind == "featured":
links.append('<a class="linkbtn" href="projects/%s.html" data-i18n="card.project_page">Project page</a>'
% proj["slug"])
if proj["docs"]:
links.append('<a class="linkbtn linkbtn-ghost" href="%s" data-i18n="card.tech_docs">Technical docs</a>'
% html.escape(proj["docs"], quote=True))
else:
links.append('<a class="linkbtn linkbtn-ghost" href="%s" target="_blank" rel="noopener" data-i18n="card.repository">Repository</a>'
% html.escape(proj["repo_url"], quote=True))
else:
links.append('<a class="linkbtn" href="projects/%s.html" data-i18n="card.project_page">Project page</a>' % proj["slug"])
if proj["docs"]:
links.append('<a class="linkbtn linkbtn-ghost" href="%s" data-i18n="card.tech_docs">Technical docs</a>'
% html.escape(proj["docs"], quote=True))
if proj["releases"]:
links.append('<a class="linkbtn linkbtn-ghost" href="%s/releases" target="_blank" rel="noopener" data-i18n="card.releases">Releases</a>'
% proj["repo_url"])
else:
links.append('<a class="linkbtn linkbtn-ghost" href="%s" target="_blank" rel="noopener" data-i18n="card.source">Source</a>'
% proj["repo_url"])
return (
'<div class="card reveal">\n'
" %s\n"
' <h3>%s</h3>\n'
' <p class="tagline">%s</p>\n'
' <div class="meta">%s</div>\n'
' <div class="card-links">\n'
' %s\n'
" </div>\n"
" </div>"
% (icon_html, html.escape(proj["name"]), html.escape(proj["tagline"]),
chips, "\n ".join(links))
)
def featured_section(projects):
return "\n".join(card_markup(p, "featured") for p in projects)
def warehouse_section(projects):
return "\n".join(card_markup(p, "warehouse") for p in projects)
def mirrored_docs_section(projects):
"""Cards for the docs.html hub: mirrored documents per project, grouped."""
cards = []
for p in projects:
docs = p.get("doc_pages") or []
if not docs:
continue
icon = p["icon_url"] or p["fallback_icon"]
cards.append(
'<div class="card reveal">\n'
' <span class="card-icon"><img src="%s" alt="%s icon" loading="lazy"></span>\n'
" <h3>%s</h3>\n"
" %s\n"
" </div>"
% (html.escape(icon, quote=True), html.escape(p["name"]),
html.escape(p["name"]), grouped_doc_list(docs))
)
return "\n".join(cards)
DOC_CATEGORY_ORDER = ["Tutorials", "FAQ", "Security", "Audits", "Release notes", "Documents"]
DOC_CATEGORY_KEYS = {
"Tutorials": "cat.tutorials",
"FAQ": "cat.faq",
"Security": "cat.security",
"Audits": "cat.audits",
"Release notes": "cat.release_notes",
"Documents": "cat.documents",
}
def doc_category(path):
low = path.lower()
base = os.path.basename(low)
if base.startswith("tutorial") or "/tutorials/" in low or "/tutorial/" in low:
return "Tutorials"
if base.startswith("faq") or "/faq/" in low:
return "FAQ"
if "audit" in base:
return "Audits"
if "shield" in base or base.startswith("security") or "/security/" in low:
return "Security"
if "changelog" in base or "release" in base:
return "Release notes"
return "Documents"
def grouped_doc_list(doc_pages):
groups = {}
for d in doc_pages:
groups.setdefault(doc_category(d["path"]), []).append(d)
parts = []
for name in DOC_CATEGORY_ORDER:
if name not in groups:
continue
items = "".join(
'<li><a href="%s">%s</a></li>'
% (html.escape(d["local"], quote=True), html.escape(d["title"]))
for d in groups[name]
)
parts.append(
'<div class="doc-group-title" data-i18n="%s">%s</div><ul class="doc-list">%s</ul>'
% (DOC_CATEGORY_KEYS.get(name, "cat.documents"), html.escape(name), items)
)
return "".join(parts)
def replace_markers(path, start_marker, end_marker, content):
with open(path, "r", encoding="utf-8") as f:
src = f.read()
if start_marker not in src or end_marker not in src:
raise RuntimeError("markers missing in %s" % path)
before = src.split(start_marker, 1)[0]
after = src.split(end_marker, 1)[1]
return before + start_marker + "\n" + content + "\n" + end_marker + after
def render_page(proj, readme_html, updated_utc):
name, tagline = proj["name"], proj["tagline"]
repo_url = proj["repo_url"]
docs = proj["docs"]
platforms, license_txt, stars, topics = (
proj["platforms"], proj["license"], proj["stars"], proj["topics"]
)
icon = proj["icon_url"] or proj["fallback_icon"]
chips = ["".join('<span class="chip chip-accent">%s</span>' % html.escape(p) for p in platforms)]
if license_txt:
chips.append('<span class="chip">%s</span>' % html.escape(license_txt))
for t in topics[:5]:
chips.append('<span class="chip">%s</span>' % html.escape(t))
latest = proj["releases"][0] if proj["releases"] else None
mirrored = proj.get("mirror") or {}
def dl_row(asset, big=False):
label, kind = PLATFORM_BY_EXT.get(
os.path.splitext(asset["name"].lower())[1], ("Download", "muted")
)
dl = (
'<span data-i18n="pg.downloads" data-i18n-args="%d">%d downloads</span>'
% (asset["count"], asset["count"])
) if asset.get("count") else human_size(asset["size"])
href = mirrored.get(asset["name"]) or asset["url"]
return (
'<a class="dl-btn%s %s" href="%s" target="_blank" rel="noopener">'
'<span class="dl-label">%s</span>'
'<span class="dl-sub">%s · %s</span></a>'
% (" big" if big else "", kind, html.escape(href, quote=True),
html.escape(label), html.escape(asset["name"]), dl)
)
if latest:
top = latest["assets"][:3] if latest["assets"] else []
alln = len(latest["assets"])
dl_latest = "".join(dl_row(a, big=True) for a in top)
if alln == 0:
dl_latest = (
'<a class="dl-btn muted" href="%s" target="_blank" rel="noopener">'
'<span class="dl-label" data-i18n="pg.no_assets">No downloadable assets</span>'
'<span class="dl-sub" data-i18n="pg.open_release">open the release page</span></a>'
% html.escape(latest["html_url"], quote=True)
)
elif alln > len(top):
dl_latest += (
'<a class="dl-btn muted" href="%s" target="_blank" rel="noopener">'
'<span class="dl-label" data-i18n="pg.all_assets" data-i18n-args="%d">All %d assets</span>'
'<span class="dl-sub" data-i18n="pg.open_release">open the release page</span></a>'
% (html.escape(latest["html_url"], quote=True), alln, alln)
)
head_sub = (
'<span class="rel-tag">%s</span><span class="rel-date">%s</span>'
% (html.escape(latest["tag"]), fmt_iso(latest["published"]))
)
else:
dl_latest = ""
head_sub = '<span class="rel-tag" data-i18n="pg.no_releases">no releases yet</span>'
rel_items = []
for rel in proj["releases"][1:]:
rel_items.append(
'<a class="rel-item" href="%s" target="_blank" rel="noopener">'
'<span class="rel-tag">%s</span>'
'<span class="rel-date">%s</span>'
'<span class="rel-assets">%d assets</span></a>'
% (html.escape(rel["html_url"], quote=True), html.escape(rel["tag"]),
fmt_iso(rel["published"]), len(rel["assets"]))
)
sidebar = []
mirror_note = ""
if mirrored:
mirror_note = (
'<div class="mirror-note" data-i18n="pg.mirror_note">'
'<a href="/downloads/">Mirrored on nusv.github.io</a> '
"— no VPN required.</div>"
)
sidebar.append(
'<div class="card pg-side">'
'<div class="side-title" data-i18n="pg.latest_release">Latest release</div>'
'%s<div class="dl-group">%s%s</div></div>'
% (head_sub, dl_latest, mirror_note)
)
if rel_items:
sidebar.append(
'<div class="card pg-side"><div class="side-title" data-i18n="pg.recent_versions">Recent versions</div>%s</div>'
% "".join(rel_items)
)
info = []
info.append('<div class="pg-info"><span data-i18n="pg.license">License</span><b>%s</b></div>' % html.escape(license_txt or "—"))
info.append('<div class="pg-info"><span data-i18n="pg.stars">Stars</span><b>%d</b></div>' % stars)
info.append('<div class="pg-info"><span data-i18n="pg.updated">Repository updated</span><b>%s</b></div>'
% html.escape((proj.get("pushed_at") or "")[:10]))
info.append('<div class="pg-info"><span data-i18n="pg.source">Source</span><a href="%s" target="_blank" rel="noopener">%s</a></div>'
% (html.escape(repo_url, quote=True), html.escape(proj["repo"])))
sidebar.append('<div class="card pg-side"><div class="side-title" data-i18n="pg.project_info">Project info</div>%s</div>'
% "".join(info))
doc_pages = proj.get("doc_pages") or []
if doc_pages:
sidebar.append(
'<div class="card pg-side"><div class="side-title" data-i18n="pg.documents">Documents</div>%s</div>'
% grouped_doc_list(doc_pages)
)
# Screenshot gallery (optional, convention-based).
gal = ""
if proj["screenshots"]:
imgs = "".join(
'<a class="gal-item" href="%s" target="_blank" rel="noopener">'
'<img src="%s" alt="Screenshot" loading="lazy" referrerpolicy="no-referrer"></a>'
% (html.escape(resolve_raw(proj["repo"], proj["branch"], p), quote=True),
html.escape(resolve_raw(proj["repo"], proj["branch"], p), quote=True))
for p in proj["screenshots"][:8]
)
gal = '<h2 class="gal-title" data-i18n="pg.screenshots">Screenshots</h2><div class="gal">%s</div>' % imgs
actions = ['<a class="btn btn-primary" href="#downloads" data-i18n="pg.download_latest">Download latest</a>']
actions.append('<a class="btn btn-ghost" href="%s" target="_blank" rel="noopener" data-i18n="pg.github_repo">GitHub repository</a>' % html.escape(repo_url, quote=True))
if docs:
actions.append('<a class="btn btn-ghost" href="%s" data-i18n="pg.tech_docs">Technical docs</a>' % html.escape(docs, quote=True))
page = """<!DOCTYPE html>
<html lang="en" data-i18n-title="title.project" data-i18n-args="{name}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>%s — Downloads & Introduction | United Science Vaca (NUSV)</title>
<meta name="description" content="%s">
<meta property="og:title" content="%s — NUSV">
<meta property="og:description" content="%s">
<meta property="og:image" content="%s">
<meta property="og:type" content="website">
<link rel="icon" type="image/png" href="../assets/img/nusv-logo.png">
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
%s
<main class="wrap">
<nav class="pg-back"><a href="../warehouse.html" data-i18n="pg.back">‹ All projects</a></nav>
<article class="pg">
<div class="pg-head">
<div class="pg-head-main">
<span class="pg-icon"><img src="%s" alt="%s icon" loading="lazy"></span>
<h1>%s</h1>
<p class="lede">%s</p>
<div class="meta pg-chips">%s</div>
<div class="btn-row pg-actions">