diff --git a/crates/gosub_styling/tools/generate_definitions/.gitignore b/crates/gosub_styling/tools/generate_definitions/.gitignore new file mode 100644 index 000000000..321f722e9 --- /dev/null +++ b/crates/gosub_styling/tools/generate_definitions/.gitignore @@ -0,0 +1,2 @@ +.cache +.output diff --git a/crates/gosub_styling/tools/generate_definitions/main.go b/crates/gosub_styling/tools/generate_definitions/main.go index 584d4fcba..e9fdc73e3 100644 --- a/crates/gosub_styling/tools/generate_definitions/main.go +++ b/crates/gosub_styling/tools/generate_definitions/main.go @@ -9,6 +9,7 @@ import ( "log" "os" "path" + "sort" ) type ExportType int @@ -21,7 +22,7 @@ const ( const ( exportType = Both - ResourcePath = "crates/gosub_styling/resources/definitions" + ResourcePath = ".output" SingleFilePath = ResourcePath + "/definitions.json" MultiFileDir = ResourcePath MultiFilePrefix = "definitions_" @@ -41,7 +42,16 @@ func main() { len(webrefData.Properties), len(webrefData.Values), len(webrefData.AtRules), len(webrefData.Selectors), ) + sort.Slice(webrefData.Properties, func(i, j int) bool { + return webrefData.Properties[i].Name < webrefData.Properties[j].Name + }) + for _, property := range webrefData.Properties { + if property.Syntax == "" { + // Skip any empty syntax, as they will be filled later with alias table + continue + } + prop := utils.Property{ Name: property.Name, Syntax: property.Syntax, @@ -100,6 +110,23 @@ func main() { Descriptors: descriptors, Values: atRule.Values, }) + + for _, property := range webrefData.Properties { + if property.Syntax != "" { + // Skip properties with syntax + continue + } + + alias, err := webref.GetAlias(property.Name) + if err != nil { + log.Panic("failed to get alias syntax for property: " + property.Name) + } + + data.PropAliases = append(data.PropAliases, utils.PropAlias{ + Name: property.Name, + For: alias, + }) + } } data.Selectors = webrefData.Selectors @@ -140,6 +167,7 @@ func ExportMultiFile(data *utils.Data) { ExportData(data.Values, path.Join(MultiFileDir, MultiFilePrefix+"values.json")) ExportData(data.AtRules, path.Join(MultiFileDir, MultiFilePrefix+"at-rules.json")) ExportData(data.Selectors, path.Join(MultiFileDir, MultiFilePrefix+"selectors.json")) + ExportData(data.PropAliases, path.Join(MultiFileDir, MultiFilePrefix+"prop-aliases.json")) } diff --git a/crates/gosub_styling/tools/generate_definitions/utils/types.go b/crates/gosub_styling/tools/generate_definitions/utils/types.go index b4b08e943..135e57025 100644 --- a/crates/gosub_styling/tools/generate_definitions/utils/types.go +++ b/crates/gosub_styling/tools/generate_definitions/utils/types.go @@ -1,10 +1,16 @@ package utils type Data struct { - Properties []Property `json:"properties"` - Values []Value `json:"values"` - AtRules []AtRule `json:"atrules"` - Selectors []Selector `json:"selectors"` + Properties []Property `json:"properties"` + Values []Value `json:"values"` + AtRules []AtRule `json:"atrules"` + Selectors []Selector `json:"selectors"` + PropAliases []PropAlias `json:"propAliases"` +} + +type PropAlias struct { + Name string `json:"name"` + For string `json:"for"` } type Value struct { diff --git a/crates/gosub_styling/tools/generate_definitions/utils/utils.go b/crates/gosub_styling/tools/generate_definitions/utils/utils.go index 3599e0866..a900ee2c1 100644 --- a/crates/gosub_styling/tools/generate_definitions/utils/utils.go +++ b/crates/gosub_styling/tools/generate_definitions/utils/utils.go @@ -10,7 +10,7 @@ const ( REPO = "w3c/webref" LOCATION = "ed/css" PATCH_LOCATION = "ed/csspatches" - CACHE_DIR = "crates/gosub_styling/resources/cache" + CACHE_DIR = ".cache" CACHE_INDEX_FILE = CACHE_DIR + "/index/cache_index.json" CUSTOM_PATCH_DIR = "crates/gosub_styling/resources/patches" BRANCH = "curated" diff --git a/crates/gosub_styling/tools/generate_definitions/webref/webref.go b/crates/gosub_styling/tools/generate_definitions/webref/webref.go index d86472301..53dacd0ff 100644 --- a/crates/gosub_styling/tools/generate_definitions/webref/webref.go +++ b/crates/gosub_styling/tools/generate_definitions/webref/webref.go @@ -2,6 +2,7 @@ package webref import ( "encoding/json" + "errors" "generate_definitions/utils" "io" "log" @@ -198,6 +199,10 @@ func DownloadFileContent(file *utils.DirectoryListItem) []byte { return nil } + if err := os.MkdirAll(path.Dir(cachePath), 0755); err != nil { + log.Panic("Failed to create cache directory ", path.Dir(cachePath), err) + } + if err := os.WriteFile(cachePath, body, 0644); err != nil { log.Panic("Failed to write cache file ", cachePath, err) } @@ -306,12 +311,11 @@ func skip(shortname string) bool { return true } -// ProcessValue will process a single value (from either root values or propertt values) and add it +// ProcessValue will process a single value (from either root values or property values) and add it // to the ParseData if possible. func ProcessValue(name string, type_ string, syntax string, pd *ParseData) { if name == syntax { - //log.Println("name == syntax for ", name) - + // log.Println("name == syntax for ", name) return } @@ -369,11 +373,13 @@ func DecodeFileContent(content []byte, pd *ParseData) { } for _, property := range fileData.Properties { - //log.Println("Processing property: ", property.Name) + if property.Name == "stop-color" || property.Name == "stop-opacity" { + // Some hardcoded skips + return + } for _, v := range property.Values { ProcessValue(v.Name, v.Type, v.Syntax, pd) - ProcessExtraValues(v.Values, pd) } @@ -444,3 +450,83 @@ func ProcessExtraValues(values []WebRefValue, pd *ParseData) { ProcessExtraValues(value.Values, pd) } } + +var PropertyAliasTable = map[string]string{ + "-webkit-align-content": "align-content", + "-webkit-align-items": "align-items", + "-webkit-align-self": "align-self", + "-webkit-animation": "animation", + "-webkit-animation-delay": "animation-delay", + "-webkit-animation-direction": "animation-direction", + "-webkit-animation-duration": "animation-duration", + "-webkit-animation-fill-mode": "animation-fill-mode", + "-webkit-animation-iteration-count": "animation-iteration-count", + "-webkit-animation-name": "animation-name", + "-webkit-animation-play-state": "animation-play-state", + "-webkit-animation-timing-function": "animation-timing-function", + "-webkit-appearance": "appearance", + "-webkit-backface-visibility": "backface-visibility", + "-webkit-background-clip": "background-clip", + "-webkit-background-origin": "background-origin", + "-webkit-background-size": "background-size", + "-webkit-border-bottom-left-radius": "border-bottom-left-radius", + "-webkit-border-bottom-right-radius": "border-bottom-right-radius", + "-webkit-border-radius": "border-radius", + "-webkit-border-top-left-radius": "border-top-left-radius", + "-webkit-border-top-right-radius": "border-top-right-radius", + "-webkit-box-align": "box-align", + "-webkit-box-flex": "box-flex", + "-webkit-box-ordinal-group": "box-ordinal-group", + "-webkit-box-orient": "box-orient", + "-webkit-box-pack": "box-pack", + "-webkit-box-shadow": "box-shadow", + "-webkit-box-sizing": "box-sizing", + "-webkit-filter": "filter", + "-webkit-flex": "flex", + "-webkit-flex-basis": "flex-basis", + "-webkit-flex-direction": "flex-direction", + "-webkit-flex-flow": "flex-flow", + "-webkit-flex-grow": "flex-grow", + "-webkit-flex-shrink": "flex-shrink", + "-webkit-flex-wrap": "flex-wrap", + "-webkit-justify-content": "justify-content", + "-webkit-mask": "mask", + "-webkit-mask-box-image": "mask-border", + "-webkit-mask-box-image-outset": "mask-border-outset", + "-webkit-mask-box-image-repeat": "mask-border-repeat", + "-webkit-mask-box-image-slice": "mask-border-slice", + "-webkit-mask-box-image-source": "mask-border-source", + "-webkit-mask-box-image-width": "mask-border-width", + "-webkit-mask-clip": "mask-clip", + "-webkit-mask-composite": "mask-composite", + "-webkit-mask-image": "mask-image", + "-webkit-mask-origin": "mask-origin", + "-webkit-mask-position": "mask-position", + "-webkit-mask-repeat": "mask-repeat", + "-webkit-mask-size": "mask-size", + "-webkit-order": "order", + "-webkit-perspective": "perspective", + "-webkit-perspective-origin": "perspective-origin", + "-webkit-text-size-adjust": "text-size-adjust", + "-webkit-transform": "transform", + "-webkit-transform-origin": "transform-origin", + "-webkit-transform-style": "transform-style", + "-webkit-transition": "transition", + "-webkit-transition-delay": "transition-delay", + "-webkit-transition-duration": "transition-duration", + "-webkit-transition-property": "transition-property", + "-webkit-transition-timing-function": "transition-timing-function", + "-webkit-user-select": "user-select", + "font-stretch": "font-width", + "grid-column-gap": "column-gap", + "grid-gap": "gap", + "grid-row-gap": "row-gap", +} + +func GetAlias(propName string) (string, error) { + if PropertyAliasTable[propName] == "" { + return "", errors.New("No alias syntax for property: " + propName) + } + + return PropertyAliasTable[propName], nil +}