@@ -75,90 +75,103 @@ const coverPropertyConversions: PropertyConversions = {
75
75
width : "--page-margin-right" ,
76
76
} ;
77
77
78
- const cssFileContent : string = fs . readFileSync ( "./zebra.css" , "utf8" ) ;
79
- const cssObject : Stylesheet = parse ( cssFileContent ) ;
80
-
81
- if ( cssObject . stylesheet && cssObject . stylesheet . rules ) {
82
- cssObject . stylesheet . rules . forEach ( ( rule : Rule ) => {
83
- if (
84
- rule . type === "rule" &&
85
- rule . selectors &&
86
- rule . selectors . some ( ( s : string ) => s . includes ( ".marginBox" ) )
87
- ) {
88
- // remove the .marginBox class from the selectors
89
- rule . selectors = rule . selectors . map ( ( s : string ) =>
90
- s . replace ( ".marginBox" , "" )
91
- ) ;
92
-
93
- // find the sizes element that has the same name as one of the selectors
94
- const size = sizes . find ( ( sz ) =>
95
- rule . selectors ! . some ( ( sel ) => sel . includes ( sz . name ) )
96
- ) ;
97
-
98
- var x = rule . declarations ?. find (
99
- ( d : Declaration ) => d . property === "left"
100
- ) as Declaration ;
101
-
102
- var left = Number . parseFloat (
103
- (
78
+ export function migrateCssToAppearance ( cssFileContent : string ) : string {
79
+ var cssObject : Stylesheet ;
80
+ try {
81
+ cssObject = parse ( cssFileContent ) ;
82
+ } catch ( e ) {
83
+ console . log ( "Error parsing css: " + e ) ;
84
+ return cssFileContent ;
85
+ }
86
+
87
+ if ( cssObject . stylesheet && cssObject . stylesheet . rules ) {
88
+ cssObject . stylesheet . rules . forEach ( ( rule : Rule ) => {
89
+ if (
90
+ rule . type === "rule" &&
91
+ rule . selectors &&
92
+ rule . selectors . some ( ( s : string ) => s . includes ( ".marginBox" ) )
93
+ ) {
94
+ // THE Following removal, LIKE A LOT OF THIS FILE, IS GARBAGE. A RULE LIKE
95
+ // .marginBox { background-color: red; } is just fine.
96
+
97
+ // remove the .marginBox class from the selectors
98
+ rule . selectors = rule . selectors . map ( ( s : string ) =>
99
+ s . replace ( ".marginBox" , "" )
100
+ ) ;
101
+
102
+ // find the sizes element that has the same name as one of the selectors
103
+ const size = sizes . find ( ( sz ) =>
104
+ rule . selectors ! . some ( ( sel ) => sel . includes ( sz . name ) )
105
+ ) ;
106
+
107
+ var x = rule . declarations ?. find (
108
+ ( d : Declaration ) => d . property === "left"
109
+ ) as Declaration ;
110
+
111
+ const l = (
104
112
rule . declarations ?. find (
105
113
( d : Declaration ) => d . property === "left"
106
114
) as Declaration
107
- ) . value !
108
- ) ;
109
- var top = Number . parseFloat (
110
- (
115
+ ) ?. value ! ;
116
+
117
+ const t = (
111
118
rule . declarations ?. find (
112
119
( d : Declaration ) => d . property === "top"
113
120
) as Declaration
114
- ) . value !
115
- ) ;
116
- rule . declarations ?. forEach ( ( declaration : Declaration ) => {
117
- if ( declaration . type === "declaration" ) {
118
- const key = ( declaration as Declaration ) . property ;
119
-
120
- if ( size ) {
121
- if ( key === "width" ) {
122
- declaration . value =
123
- size . width -
124
- Number . parseFloat ( declaration . value ! ) -
125
- left +
126
- "mm" ;
121
+ ) ?. value ! ;
122
+
123
+ if ( ! l || ! t ) return ; // todo log it?
124
+
125
+ var left = parseFloatOrUndefined ( l ) ;
126
+ var top = parseFloatOrUndefined ( t ) ;
127
+ rule . declarations ?. forEach ( ( declaration : Declaration ) => {
128
+ if ( declaration . type === "declaration" ) {
129
+ const key = ( declaration as Declaration ) . property ;
130
+
131
+ if ( size ) {
132
+ const v = parseFloatOrUndefined ( declaration . value ! ) ;
133
+ if ( v === undefined || left === undefined || top === undefined )
134
+ declaration . value = ` ignore /* error: ${ rule . declarations ?. toString ( ) } */` ;
135
+ else {
136
+ if ( key === "width" ) {
137
+ declaration . value = size . width - v - left + "mm" ;
138
+ }
139
+ if ( key === "height" ) {
140
+ declaration . value = size . height - v - top + "mm" ;
141
+ }
142
+ }
127
143
}
128
- if ( key === "height" ) {
129
- declaration . value =
130
- size . height -
131
- Number . parseFloat ( declaration . value ! ) -
132
- top +
133
- "mm" ;
144
+
145
+ const isCover = rule . selectors ! . some ( ( sel ) =>
146
+ sel . includes ( "Cover" )
147
+ ) ;
148
+ const map = isCover
149
+ ? coverPropertyConversions
150
+ : propertyConversions ;
151
+ if ( declaration . property ! in map ) {
152
+ declaration . property = map [ declaration . property ! ] ;
153
+ declaration . value = declaration . value ?. replace ( "!important" , "" ) ;
134
154
}
135
155
}
156
+ } ) ;
136
157
137
- const isCover = rule . selectors ! . some ( ( sel ) => sel . includes ( "Cover" ) ) ;
138
- const map = isCover ? coverPropertyConversions : propertyConversions ;
139
- if ( declaration . property ! in map ) {
140
- declaration . property = map [ declaration . property ! ] ;
141
- declaration . value = declaration . value ?. replace ( "!important" , "" ) ;
142
- }
143
- }
144
- } ) ;
158
+ // danger, this would probably break if there is anything but classes in the selector
159
+ sortClassesInSelector ( rule ) ;
145
160
146
- // danger, this would probably break if there is anything but classes in the selector
147
- sortClassesInSelector ( rule ) ;
161
+ // TODO: this doesn't yet move top and bottom margins with .outsideFrontCover and .outsideBackCover to --cover-margin-top and --cover-margin-bottom
148
162
149
- // TODO: this doesn't yet move top and bottom margins with .outsideFrontCover and .outsideBackCover to --cover-margin-top and --cover-margin-bottom
163
+ sortDeclarations ( rule ) ;
164
+ }
165
+ } ) ;
150
166
151
- sortDeclarations ( rule ) ;
152
- }
153
- } ) ;
167
+ // danger, normally sorting rules is not a good idea!
168
+ cssObject . stylesheet . rules = sortRules ( cssObject . stylesheet . rules ) ;
169
+ }
154
170
155
- // danger, normally sorting rules is not a good idea!
156
- cssObject . stylesheet . rules = sortRules ( cssObject . stylesheet . rules ) ;
171
+ const modifiedCss : string = stringify ( cssObject ) ;
172
+ return modifiedCss ;
157
173
}
158
174
159
- const modifiedCss : string = stringify ( cssObject ) ;
160
- console . log ( modifiedCss ) ;
161
-
162
175
function sortDeclarations ( rule : Rule ) {
163
176
// Define the type of propertyConversions
164
177
type PropertyConversions = {
@@ -215,3 +228,12 @@ function sortRules(rules: Rule[]): Rule[] {
215
228
}
216
229
} ) ;
217
230
}
231
+
232
+ function parseFloatOrUndefined ( value : string ) : number | undefined {
233
+ try {
234
+ const result = parseFloat ( value ) ;
235
+ return isNaN ( result ) ? undefined : result ;
236
+ } catch ( e ) {
237
+ return undefined ;
238
+ }
239
+ }
0 commit comments