/** * css-extractor.ts — Extract component CSS from src/widgets/*.c files. * * LCUI widget C source files define their default CSS using either: * * static const char *ui_button_css = css_string( * button { ... } * ); * * or inline string literals: * * static const char *ui_scrollarea_css = "\ * scrollarea-content { min-width: min-content; }"; * * This extractor handles both patterns and concatenates all extracted CSS * into a single docs/build/public/component-styles.css file for use by the * Docusaurus web compiler (M5) to render static component previews. */ import { readFileSync, readdirSync, writeFileSync } from "fs"; import { join } from "path"; function extractCssStringMacro(source: string): string[] { const results: string[] = []; const re = /=\s*css_string\s*\(\s*([\s\S]*?)\s*\)\s*;/g; let m: RegExpExecArray | null; while ((m = re.exec(source)) !== null) { results.push(m[1].trim()); } return results; } function extractCStringLiteral(source: string): string[] { const results: string[] = []; const re = /=\s*"((?:[^"\\]|\\[\s\S])*?)"\s*;/g; let m: RegExpExecArray | null; while ((m = re.exec(source)) !== null) { const val = m[1] .replace(/\\\n/g, "\n") .replace(/\\n/g, "\n") .replace(/\\t/g, "\t") .replace(/\\"/g, '"'); if (val.includes("{")) { results.push(val.trim()); } } return results; } export async function extractWidgetCss( widgetsSrcDir: string, outDir: string, ): Promise { let files: string[]; try { files = readdirSync(widgetsSrcDir).filter((f) => f.endsWith(".c")); } catch { console.warn( `[css-extractor] Could not read ${widgetsSrcDir} — skipping CSS extraction`, ); writeFileSync( join(outDir, "component-styles.css"), "/* CSS extraction skipped: src/widgets/ not found */\n", "utf8", ); return; } const chunks = [ "/* LCUI Component Styles", " * Auto-generated by tools/doc-compiler/css-extractor.ts", " * Sources: src/widgets/*.c", " */\n", ]; for (const file of files.sort()) { const filePath = join(widgetsSrcDir, file); const source = readFileSync(filePath, "utf8"); const fromMacro = extractCssStringMacro(source); const fromLiteral = extractCStringLiteral(source); const all = [...fromMacro, ...fromLiteral]; if (all.length > 0) { chunks.push(`/* ── ${file} ${"─".repeat(Math.max(0, 44 - file.length))} */`); for (const css of all) { chunks.push(css); } chunks.push(""); } } writeFileSync(join(outDir, "component-styles.css"), chunks.join("\n"), "utf8"); }