File size: 7,488 Bytes
06966eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import json


class TokenCodeGenerator:
    def generate_css_variables(self, tokens):
        """Generate CSS custom properties"""
        css = ":root {\n"
        
        # Colors
        for name, color in tokens.get('colors', {}).items():
            css += f"  --color-{name}: {color['hex']};\n"
            css += f"  --color-{name}-rgb: {color['rgb']};\n"
        
        css += "\n"
        
        # Spacing
        for name, value in tokens.get('spacing', {}).items():
            css += f"  --spacing-{name}: {value};\n"
        
        css += "\n"
        
        # Typography
        if 'typography' in tokens:
            for name, props in tokens['typography'].items():
                css += f"  --font-{name}: {props.get('family', 'sans-serif')};\n"
                css += f"  --font-size-{name}: {props.get('size', '16px')};\n"
                css += f"  --font-weight-{name}: {props.get('weight', '400')};\n"
        
        css += "}\n\n"
        
        # Add example usage comments
        css += "/* Example usage:\n"
        css += " * color: var(--color-primary);\n"
        css += " * padding: var(--spacing-medium);\n"
        css += " * font-family: var(--font-body);\n"
        css += " */\n"
        
        return css
    
    def generate_tailwind_config(self, tokens):
        """Generate Tailwind configuration"""
        config = {
            "theme": {
                "extend": {
                    "colors": {},
                    "spacing": {},
                    "fontFamily": {},
                    "fontSize": {},
                    "fontWeight": {}
                }
            }
        }
        
        # Add colors
        for name, color in tokens.get('colors', {}).items():
            config["theme"]["extend"]["colors"][name] = color['hex']
        
        # Add spacing
        for name, value in tokens.get('spacing', {}).items():
            config["theme"]["extend"]["spacing"][name] = value
        
        # Add typography
        if 'typography' in tokens:
            for name, props in tokens['typography'].items():
                if 'family' in props:
                    config["theme"]["extend"]["fontFamily"][name] = props['family']
                if 'size' in props:
                    config["theme"]["extend"]["fontSize"][name] = props['size']
                if 'weight' in props:
                    config["theme"]["extend"]["fontWeight"][name] = props['weight']
        
        # Format as JavaScript module
        output = "/** @type {import('tailwindcss').Config} */\n"
        output += f"module.exports = {json.dumps(config, indent=2)}"
        
        return output
    
    def generate_json_tokens(self, tokens):
        """Generate W3C Design Token Community Group format"""
        formatted_tokens = {
            "$schema": "https://design-tokens.github.io/community-group/format.json",
            "tokens": {}
        }
        
        # Colors
        if 'colors' in tokens:
            formatted_tokens["tokens"]["color"] = {}
            for name, color in tokens['colors'].items():
                formatted_tokens["tokens"]["color"][name] = {
                    "$value": color['hex'],
                    "$type": "color",
                    "$description": f"Color {name} - {color.get('proportion', 0)*100:.1f}% of design"
                }
        
        # Spacing
        if 'spacing' in tokens:
            formatted_tokens["tokens"]["spacing"] = {}
            for name, value in tokens['spacing'].items():
                formatted_tokens["tokens"]["spacing"][name] = {
                    "$value": value,
                    "$type": "dimension"
                }
        
        # Typography
        if 'typography' in tokens:
            formatted_tokens["tokens"]["typography"] = {}
            for name, props in tokens['typography'].items():
                formatted_tokens["tokens"]["typography"][name] = {
                    "fontFamily": {
                        "$value": props.get('family', 'sans-serif'),
                        "$type": "fontFamily"
                    },
                    "fontSize": {
                        "$value": props.get('size', '16px'),
                        "$type": "dimension"
                    },
                    "fontWeight": {
                        "$value": props.get('weight', '400'),
                        "$type": "fontWeight"
                    }
                }
        
        return json.dumps(formatted_tokens, indent=2)
    
    def generate_style_dictionary(self, tokens):
        """Generate Style Dictionary format tokens"""
        sd_tokens = {
            "color": {},
            "spacing": {},
            "typography": {}
        }
        
        # Transform colors
        for name, color in tokens.get('colors', {}).items():
            sd_tokens["color"][name] = {
                "value": color['hex'],
                "type": "color",
                "attributes": {
                    "rgb": color.get('rgb', ''),
                    "proportion": color.get('proportion', 0)
                }
            }
        
        # Transform spacing
        for name, value in tokens.get('spacing', {}).items():
            sd_tokens["spacing"][name] = {
                "value": value,
                "type": "spacing"
            }
        
        # Transform typography
        if 'typography' in tokens:
            for name, props in tokens['typography'].items():
                sd_tokens["typography"][name] = {
                    "fontFamily": {
                        "value": props.get('family', 'sans-serif')
                    },
                    "fontSize": {
                        "value": props.get('size', '16px')
                    },
                    "fontWeight": {
                        "value": props.get('weight', '400')
                    }
                }
        
        return json.dumps(sd_tokens, indent=2)
    
    def generate_scss_variables(self, tokens):
        """Generate SCSS variables"""
        scss = "// Design Tokens - SCSS Variables\n\n"
        
        # Colors
        scss += "// Colors\n"
        for name, color in tokens.get('colors', {}).items():
            scss += f"$color-{name}: {color['hex']};\n"
        
        scss += "\n// Spacing\n"
        for name, value in tokens.get('spacing', {}).items():
            scss += f"$spacing-{name}: {value};\n"
        
        scss += "\n// Typography\n"
        if 'typography' in tokens:
            for name, props in tokens['typography'].items():
                scss += f"$font-{name}: {props.get('family', 'sans-serif')};\n"
                scss += f"$font-size-{name}: {props.get('size', '16px')};\n"
                scss += f"$font-weight-{name}: {props.get('weight', '400')};\n"
                scss += "\n"
        
        # Add mixins for common patterns
        scss += "\n// Utility Mixins\n"
        scss += "@mixin text-style($style) {\n"
        scss += "  @if $style == 'heading' {\n"
        scss += "    font-family: $font-heading;\n"
        scss += "    font-size: $font-size-heading;\n"
        scss += "    font-weight: $font-weight-heading;\n"
        scss += "  } @else if $style == 'body' {\n"
        scss += "    font-family: $font-body;\n"
        scss += "    font-size: $font-size-body;\n"
        scss += "    font-weight: $font-weight-body;\n"
        scss += "  }\n"
        scss += "}\n"
        
        return scss