File size: 6,609 Bytes
3bc016b | 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 | import { motion } from "framer-motion";
import { Fragment } from "react";
import { MONO } from "../theme";
// VS Code "Dark+" token palette. The code panel is always dark (like an
// embedded editor) so colors read the same in light or dark deck themes.
const C = {
bg: "#0d1117",
panelBorder: "#30363d",
headerBg: "#161b22",
text: "#d4d4d4",
comment: "#6a9955",
string: "#ce9178",
number: "#b5cea8",
keyword: "#c586c0", // control flow
decl: "#569cd6", // def / class / None / True / False / self
type: "#4ec9b0", // known types / classes
func: "#dcdcaa", // function calls & decorators
op: "#d4d4d4",
dim: "#8b949e",
};
const CONTROL = new Set([
"import", "from", "return", "if", "else", "elif", "for", "while", "in", "not",
"and", "or", "with", "as", "try", "except", "finally", "raise", "lambda",
"yield", "async", "await", "pass", "break", "continue", "is", "del", "global",
]);
const DECL = new Set(["def", "class", "None", "True", "False", "self", "super"]);
const TYPES = new Set([
"str", "int", "float", "bool", "list", "dict", "tuple", "set", "bytes",
"Observation", "State", "Action", "Rubric", "FastMCP", "MCPEnvironment",
"GRPOConfig", "GRPOTrainer", "Sandbox", "CodingEnv", "TestsPassRubric",
]);
type Tok = { t: string; c: string };
// Tokenize one line of Python into colored spans.
function pyTokens(line: string): Tok[] {
const out: Tok[] = [];
let i = 0;
const push = (t: string, c: string) => t && out.push({ t, c });
while (i < line.length) {
const ch = line[i];
// comment
if (ch === "#") {
push(line.slice(i), C.comment);
break;
}
// string
if (ch === '"' || ch === "'") {
let j = i + 1;
while (j < line.length && line[j] !== ch) {
if (line[j] === "\\") j++;
j++;
}
push(line.slice(i, j + 1), C.string);
i = j + 1;
continue;
}
// decorator
if (ch === "@") {
let j = i + 1;
while (j < line.length && /[\w.]/.test(line[j])) j++;
push(line.slice(i, j), C.func);
i = j;
continue;
}
// number
if (/[0-9]/.test(ch)) {
let j = i;
while (j < line.length && /[0-9._]/.test(line[j])) j++;
push(line.slice(i, j), C.number);
i = j;
continue;
}
// identifier / keyword
if (/[A-Za-z_]/.test(ch)) {
let j = i;
while (j < line.length && /[\w]/.test(line[j])) j++;
const word = line.slice(i, j);
const isCall = line[j] === "(";
let color = C.text;
if (CONTROL.has(word)) color = C.keyword;
else if (DECL.has(word)) color = C.decl;
else if (TYPES.has(word)) color = C.type;
else if (isCall) color = C.func;
push(word, color);
i = j;
continue;
}
// whitespace / punctuation run
let j = i;
while (j < line.length && !/[\w"'@#]/.test(line[j])) j++;
push(line.slice(i, Math.max(j, i + 1)), C.op);
i = Math.max(j, i + 1);
}
return out;
}
// Tokenize one line of a shell transcript.
function shTokens(line: string): Tok[] {
const trimmed = line.trimStart();
if (!trimmed.startsWith("$")) return [{ t: line, c: C.comment }];
const out: Tok[] = [];
const lead = line.slice(0, line.length - trimmed.length);
out.push({ t: lead + "$", c: C.dim });
const rest = trimmed.slice(1);
const hash = rest.indexOf("#");
const cmd = hash >= 0 ? rest.slice(0, hash) : rest;
const comment = hash >= 0 ? rest.slice(hash) : "";
cmd.split(/(\s+)/).forEach((tok, idx) => {
if (/^\s+$/.test(tok) || tok === "") out.push({ t: tok, c: C.text });
else if (tok.startsWith("--") || tok.startsWith("-")) out.push({ t: tok, c: C.decl });
else if (idx === 1) out.push({ t: tok, c: C.func }); // command name
else out.push({ t: tok, c: C.text });
});
if (comment) out.push({ t: comment, c: C.comment });
return out;
}
function renderLine(line: string, lang: string) {
// ⟪…⟫ marks an emphasized span (emerald tint background), tokens still colored
const parts = line.split(/(⟪[^⟫]*⟫)/g).filter((s) => s.length > 0);
return parts.map((part, pi) => {
const hi = part.startsWith("⟪") && part.endsWith("⟫");
const text = hi ? part.slice(1, -1) : part;
const toks = lang === "bash" ? shTokens(text) : pyTokens(text);
const inner = toks.map((tk, i) => (
<span key={i} style={{ color: tk.c }}>
{tk.t}
</span>
));
return hi ? (
<span key={pi} style={{ background: "rgba(16,240,164,0.14)", borderRadius: 3 }}>
{inner}
</span>
) : (
<Fragment key={pi}>{inner}</Fragment>
);
});
}
export function CodeBlock({
filename,
lang = "python",
code,
fontSize = 22,
delay = 0.25,
}: {
filename?: string;
lang?: string;
code: string;
fontSize?: number;
delay?: number;
}) {
const lines = code.replace(/\n$/, "").split("\n");
return (
<motion.div
initial={{ opacity: 0, y: 18 }}
animate={{ opacity: 1, y: 0 }}
transition={{ type: "spring", damping: 24, delay }}
style={{
background: C.bg,
border: `1px solid ${C.panelBorder}`,
borderRadius: 14,
overflow: "hidden",
boxShadow: "0 12px 40px rgba(0,0,0,0.4)",
}}
>
{filename && (
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
padding: "11px 20px",
background: C.headerBg,
borderBottom: `1px solid ${C.panelBorder}`,
fontFamily: MONO,
fontSize: 15,
color: C.dim,
}}
>
<span style={{ display: "flex", alignItems: "center", gap: 8 }}>
<span style={{ width: 11, height: 11, borderRadius: "50%", background: "#ff5f56" }} />
<span style={{ width: 11, height: 11, borderRadius: "50%", background: "#ffbd2e" }} />
<span style={{ width: 11, height: 11, borderRadius: "50%", background: "#27c93f" }} />
<span style={{ marginLeft: 8 }}>{filename}</span>
</span>
<span style={{ letterSpacing: 2, textTransform: "uppercase" }}>{lang}</span>
</div>
)}
<pre
style={{
margin: 0,
padding: "20px 24px",
fontFamily: MONO,
fontSize,
lineHeight: 1.55,
whiteSpace: "pre",
overflow: "hidden",
color: C.text,
}}
>
{lines.map((line, i) => (
<div key={i}>{line ? renderLine(line, lang) : " "}</div>
))}
</pre>
</motion.div>
);
}
|