ludic/tools/docgen/assets/ludic-highlight.tmpl.js
Orkuncakilkaya c93eca80af
All checks were successful
docs / build-and-deploy (push) Successful in 2s
docs(site): mobile-first revamp of all pages
Rewrite both stylesheets mobile-first (base = phone; @media min-width
progressively enhances) and add a responsive nav.

- Hamburger menu on small screens: links collapse into a clean, uniform
  dropdown list (plain rows + separators, no out-of-place boxed buttons);
  full inline nav with boxed CTA returns at >=720px. Auto-wired for every page.
- Fix horizontal-overflow root causes: min-width:0 on grid/flex code containers,
  pre/sig scroll internally, long names wrap (overflow-wrap). 0 page overflow on
  every page type at 375px.
- Stack layouts on mobile: single-column hero/features/steps/index grid,
  column parameter cards, 2-col color swatches; multi-column returns on wider
  screens. Reduced hero/section padding; fluid clamp() headings.
- Hover cards gated to hover-capable devices so a tap just navigates.
- Desktop layout preserved (verified at 1280px: inline nav, 2-col hero, 3-col
  features, no regression).

Verified in-browser at 375px and 1280px across landing, API index, item
(method/keyword/annotation/phase), namespace overview, and color pages.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-29 18:22:55 +03:00

206 lines
11 KiB
JavaScript

