Skip to content

Commit f48197a

Browse files
authored
Merge pull request #118 from javaevolved/copilot/add-proof-link-to-slug-detail
Add "Proof" section to slug detail pages linking to proof source
2 parents b327a1f + f9a2dc3 commit f48197a

File tree

15 files changed

+78
-0
lines changed

15 files changed

+78
-0
lines changed

html-generators/generate.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,30 @@ String renderDocLinks(String tpl, JsonNode docs) {
404404
return String.join("\n", links);
405405
}
406406

407+
String slugToPascalCase(String slug) {
408+
return Arrays.stream(slug.split("-"))
409+
.filter(w -> !w.isEmpty())
410+
.map(w -> Character.toUpperCase(w.charAt(0)) + w.substring(1))
411+
.collect(Collectors.joining());
412+
}
413+
414+
String renderProofSection(Snippet s, Map<String, String> strings) {
415+
var pascal = slugToPascalCase(s.slug());
416+
var proofFile = Path.of("proof", s.category(), pascal + ".java");
417+
if (!Files.exists(proofFile)) return "";
418+
var proofUrl = "https://github.com/javaevolved/javaevolved.github.io/blob/main/proof/%s/%s.java"
419+
.formatted(s.category(), pascal);
420+
var label = strings.getOrDefault("sections.proof", "Proof");
421+
var linkText = strings.getOrDefault("sections.proofLink", "View proof source");
422+
return """
423+
<section class="docs-section">
424+
<div class="section-label">%s</div>
425+
<div class="docs-links">
426+
<a href="%s" target="_blank" rel="noopener" class="doc-link">%s ↗</a>
427+
</div>
428+
</section>""".formatted(label, proofUrl, linkText);
429+
}
430+
407431
String renderRelatedSection(String tpl, Snippet snippet, Map<String, Snippet> all, String locale, Map<String, String> strings) {
408432
return snippet.related().stream().filter(all::containsKey)
409433
.map(p -> renderRelatedCard(tpl, all.get(p), locale, strings))
@@ -469,6 +493,7 @@ String generateHtml(Templates tpl, Snippet s, Map<String, Snippet> all, Map<Stri
469493
Map.entry("navArrows", renderNavArrows(s, locale)),
470494
Map.entry("whyCards", renderWhyCards(tpl.whyCard(), s.whyModernWins())),
471495
Map.entry("docLinks", renderDocLinks(tpl.docLink(), s.node().withArray("docs"))),
496+
Map.entry("proofSection", renderProofSection(s, extraTokens)),
472497
Map.entry("relatedCards", renderRelatedSection(tpl.relatedCard(), s, all, locale, extraTokens)),
473498
Map.entry("socialShare", renderSocialShare(tpl.socialShare(), s.slug(), s.title(), extraTokens))));
474499
var localeName = LOCALES.getOrDefault(locale, locale);

html-generators/generate.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,32 @@ def render_related_section(tpl, data, all_snippets, locale, strings):
341341
return "\n".join(cards)
342342

343343

344+
def slug_to_pascal_case(slug):
345+
"""Convert a hyphen-delimited slug to PascalCase. E.g. 'type-inference-with-var' -> 'TypeInferenceWithVar'."""
346+
return "".join(w.capitalize() for w in slug.split("-") if w)
347+
348+
349+
def render_proof_section(data, strings):
350+
"""Render the proof section linking to the proof source file on GitHub, or empty string if no proof exists."""
351+
slug = data["slug"]
352+
category = data["category"]
353+
pascal = slug_to_pascal_case(slug)
354+
proof_file = os.path.join("proof", category, f"{pascal}.java")
355+
if not os.path.isfile(proof_file):
356+
return ""
357+
proof_url = f"https://github.com/javaevolved/javaevolved.github.io/blob/main/proof/{category}/{pascal}.java"
358+
label = strings.get("sections.proof", "Proof")
359+
link_text = strings.get("sections.proofLink", "View proof source")
360+
return (
361+
' <section class="docs-section">\n'
362+
f' <div class="section-label">{label}</div>\n'
363+
' <div class="docs-links">\n'
364+
f' <a href="{proof_url}" target="_blank" rel="noopener" class="doc-link">{link_text} ↗</a>\n'
365+
' </div>\n'
366+
' </section>'
367+
)
368+
369+
344370
def render_social_share(tpl, slug, title, strings):
345371
"""Render social share URLs."""
346372
encoded_url = url_encode(f"{BASE_URL}/{slug}.html")
@@ -520,6 +546,7 @@ def generate_html(templates, data, all_snippets, extra_tokens, locale):
520546
"navArrows": render_nav_arrows(data, locale),
521547
"whyCards": render_why_cards(templates["why_card"], data["whyModernWins"]),
522548
"docLinks": render_doc_links(templates["doc_link"], data.get("docs", [])),
549+
"proofSection": render_proof_section(data, extra_tokens),
523550
"relatedCards": render_related_section(
524551
templates["related_card"], data, all_snippets, locale, extra_tokens
525552
),

templates/slug-template.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ <h1>{{title}}</h1>
214214
</div>
215215
</section>
216216

217+
{{proofSection}}
218+
217219
<section class="related">
218220
<div class="section-label">{{sections.relatedPatterns}}</div>
219221
<div class="related-grid">

translations/strings/ar.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ sections:
2525
jdkSupport: دعم JDK
2626
howItWorks: كيف يعمل
2727
relatedDocs: توثيق ذو صلة
28+
proof: إثبات
29+
proofLink: عرض كود الإثبات
2830
relatedPatterns: أنماط ذات صلة
2931
filters:
3032
show: "عرض:"

translations/strings/de.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sections:
2727
jdkSupport: JDK-Unterstützung
2828
howItWorks: Wie es funktioniert
2929
relatedDocs: Zugehörige Dokumentation
30+
proof: Beweis
31+
proofLink: Beweisquelle ansehen
3032
relatedPatterns: Verwandte Muster
3133
filters:
3234
show: 'Anzeigen:'

translations/strings/en.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sections:
2727
jdkSupport: JDK Support
2828
howItWorks: How it works
2929
relatedDocs: Related Documentation
30+
proof: Proof
31+
proofLink: View proof source
3032
relatedPatterns: Related patterns
3133
filters:
3234
show: 'Show:'

translations/strings/es.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sections:
2727
jdkSupport: Soporte JDK
2828
howItWorks: Cómo funciona
2929
relatedDocs: Documentación Relacionada
30+
proof: Prueba
31+
proofLink: Ver código de prueba
3032
relatedPatterns: Patrones relacionados
3133
filters:
3234
show: 'Mostrar:'

translations/strings/fr.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sections:
2727
jdkSupport: Support JDK
2828
howItWorks: Comment ça fonctionne
2929
relatedDocs: Documentation Associée
30+
proof: Preuve
31+
proofLink: Voir la preuve source
3032
relatedPatterns: Patterns associés
3133
filters:
3234
show: 'Afficher :'

translations/strings/it.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sections:
2727
jdkSupport: Supporto JDK
2828
howItWorks: Come funziona
2929
relatedDocs: Documentazione Correlata
30+
proof: Prova
31+
proofLink: Vedi sorgente di prova
3032
relatedPatterns: Pattern correlati
3133
filters:
3234
show: 'Mostra:'

translations/strings/ja.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ sections:
2525
jdkSupport: JDKサポート
2626
howItWorks: 仕組み
2727
relatedDocs: 関連ドキュメント
28+
proof: 証明
29+
proofLink: 証明ソースを見る
2830
relatedPatterns: 関連パターン
2931
filters:
3032
show: '表示:'

0 commit comments

Comments
 (0)