/**
* Fetch the mod portal stylesheet and keep the parts the UI ports:
* docs/reference/portal-css.txt component rules (committed, for reading): our widget list
* plus every rule that touches a class or id on the search page
* (filter sidebar, exclude toggles, tooltips) and base typography
* assets/portal/*.png|jpg border-image / background data URIs (git-ignored)
*
* bun tools/fetch-portal-css.ts
*/
import { mkdirSync, writeFileSync } from 'node:fs'
import path from 'node:path'
const CSS_URL = 'https://factorio.com/static/css/main.css'
// The portal page that carries the most widgets (search box, filter sidebar, exclude toggles, tooltips).
const PAGE_URL = 'https://mods.factorio.com/search?query='
// The mod page adds the info table (dl/dt/dd), gallery and description (article) rules.
const MOD_PAGE_URL = 'https://mods.factorio.com/mod/scattergun_turret'
const root = path.resolve(import.meta.dir, '..')
const assetsDir = path.join(root, 'assets/portal')
mkdirSync(assetsDir, { recursive: true })
mkdirSync(path.join(root, 'docs/reference'), { recursive: true })
const css = await (await fetch(CSS_URL)).text()
const page = (await (await fetch(PAGE_URL)).text()) + (await (await fetch(MOD_PAGE_URL)).text())
const pageClasses = new Set([...page.matchAll(/class="([^"]*)"/g)].flatMap((m) => m[1]!.split(/\s+/)).filter(Boolean))
const pageIds = new Set([...page.matchAll(/id="([^"]+)"/g)].map((m) => m[1]!))
// Replace each data URI with a numbered file so rules stay readable.
const seen = new Map<string, string>()
const readable = css.replace(/url\("?data:image\/(png|jpeg|svg\+xml);base64,([^")]+)"?\)/g, (_m, type: string, data: string) => {
let name = seen.get(data)
if (!name) {
const ext = type === 'jpeg' ? 'jpg' : type === 'png' ? 'png' : 'svg'
name = `data-${String(seen.size).padStart(2, '0')}.${ext}`
seen.set(data, name)
writeFileSync(path.join(assetsDir, name), Buffer.from(data, 'base64'))
}
return `url(assets/portal/${name})`
})
const WANT = /(^|[\s,>])\.(button[\w-]*|panel[\w-]*|tabs[\w-]*|tab|checkbox[\w-]*|dropdown|slot[\w-]*|mod-[\w-]+|progress-bar[\w-]*|select[\w-]*|input[\w-]*|table[\w-]*|shelf[\w-]*)\b/
// Description / info table / gallery selectors that only use element names (article h3, dl dt, ul li).
const MOD_PAGE = /(panel-hole-combined|mod-page-info|gallery|panel-hole|(^|[\s,>])(article|dl|dt|dd|ul|li|code|pre|blockquote|hr|img|h[1-5])([\s,>.:{[]|$))/
const TYPOGRAPHY = /^(html|body|h[1-4]|p|a|label|input|button)(\s*,\s*(html|body|h[1-4]|p|a|label|input|button))*$/
const onPage = (selector: string) =>
[...selector.matchAll(/\.([\w-]+)/g)].some((t) => pageClasses.has(t[1]!)) ||
[...selector.matchAll(/#([\w-]+)/g)].some((t) => pageIds.has(t[1]!))
const rules: string[] = []
for (const m of readable.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
const selector = m[1]!.trim()
if (selector.startsWith('@')) continue
if (!WANT.test(` ${selector}`) && !onPage(selector) && !TYPOGRAPHY.test(selector) && !MOD_PAGE.test(selector)) continue
rules.push(`${selector} {\n ${m[2]!.trim().replace(/;\s*/g, ';\n ').replace(/\s+$/, '')}\n}`)
}
const header = `/* Extracted from ${CSS_URL} (filtered by the widgets on ${PAGE_URL}) on ${new Date().toISOString().slice(0, 10)} by tools/fetch-portal-css.ts.\n data: URIs were saved to assets/portal/. Reference for the Slint widgets under src/view/. */\n\n`
writeFileSync(path.join(root, 'docs/reference/portal-css.txt'), header + rules.join('\n\n') + '\n')
console.log(`${rules.length} rules, ${seen.size} images -> assets/portal/`)