Skip to content

Commit ad8cae6

Browse files
committed
feat: allow shortcuts
1 parent f1cfc29 commit ad8cae6

8 files changed

Lines changed: 119 additions & 23 deletions

File tree

.styler

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.0.3",
2+
"version": "0.0.4",
33
"pattern": "*.R|*.r|*.js|*.html",
44
"directory": "test",
55
"output": "test/style.min.css",
@@ -8,7 +8,7 @@
88
"media": [
99
{
1010
"maxWidth": "",
11-
"minWidth": "640px",
11+
"minWidth": "576px",
1212
"name": "sm"
1313
},
1414
{
@@ -18,13 +18,18 @@
1818
},
1919
{
2020
"maxWidth": "",
21-
"minWidth": "1024px",
21+
"minWidth": "992px",
2222
"name": "lg"
2323
},
2424
{
2525
"maxWidth": "",
26-
"minWidth": "1280px",
26+
"minWidth": "1200px",
2727
"name": "xl"
28+
},
29+
{
30+
"maxWidth": "",
31+
"minWidth": "1400px",
32+
"name": "xxl"
2833
}
2934
],
3035
"colors": {
@@ -314,5 +319,17 @@
314319
"900": "#18181b",
315320
"950": "#09090b"
316321
}
322+
},
323+
"shortcuts": {
324+
"b": "bottom",
325+
"h": "height",
326+
"l": "left",
327+
"m": "margin",
328+
"p": "padding",
329+
"r": "right",
330+
"t": "top",
331+
"w": "width",
332+
"x": "inline",
333+
"y": "block"
317334
}
318335
}

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ The configuration allows you to customize:
6262
- **Divider**: Number to divide numeric values by (default: `4`)
6363
- **Media Queries**: Custom breakpoint definitions
6464
- **Colors**: Custom color palette definitions
65+
- **Shortcuts**: Custom shorthands for common CSS properties
6566

6667
<details>
6768
<summary>Default Configuration</summary>
6869

6970
```json
7071
{
71-
"version": "0.0.3",
72+
"version": "0.0.4",
7273
"pattern": "*.R|*.r|*.js|*.html",
7374
"directory": ".",
7475
"output": "style.min.css",
@@ -383,6 +384,16 @@ The configuration allows you to customize:
383384
"900": "#18181b",
384385
"950": "#09090b"
385386
}
387+
},
388+
"shortcuts": {
389+
"b": "bottom",
390+
"h": "height",
391+
"l": "left",
392+
"m": "margin",
393+
"p": "padding",
394+
"r": "right",
395+
"t": "top",
396+
"w": "width"
386397
}
387398
}
388399
```

cmd/class.go

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"regexp"
6+
"slices"
67
"strconv"
78
"strings"
89
)
@@ -39,19 +40,17 @@ func (c *Command) checkProperty(str string) bool {
3940
return false
4041
}
4142

42-
for _, p := range c.Properties {
43-
if p == property[0] {
44-
return true
45-
}
46-
}
47-
return false
43+
return slices.Contains(c.Properties, property[0])
4844
}
4945