/* ============================================================================
* ludic-highlight.js — the Ludic syntax highlighter + docs interactions.
*
* GENERATED FILE — do not edit by hand. The symbol tables below (keywords,
* types, phases, namespace methods + their parameter labels, builtins,
* annotations, namespaces, one-line tips, per-item page targets and hover-card
* data) are produced by tools/docgen/gen.py from the per-symbol source files in
* docs/language/**. Add a symbol there and it is recognized, colored, tipped,
* linked and card-previewed here automatically.
*
* In any code sample: every keyword/type/builtin/annotation links to its own
* page; `Screen.fill_rectangle` links `Screen` → the namespace page and
* `fill_rectangle` → the method page, separately; a named argument like
* `width:` links to that parameter on the method's page; `Color.Charcoal`
* links `Color` and `Charcoal` separately. Hovering any of them shows a summary
* card pulled from the real API data.
* ========================================================================== */
(function (global) {
"use strict";
const S = /*__SYMBOLS__*/{};
const KEYWORDS = S.keywords || {}, TYPES = S.types || {}, PHASES = S.phases || {};
const BUILTINS = S.builtins || {}, NSMETHODS = S.nsmethods || {}, ANNOTS = S.annotations || {};
const NAMESPACES = S.namespaces || {}, TIPS = S.tips || {}, CARDS = S.cards || {};
function esc(s){ return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); }
const isIdStart = c => /[A-Za-z_]/.test(c);
const isId = c => /[A-Za-z0-9_]/.test(c);
function link(href, id, tip, inner){
const t = tip ? ' title="'+esc(tip).replace(/"/g,"&quot;")+'"' : "";
const d = id ? ' data-id="'+id+'"' : "";
return '<a class="tok" href="'+href+'"'+d+t+'>'+inner+'</a>';
}
function highlight(src){
let out = "", i = 0; const n = src.length;
const stack = []; // call-context stack for named-parameter linking
let pending = null; // ctx to push at the next "("
const nextNonSpace = j => { while (j < n && src[j] === " ") j++; return j; };
while (i < n){
const c = src[i];
if (c === "#"){ let j=i; while(j<n && src[j]!=="\n") j++; out+='<span class="t-com">'+esc(src.slice(i,j))+'</span>'; i=j; continue; }
if (c === '"' || c === "'" || c === "`"){ const q=c; let j=i+1; while(j<n && src[j]!==q){ if(src[j]==="\\") j++; j++; } j=Math.min(j+1,n); out+='<span class="t-str">'+esc(src.slice(i,j))+'</span>'; i=j; continue; }
if (c === "@"){ let j=i+1; while(j<n && isId(src[j])) j++; const at=src.slice(i,j); const id=ANNOTS[at];
if (id) out += link(id+".html", id, TIPS[at]||"A compile-time annotation.", '<span class="t-annot">'+esc(at)+'</span>');
else out += '<span class="t-annot">'+esc(at)+'</span>';
i=j; continue; }
if (/[0-9]/.test(c)){ let j=i; while(j<n && /[0-9a-fA-FxX._]/.test(src[j])) j++; out+='<span class="t-num">'+esc(src.slice(i,j))+'</span>'; i=j; continue; }
if (isIdStart(c)){
let j=i; while(j<n && isId(src[j])) j++; const word=src.slice(i,j);
// Namespace.member — link the two halves separately
if (NAMESPACES[word] && src[j]==="." && j+1<n && isIdStart(src[j+1])){
let k=j+1; while(k<n && isId(src[k])) k++; const member=src.slice(j+1,k);
const nsId=NAMESPACES[word];
out += link(nsId+".html", nsId, TIPS[word]||(word+" namespace"), '<span class="t-type">'+word+'</span>');
out += '<span class="t-punc">.</span>';
if (word === "Color"){
out += link("ns-color.html#"+member.toLowerCase(), "", "Named color "+word+"."+member+".", '<span class="t-fn">'+esc(member)+'</span>');
} else {
const key=word+"."+member, m=NSMETHODS[key];
if (m){
out += link(m.id+".html", m.id, TIPS[key]||key, '<span class="t-fn">'+esc(member)+'</span>');
if (src[nextNonSpace(k)]==="(") pending = {page:m.id, params:new Set(m.params||[])};
} else out += '<span class="t-fn">'+esc(member)+'</span>';
}
i=k; continue;
}
const ns = nextNonSpace(j);
const callish = src[ns] === "(";
const labelish = src[ns] === ":";
const top = stack.length ? stack[stack.length-1] : null;
if (KEYWORDS[word]){
out += link(KEYWORDS[word]+".html", KEYWORDS[word], TIPS[word], '<span class="t-key">'+word+'</span>');
} else if (BUILTINS[word] && callish){
const b=BUILTINS[word];
out += link(b.id+".html", b.id, TIPS[word], '<span class="t-fn">'+word+'</span>');
pending = {page:b.id, params:new Set(b.params||[])};
} else if (labelish && top && top.params.has(word)){
out += link(top.page+".html#param-"+word, "", "parameter: "+word, '<span class="t-arg">'+word+'</span>');
} else if (TYPES[word]){
out += link(TYPES[word]+".html", TYPES[word], TIPS[word]||("The "+word+" type."), '<span class="t-type">'+word+'</span>');
} else if (PHASES[word]){
out += link(PHASES[word]+".html", PHASES[word], TIPS[word]||("The "+word+" phase."), '<span class="t-type">'+word+'</span>');
} else if (callish){
out += '<span class="t-fn">'+esc(word)+'</span>';
} else {
out += esc(word);
}
i=j; continue;
}
if (c === "("){ stack.push(pending); pending=null; out+='<span class="t-punc">(</span>'; i++; continue; }
if (c === ")"){ stack.pop(); out+='<span class="t-punc">)</span>'; i++; continue; }
if (/[{}\[\]=<>+\-*\/%,.:;!&|~]/.test(c)){ out+='<span class="t-punc">'+esc(c)+'</span>'; i++; continue; }
out += esc(c); i++;
}
return out;
}
function highlightAll(){
document.querySelectorAll('pre[data-lang="ludic"]').forEach(pre => { pre.innerHTML = highlight(pre.textContent); });
}
/* ---- hover cards ------------------------------------------------------- */
let cardEl = null, cardTimer = null;
function ensureCard(){
if (cardEl) return cardEl;
cardEl = document.createElement("div"); cardEl.className = "hovercard"; cardEl.hidden = true;
document.body.appendChild(cardEl); return cardEl;
}
function showCard(a){
const id = a.getAttribute("data-id"); if (!id) return;
const c = CARDS[id]; if (!c) return;
const el = ensureCard();
el.innerHTML = '<div class="hc-top"><span class="kind-badge kind-'+(c.kind||"").replace("namespace-method","method")+'">'
+ (c.kind||"").replace("namespace-method","method") + '</span><span class="hc-name">'+esc(c.name||"")+'</span></div>'
+ (c.sig ? '<code class="hc-sig">'+esc(c.sig)+'</code>' : "")
+ '<div class="hc-tip">'+esc(c.tip||"")+'</div>'
+ '<div class="hc-foot">'+esc(c.section||"")+' · click to open</div>';
el.hidden = false;
const r = a.getBoundingClientRect();
const cw = el.offsetWidth, ch = el.offsetHeight;
let left = r.left, top = r.bottom + 8;
if (left + cw > window.innerWidth - 12) left = window.innerWidth - cw - 12;
if (left < 12) left = 12;
if (top + ch > window.innerHeight - 12) top = r.top - ch - 8;
el.style.left = Math.max(12,left) + "px"; el.style.top = Math.max(12,top) + "px";
}
function hideCard(){ if (cardEl) cardEl.hidden = true; }
function installCards(){
// hover cards are a pointer affordance; skip on touch so a tap just navigates
if (!(window.matchMedia && window.matchMedia("(hover: hover)").matches)) return;
document.addEventListener("mouseover", e => {
const a = e.target.closest && e.target.closest("a.tok[data-id]");
if (!a) return;
clearTimeout(cardTimer); cardTimer = setTimeout(()=>showCard(a), 130);
});
document.addEventListener("mouseout", e => {
const a = e.target.closest && e.target.closest("a.tok[data-id]");
if (a){ clearTimeout(cardTimer); hideCard(); }
});
window.addEventListener("scroll", hideCard, {passive:true});
}
/* ---- flash the scrolled-to target ------------------------------------- */
function flash(el){ if (!el) return; el.classList.remove("flash"); void el.offsetWidth; el.classList.add("flash"); }
function flashTarget(){
const go = () => {
const h = location.hash ? decodeURIComponent(location.hash.slice(1)) : "";
let el = h ? document.getElementById(h) : null;
if (!el) el = document.querySelector(".item-head") || document.getElementById("top");
flash(el);
};
if (document.readyState !== "loading") go(); else document.addEventListener("DOMContentLoaded", go);
window.addEventListener("hashchange", () => { const el = document.getElementById(decodeURIComponent(location.hash.slice(1))); flash(el); });
}
/* ---- fuzzy search on the API index ------------------------------------ */
function subseq(q, s){ let i=0; for (let k=0;k<s.length && i<q.length;k++) if (s[k]===q[i]) i++; return i===q.length; }
function installSearch(){
const box = document.getElementById("search"); if (!box) return;
const items = [...document.querySelectorAll(".idx-item")];
const secs = [...document.querySelectorAll(".idx-sec")];
const none = document.getElementById("noresults");
const run = () => {
const q = box.value.trim().toLowerCase();
let shown = 0;
items.forEach(it => {
const name = (it.getAttribute("data-name")||"").toLowerCase();
const tip = (it.getAttribute("data-tip")||"").toLowerCase();
const hit = !q || name.includes(q) || tip.includes(q) || subseq(q, name);
it.style.display = hit ? "" : "none"; if (hit) shown++;
});
secs.forEach(sec => { const any = [...sec.querySelectorAll(".idx-item")].some(i=>i.style.display!=="none"); sec.style.display = any ? "" : "none"; });
if (none) none.hidden = shown !== 0;
};
box.addEventListener("input", run);
box.addEventListener("keydown", e => { if (e.key === "Enter"){ const first = items.find(i=>i.style.display!=="none"); if (first) location.href = first.getAttribute("href"); } });
}
/* ---- mobile nav toggle (auto-wired on every page) ------------------- */
function initNav(){
document.querySelectorAll(".nav-toggle").forEach(btn => {
btn.addEventListener("click", () => {
const h = btn.closest("header.nav"); if (!h) return;
const open = h.classList.toggle("open");
btn.setAttribute("aria-expanded", open ? "true" : "false");
});
});
document.querySelectorAll("header.nav .nav-links a").forEach(a => {
a.addEventListener("click", () => {
const h = a.closest("header.nav");
if (h){ h.classList.remove("open"); const b = h.querySelector(".nav-toggle"); if (b) b.setAttribute("aria-expanded","false"); }
});
});
}
if (document.readyState !== "loading") initNav(); else document.addEventListener("DOMContentLoaded", initNav);
global.Ludic = { highlight, highlightAll, installCards, flashTarget, installSearch, initNav, SYMBOLS: S };
})(window);