|
| 1 | +// Generate utilities metadata JSON for documentation |
| 2 | +// This file is compiled to extract utility information without generating CSS |
| 3 | + |
| 4 | +@use "sass:map"; |
| 5 | +@use "sass:list"; |
| 6 | +@use "sass:string"; |
| 7 | +@use "sass:meta"; |
| 8 | +@use "../scss/config" as *; |
| 9 | +@use "../scss/colors" as *; |
| 10 | +@use "../scss/variables" as *; |
| 11 | +@use "../scss/functions" as *; |
| 12 | +@use "../scss/theme" as *; |
| 13 | +@use "../scss/utilities" as *; |
| 14 | + |
| 15 | +// Access the utilities map |
| 16 | +$utilities-map: $utilities !default; |
| 17 | + |
| 18 | +// Start JSON output |
| 19 | +$json: '{"utilities":{' !default; |
| 20 | + |
| 21 | +$utility-count: 0 !default; |
| 22 | +$total-utilities: list.length(map.keys($utilities-map)) !default; |
| 23 | + |
| 24 | +@each $key, $utility in $utilities-map { |
| 25 | + $utility-count: $utility-count + 1; |
| 26 | + |
| 27 | + // Skip if utility is null or false (disabled) |
| 28 | + @if $utility { |
| 29 | + // Extract class prefix |
| 30 | + $class: if(map.has-key($utility, "class"), map.get($utility, "class"), $key); |
| 31 | + |
| 32 | + // Extract property |
| 33 | + $property: if(map.has-key($utility, "property"), map.get($utility, "property"), null); |
| 34 | + |
| 35 | + // Extract values |
| 36 | + $values: if(map.has-key($utility, "values"), map.get($utility, "values"), null); |
| 37 | + |
| 38 | + // Generate class list |
| 39 | + $classes: ""; |
| 40 | + @if $values { |
| 41 | + @if meta.type-of($values) == "map" { |
| 42 | + $value-keys: map.keys($values); |
| 43 | + $first: true; |
| 44 | + @each $value-key in $value-keys { |
| 45 | + @if not $first { |
| 46 | + $classes: $classes + ", "; |
| 47 | + } |
| 48 | + $class-name: if($value-key == "null" or $value-key == null, $class, "#{$class}-#{$value-key}"); |
| 49 | + $classes: $classes + '"' + $class-name + '"'; |
| 50 | + $first: false; |
| 51 | + } |
| 52 | + } @else if meta.type-of($values) == "list" { |
| 53 | + $first: true; |
| 54 | + @each $value in $values { |
| 55 | + @if not $first { |
| 56 | + $classes: $classes + ", "; |
| 57 | + } |
| 58 | + $class-name: "#{$class}-#{$value}"; |
| 59 | + $classes: $classes + '"' + $class-name + '"'; |
| 60 | + $first: false; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Build JSON entry |
| 66 | + $json: $json + '"' + $key + '":{"class":"' + $class + '"'; |
| 67 | + |
| 68 | + @if $property { |
| 69 | + @if meta.type-of($property) == "string" { |
| 70 | + $json: $json + ',"property":"' + $property + '"'; |
| 71 | + } @else if meta.type-of($property) == "list" { |
| 72 | + $property-str: ""; |
| 73 | + $first: true; |
| 74 | + @each $prop in $property { |
| 75 | + @if not $first { |
| 76 | + $property-str: $property-str + " "; |
| 77 | + } |
| 78 | + $property-str: $property-str + $prop; |
| 79 | + $first: false; |
| 80 | + } |
| 81 | + $json: $json + ',"property":"' + $property-str + '"'; |
| 82 | + } |
| 83 | + // Skip map properties as they're complex and don't translate to JSON well |
| 84 | + } |
| 85 | + |
| 86 | + @if $classes != "" { |
| 87 | + $json: $json + ',"classes":[' + $classes + "]"; |
| 88 | + } @else { |
| 89 | + $json: $json + ',"classes":[]'; |
| 90 | + } |
| 91 | + |
| 92 | + $json: $json + "}"; |
| 93 | + |
| 94 | + @if $utility-count < $total-utilities { |
| 95 | + $json: $json + ","; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// stylelint-disable-next-line scss/dollar-variable-default |
| 101 | +$json: $json + "}}"; |
| 102 | + |
| 103 | +// Output as CSS comment so it appears in compiled file |
| 104 | + |
| 105 | +/*! BOOTSTRAP-UTILITIES-METADATA-START |
| 106 | +#{$json} |
| 107 | +BOOTSTRAP-UTILITIES-METADATA-END */ |
| 108 | + |
| 109 | +// Prevent any actual CSS output |
| 110 | +.bootstrap-utilities-metadata-generator { |
| 111 | + content: "This file should not generate CSS, only metadata comments"; |
| 112 | +} |
0 commit comments