5046
func (c *Command) makeProperty(str string) string {
5147
// remove media and prefix from attribute
5248
str = mediaRegex.ReplaceAllString(str, "")
5349
str = prefixRegex.ReplaceAllString(str, "")
5450

51+
// Expand shortcuts BEFORE any other processing
52+
str = c.expandShortcuts(str)
53+
5554
// First check for color patterns like "color-red-400"
5655
colorStr, isColor := c.makeColor(str)
5756
if isColor {
@@ -85,17 +84,17 @@ func (c *Command) makeProperty(str string) string {
8584

8685
// Join parts before the property end to form the property name
8786
property := strings.Join(parts[:propertyEndIdx], "-")
88-
87+
8988
// Process each value part individually
9089
valueParts := parts[propertyEndIdx:]
9190
processedValues := make([]string, len(valueParts))
92-
91+
9392
for i, part := range valueParts {
9493
// Check if this part contains a ~ for strict value
9594
if strings.Contains(part, "~") {
9695
// Split by ~ and take the strict value
9796
strictParts := strings.Split(part, "~")
98-
97+
9998
// First part could be empty if ~ is at the beginning
10099
if strictParts[0] != "" {
101100
// First part is not strict, process normally
@@ -109,7 +108,7 @@ func (c *Command) makeProperty(str string) string {
109108
processedValues[i] = strictParts[0]
110109
}
111110
}
112-
111+
113112
// Second part is strict, don't apply divider or unit
114113
if len(strictParts) > 1 {
115114
if processedValues[i] != "" {
@@ -131,7 +130,7 @@ func (c *Command) makeProperty(str string) string {
131130
}
132131
}
133132
}
134-
133+
135134
// Join the processed values with spaces
136135
return property + ":" + strings.Join(processedValues, " ")
137136
}
@@ -157,6 +156,60 @@ func (c *Command) makeColor(str string) (string, bool) {
157156
return str, false
158157
}
159158

159+
func (c *Command) expandShortcuts(str string) string {
160+
if len(c.Config.Shortcuts) == 0 {
161+
return str
162+
}
163+
164+
return c.expandRegularShortcuts(str)
165+
}
166+
167+
func (c *Command) expandRegularShortcuts(str string) string {
168+
parts := strings.Split(str, "-")
169+
if len(parts) <= 1 {
170+
return str
171+
}
172+
173+
// Expand shortcuts in property parts (before values)
174+
expanded := make([]string, len(parts))
175+
copy(expanded, parts)
176+
177+
for i, part := range parts {
178+
// Stop expanding when we hit a numeric value or potential color
179+
if regexp.MustCompile(`\d`).MatchString(part) || c.isColorPart(i, parts) {
180+
break
181+
}
182+
183+
if replacement, exists := c.Config.Shortcuts[part]; exists {
184+
expanded[i] = replacement
185+
}
186+
}
187+
188+
return strings.Join(expanded, "-")
189+
}
190+
191+
func (c *Command) isColorPart(index int, parts []string) bool {
192+
// Check if this might be part of a color pattern
193+
// We need at least 2 more parts after current index for color-shade pattern
194+
if index >= len(parts)-2 {
195+
return false
196+
}
197+
198+
// Look ahead to see if we have a color-shade pattern
199+
// For example: "color-red-400" where current part is "color", next is "red", and after that is "400"
200+
if index <= len(parts)-3 {
201+
colorName := parts[index+1]
202+
shadeName := parts[index+2]
203+
if colorMap, exists := c.Config.Colors[colorName]; exists {
204+
if _, exists := colorMap[shadeName]; exists {
205+
return true
206+
}
207+
}
208+
}
209+
210+
return false
211+
}
212+
160213
func classType(str string) string {
161214
if strings.Contains(str, ":") {
162215
return "prefix"

cmd/properties.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

options/config.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Config struct {
2525
Divider int `json:"divider"`
2626
Media []Media `json:"media"`
2727
Colors map[string]map[string]string `json:"colors"`
28+
Shortcuts map[string]string `json:"shortcuts"`
2829
}
2930

3031
var colors = map[string]map[string]string{
@@ -316,6 +317,19 @@ var colors = map[string]map[string]string{
316317
},
317318
}
318319

320+
var shortcuts = map[string]string{
321+
"m": "margin",
322+
"p": "padding",
323+
"w": "width",
324+
"h": "height",
325+
"t": "top",
326+
"r": "right",
327+
"b": "bottom",
328+
"l": "left",
329+
"d": "display",
330+
"f": "flex",
331+
}
332+
319333
func New() Config {
320334
return Config{
321335
Version: Version,
@@ -346,7 +360,8 @@ func New() Config {
346360
Name: "xxl",
347361
},
348362
},
349-
Colors: colors,
363+
Colors: colors,
364+
Shortcuts: shortcuts,
350365
}
351366
}
352367

properties.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

styler

3.36 MB
Binary file not shown.

test/test.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ ui <- fluidPage(
66
class = "display-flex padding-2 margin-bottom-2 width-40",
77
div(
88
class = "flex-grow~1 box-shadow-1-1-1~2-grey",
9-
h1("Hello, world!", class = "color-red-400 hover:color-cyan-500")
9+
h1("Hello, world!", class = "color-red-400 hover:color-cyan-500"),
10+
h3("Must have loads of padding", class = "p-t-50")
1011
),
1112
div(
12-
class = "flex-shrink~1 md@display-none",
13+
class = "flex-shrink~1 sm@display-none",
1314
h2(
1415
"This is hidden on medium screens and larger hello-world",
1516
class = "color-blue hover:color-green"
@@ -18,7 +19,6 @@ ui <- fluidPage(
1819
)
1920
)
2021

21-
server <- \(input, output, session) {
22-
}
22+
server <- \(input, output, session) {}
2323

2424
shinyApp(ui, server)

0 commit comments

Comments
 (0)