dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / tools / embed_css / module.lucb
#==============================================================================================
#
# embed_css - the generator of the engine's embedded style sheets
#
# DESCRIPTION:
# Port of Meta/Generators/embed_as_string.py, which embeds a file into a `String`
# variable "a la #embed from C++23". It writes, from the style sheets copied into
# data/css, one fragment each into src/web/generated:
#
# default_style_sheet_source.lucb Default.css: Web::CSS::default_stylesheet_source
# (Build/.../CSS/DefaultStyleSheetSource.cpp)
# quirks_mode_style_sheet_source.lucb QuirksMode.css: Web::CSS::quirks_mode_stylesheet_source
# (Build/.../CSS/QuirksModeStyleSheetSource.cpp)
# mathml_style_sheet_source.lucb MathML/Default.css: Web::CSS::mathml_stylesheet_source
# (Build/.../MathML/MathMLStyleSheetSource.cpp)
# svg_style_sheet_source.lucb SVG/Default.css: Web::CSS::svg_stylesheet_source
# (Build/.../SVG/SVGStyleSheetSource.cpp)
#
# The C++ variable is a `String`; here it is a `str` constant (static text), from
# which a String is made where one is needed (`ak.string_from_str`). The text is a
# triple-quoted literal whose closing delimiter is in column 0, so no indentation is
# stripped; backslashes are escaped (the sheets use CSS escapes such as `\25BE`).
#
# Usage: embed_css DATA_DIRECTORY OUTPUT_DIRECTORY, e.g. embed_css data/css
# src/web/generated. test.sh regenerates into a temporary directory
# and compares with the committed fragments.
#
# Copyright (c) 2024-2026, the Ladybird developers.
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause (see data/css/LICENSE)
#
#==============================================================================================
import c
from luce_std import collections, files
## The style sheets: (input file, output fragment, C++ variable, Luce name, C++ source file under
## Build/Libraries/LibWeb).
let sheets: (str, str, str, str, str)[4] = [
("Default.css", "default_style_sheet_source", "default_stylesheet_source", "css_default_stylesheet_source", "CSS/DefaultStyleSheetSource.cpp"),
("QuirksMode.css", "quirks_mode_style_sheet_source", "quirks_mode_stylesheet_source", "css_quirks_mode_stylesheet_source", "CSS/QuirksModeStyleSheetSource.cpp"),
("MathML/Default.css", "mathml_style_sheet_source", "mathml_stylesheet_source", "css_mathml_stylesheet_source", "MathML/MathMLStyleSheetSource.cpp"),
("SVG/Default.css", "svg_style_sheet_source", "svg_stylesheet_source", "css_svg_stylesheet_source", "SVG/SVGStyleSheetSource.cpp"),
]
## Embed every style sheet of `arguments[1]` into `arguments[2]`.
pub func main(arguments: c.str[]) -> i32!:
if arguments.length != 3:
print("usage: embed_css DATA_DIRECTORY OUTPUT_DIRECTORY")
return 2
let data_directory = try str(arguments[1])
let output_directory = try str(arguments[2])
for sheet in sheets:
let (input, fragment, variable, luce_name, source) = sheet
let text = try files.read(try c_cat([data_directory, "/", input]))
let output = try embed(text.value, input, fragment, variable, luce_name, source)
text.release()
try files.write(try c_cat([output_directory, "/", fragment, ".lucb"]), output.bytes)
return 0
## The fragment that embeds `text`.
func embed(text: const u8[], input: str, fragment: str, variable: str, luce_name: str, source: str) -> str!:
let rule = "#=============================================================================================="
var builder: collections.Buffer
try builder.put_text(rule)
try builder.put_text("\n#\n")
_ = try builder.write(f"# {fragment} - {input}, embedded\n")
try builder.put_text("#\n# DESCRIPTION:\n")
_ = try builder.write(f"# Generated by tools/embed_css from data/css/{input}; do not edit.\n")
_ = try builder.write(f"# The C++ it stands for, at Ladybird 47c82b38d0, is\n")
_ = try builder.write(f"# Build/Libraries/LibWeb/{source},\n")
try builder.put_text("# written by Meta/Generators/embed_as_string.py.\n")
try builder.put_text("#\n# Copyright (c) 2018-2026, the Ladybird developers.\n")
try builder.put_text("# SPDX-License-Identifier: BSD-2-Clause (see data/css/LICENSE)\n#\n")
try builder.put_text(rule)
try builder.put_text("\n\n")
_ = try builder.write(f"## Web::CSS::{variable} ({source}): the text of {input}.\n")
_ = try builder.write(f"pub let {luce_name}: str = \"\"\"\n")
for byte in text:
if byte == '\\':
try builder.put_text("\\\\")
elif byte == '\t':
try builder.put_text("\\t")
elif byte == '\r':
try builder.put_text("\\r")
else:
let one: u8[1] = [byte]
_ = try builder.write(one)
# The closing delimiter follows the last character of the text (a sheet's final line feed included).
try builder.put_text("\"\"\"\n")
return builder.text()
## The concatenation of `parts` as the C string the files module takes.
func c_cat(parts: const str[]) -> c.str!:
var builder: collections.Buffer
for part in parts:
try builder.put_text(part)
return try builder.terminated()