Skip to content

Commit b5709b8

Browse files
committed
feat: allow strict values + fix read pattern
1 parent 261e053 commit b5709b8

13 files changed

Lines changed: 438 additions & 145 deletions

File tree

.styler

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": "0.0.2",
3-
"pattern": "*.r|*.js|*.html",
3+
"pattern": "*.R|*.r|*.js|*.html",
44
"directory": "test",
55
"output": "test/style.min.css",
66
"unit": "rem",

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ Call `styler` to generate the CSS.
4646
styler
4747
```
4848

49-
- Media queries are suffixed with `@` and prefixed with `md@`, `lg@` etc.
50-
- States are suffixed with `:` and prefixed with `hover:`, `active:` etc.
49+
- Media queries are suffixed with `@` e.g.: `md@`, `lg@`
50+
- States are suffixed with `:`, e.g.: `hover:`, `active:`
5151
- Numeric values are set as `unit` specified in the config (defaults to `rem`)
5252
and are divided by the `divider` specified in the config (defaults to `4`),
5353
e.g.: `padding-top-2` will result in `padding-top: 0.5rem`
54+
- Numerics precded by `~` are treated as strict and are not divided or suffixed with `unit`
55+
e.g.: `flex~1` results in `flex: 1`
5456
- Media queries can be edited in `.styler` config file
5557
- Colors can be edited in `.styler` config file
5658
- Extracted CSS properties are checked against the [W3C CSS Properties](https://www.w3.org/Style/CSS/all-properties.en.html) table

cmd/class.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"regexp"
6+
"strconv"
67
"strings"
78
)
89

@@ -51,25 +52,38 @@ func (c *Command) makeProperty(str string) string {
5152
str = mediaRegex.ReplaceAllString(str, "")
5253
str = prefixRegex.ReplaceAllString(str, "")
5354

55+
strict := false
5456
last := strings.LastIndex(str, "-")
57+
if strings.Contains(str, "~") {
58+
strict = true
59+
last = strings.LastIndex(str, "~")
60+
}
5561

5662
if last == -1 {
5763
return str
5864
}
5965

6066
value := str[last+1:]
6167

62-
var intValue int
63-
_, err := fmt.Sscanf(value, "%d", &intValue)
68+
intValue, err := strconv.Atoi(value)
6469

6570
if err == nil {
6671
// it ends in a number it may be a color, e.g.: color-red-400
6772
str, ok := c.makeColor(str)
6873
if ok {
6974
return str
7075
}
71-
val := float32(intValue) / float32(c.Config.Divider)
72-
return str[:last] + ":" + fmt.Sprintf("%v", val) + c.Config.Unit
76+
val := float32(intValue)
77+
if !strict {
78+
val = float32(intValue) / float32(c.Config.Divider)
79+
}
80+
81+
property := str[:last] + ":" + fmt.Sprintf("%v", val)
82+
83+
if strict {
84+
return property
85+
}
86+
return property + c.Config.Unit
7387
}
7488

7589
return str[:last] + ":" + value
@@ -117,7 +131,7 @@ func (c *Command) makeMediaClass(str string) {
117131
_, ok := c.MediaMaps[strs[0]]
118132

119133
if !ok {
120-
fmt.Printf("%v media not found in .styler", strs[0])
134+
fmt.Printf("%v media not found in .styler\n", strs[0])
121135
return
122136
}
123137

@@ -131,26 +145,9 @@ func (c *Command) makeMediaClass(str string) {
131145
}
132146

133147
func (c *Command) makeClassName(str string) string {
134-
t := classType(str)
135-
136-
switch t {
137-
case "prefix":
138-
return c.makeClassNamePrefix(str)
139-
case "media":
140-
return c.makeClassNameMedia(str)
141-
default:
142-
return str
143-
}
144-
}
145-
146-
func (c *Command) makeClassNameMedia(str string) string {
147-
return strings.ReplaceAll(str, "@", "\\@")
148-
}
149-
150-
func (c *Command) makeClassNamePrefix(str string) string {
151-
strs := strings.Split(str, ":")
152-
if len(strs) < 2 {
153-
return str
154-
}
155-
return strs[0] + "\\:" + strs[1] + ":" + strs[0]
148+
str = strings.ReplaceAll(str, "@", "\\@")
149+
str = strings.ReplaceAll(str, "%", "\\%")
150+
str = strings.ReplaceAll(str, ":", "\\:")
151+
str = strings.ReplaceAll(str, "~", "\\~")
152+
return str
156153
}

cmd/properties.json

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

cmd/read.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"strings"
78
)
89

910
func (c *Command) read() error {
@@ -16,6 +17,9 @@ func (c *Command) read() error {
1617
return fmt.Errorf("%s is not a directory", c.Config.Directory)
1718
}
1819

20+
// Split the pattern string by the pipe character to support multiple patterns
21+
patterns := strings.Split(c.Config.Pattern, "|")
22+
1923
return filepath.Walk(c.Config.Directory, func(path string, info os.FileInfo, err error) error {
2024
if err != nil {
2125
return fmt.Errorf("error accessing path %s: %w", path, err)
@@ -25,6 +29,26 @@ func (c *Command) read() error {
2529
return nil
2630
}
2731

32+
// Check if the file matches any of the patterns
33+
fileName := filepath.Base(path)
34+
matched := false
35+
36+
for _, pattern := range patterns {
37+
patternMatched, err := filepath.Match(pattern, fileName)
38+
if err != nil {
39+
return fmt.Errorf("error matching pattern %s for file %s: %w", pattern, path, err)
40+
}
41+
if patternMatched {
42+
matched = true
43+
break
44+
}
45+
}
46+
47+
// Skip files that don't match any pattern
48+
if !matched {
49+
return nil
50+
}
51+
2852
content, err := os.ReadFile(path)
2953
if err != nil {
3054
return fmt.Errorf("error reading file %s: %w", path, err)

0 commit comments

Comments
 (0)