Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-css

Source of release 0.5.1

0.5.1 / tools / gen_css / module.lucb

#==============================================================================================
#
#   gen_css - the generator of css_data's fragments
#
#   DESCRIPTION:
#       Ladybird generates LibWeb's CSS data (Build/Libraries/LibWeb/CSS/PropertyID.*,
#       Keyword.*, Enums.*, Units.*, ...) at build time with the Python programs in
#       Meta/Generators, from the JSON files in Libraries/LibWeb/CSS. This program is a port
#       of those generators' logic (DESIGN.md §5): it reads the same JSON, copied into
#       data/css, and writes Luce instead of C++, one fragment of the css_data module per
#       generated C++ header and implementation:
#
#           generated_descriptor_id.lucb          Descriptors.json
#           generated_enums.lucb                  Enums.json
#           generated_environment_variable.lucb   EnvironmentVariables.json
#           generated_keyword.lucb                Keywords.json
#           generated_math_functions.lucb         MathFunctions.json
#           generated_media_feature_id.lucb       MediaFeatures.json
#           generated_property_id*.lucb           Properties.json, Enums.json,
#                                                 LogicalPropertyGroups.json
#           generated_pseudo_class.lucb           PseudoClasses.json
#           generated_pseudo_element.lucb         PseudoElements.json
#           generated_transform_functions.lucb    TransformFunctions.json
#           generated_units.lucb                  Units.json
#
#       Functions of those files that build or parse style values (property_initial_value,
#       property_accepts_length, parse_math_function, ...) belong to the engine, which has
#       the StyleValue types (DESIGN.md §4.1.3); they are not written here.
#
#       Usage: gen_css DATA_DIRECTORY OUTPUT_DIRECTORY, e.g. gen_css data/css
#       src/css_data. test.sh regenerates into a temporary directory and
#       compares with the committed fragments.
#
#       Copyright (c) 2018-2026, the Ladybird developers.
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause (see LICENSE)
#
#==============================================================================================

import c
from luce_std import collections, files
import strings

## Generate every fragment from the JSON of `arguments[1]` into `arguments[2]`.
pub func main(arguments: c.str[]) -> i32!:
    if arguments.length != 3:
        print("usage: gen_css DATA_DIRECTORY OUTPUT_DIRECTORY")
        return 2
    let data_directory = try str(arguments[1])
    let output_directory = try str(arguments[2])

    let enums = try read_json(data_directory, "Enums.json")
    try write_output(output_directory, "generated_enums.lucb", try emit_enums(enums))
    var property_id_outputs = try StrList.create()
    try emit_property_id(try read_json(data_directory, "Properties.json"), enums,
                         try read_json(data_directory, "LogicalPropertyGroups.json"), &property_id_outputs)
    try write_output(output_directory, "generated_property_id.lucb", property_id_outputs.at(0))
    try write_output(output_directory, "generated_property_id_2.lucb", property_id_outputs.at(1))
    try write_output(output_directory, "generated_property_id_3.lucb", property_id_outputs.at(2))
    try write_output(output_directory, "generated_keyword.lucb", try emit_keyword(try read_json(data_directory, "Keywords.json")))
    try write_output(output_directory, "generated_environment_variable.lucb", try emit_environment_variable(try read_json(data_directory, "EnvironmentVariables.json")))
    try write_output(output_directory, "generated_math_functions.lucb", try emit_math_functions(try read_json(data_directory, "MathFunctions.json")))
    try write_output(output_directory, "generated_media_feature_id.lucb", try emit_media_feature_id(try read_json(data_directory, "MediaFeatures.json")))
    try write_output(output_directory, "generated_pseudo_class.lucb", try emit_pseudo_class(try read_json(data_directory, "PseudoClasses.json")))
    try write_output(output_directory, "generated_pseudo_element.lucb", try emit_pseudo_element(try read_json(data_directory, "PseudoElements.json")))
    try write_output(output_directory, "generated_transform_functions.lucb", try emit_transform_functions(try read_json(data_directory, "TransformFunctions.json")))
    try write_output(output_directory, "generated_units.lucb", try emit_units(try read_json(data_directory, "Units.json")))
    try write_output(output_directory, "generated_descriptor_id.lucb", try emit_descriptors(try read_json(data_directory, "Descriptors.json")))
    return 0

## The JSON file `name` of `directory`, parsed. Its numbers borrow the file's bytes, which the
## whole run keeps, so they are never released.
func read_json(directory: str, name: str) -> Json*!:
    let data = try files.read(try file_path(directory, name))
    return try json_parse(data.value)

## Write `text` to the file `name` of `directory`.
func write_output(directory: str, name: str, text: str) -> !:
    try files.write(try file_path(directory, name), text.bytes)

## `directory/name` as the C string the files module takes; like every string here, it
## lives for the whole run.
func file_path(directory: str, name: str) -> c.str!:
    var path: collections.Buffer
    try path.put_text(directory)
    try path.put_text("/")
    try path.put_text(name)
    return try path.terminated()