@@ -55,6 +55,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
55
55
keep_link_tags : bool ,
56
56
base_url : Option < String > ,
57
57
extra_css : Option < String > ,
58
+ extra_css_files : Vec < String > ,
58
59
output_filename_prefix : Option < OsString > ,
59
60
load_remote_stylesheets : bool ,
60
61
#[ cfg( feature = "stylesheet-cache" ) ]
@@ -72,6 +73,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
72
73
keep_link_tags : false ,
73
74
base_url : None ,
74
75
extra_css : None ,
76
+ extra_css_files : Vec :: new ( ) ,
75
77
output_filename_prefix : None ,
76
78
load_remote_stylesheets : false ,
77
79
#[ cfg( feature = "stylesheet-cache" ) ]
@@ -101,6 +103,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
101
103
"inline-style-tags"
102
104
| "base-url"
103
105
| "extra-css"
106
+ | "extra-css-file"
104
107
| "output-filename-prefix"
105
108
| if_cfg_feature_stylesheet_cache!( "cache-size" )
106
109
)
@@ -125,6 +128,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
125
128
"inline-style-tags" => parsed. inline_style_tags = parse_value ( value, flag) ?,
126
129
"base-url" => parsed. base_url = Some ( value. to_string ( ) ) ,
127
130
"extra-css" => parsed. extra_css = Some ( value. to_string ( ) ) ,
131
+ "extra-css-file" => parsed. extra_css_files . push ( value. to_string ( ) ) ,
128
132
"output-filename-prefix" => {
129
133
parsed. output_filename_prefix = Some ( value. to_string ( ) . into ( ) ) ;
130
134
}
@@ -155,6 +159,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
155
159
Ok ( ( ) )
156
160
}
157
161
162
+ fn combine_extra_css (
163
+ extra_css : Option < String > ,
164
+ extra_css_files : Vec < String > ,
165
+ ) -> Result < Option < String > , Box < dyn std:: error:: Error > > {
166
+ let mut buffer = extra_css. unwrap_or_default ( ) ;
167
+
168
+ if !buffer. is_empty ( ) {
169
+ buffer. push ( '\n' ) ;
170
+ }
171
+
172
+ for path in extra_css_files {
173
+ let mut file =
174
+ File :: open ( & path) . map_err ( |e| format ! ( "Failed to read CSS file '{path}': {e}" ) ) ?;
175
+ file. read_to_string ( & mut buffer) ?;
176
+ if !buffer. is_empty ( ) {
177
+ buffer. push ( '\n' ) ;
178
+ }
179
+ }
180
+
181
+ Ok ( if buffer. is_empty ( ) {
182
+ None
183
+ } else {
184
+ Some ( buffer)
185
+ } )
186
+ }
187
+
158
188
fn format_error ( filename : Option < & str > , error : impl fmt:: Display ) {
159
189
let mut buffer = String :: with_capacity ( 128 ) ;
160
190
if let Some ( filename) = filename {
@@ -212,6 +242,10 @@ OPTIONS:
212
242
--extra-css
213
243
Additional CSS to inline.
214
244
245
+ --extra-css-file <PATH>
246
+ Load additional CSS from a file to inline. Can be used multiple times to load
247
+ from several files. The CSS will be processed alongside any existing styles.
248
+
215
249
--output-filename-prefix
216
250
Custom prefix for output files. Defaults to `inlined.`.
217
251
"#
@@ -280,6 +314,13 @@ OPTIONS:
280
314
} else {
281
315
None
282
316
} ;
317
+ let extra_css = match combine_extra_css ( args. extra_css , args. extra_css_files ) {
318
+ Ok ( css) => css,
319
+ Err ( error) => {
320
+ format_error ( None , error) ;
321
+ std:: process:: exit ( 1 ) ;
322
+ }
323
+ } ;
283
324
let options = InlineOptions {
284
325
inline_style_tags : args. inline_style_tags ,
285
326
keep_style_tags : args. keep_style_tags ,
@@ -288,7 +329,7 @@ OPTIONS:
288
329
load_remote_stylesheets : args. load_remote_stylesheets ,
289
330
#[ cfg( feature = "stylesheet-cache" ) ]
290
331
cache,
291
- extra_css : args . extra_css . as_deref ( ) . map ( Cow :: Borrowed ) ,
332
+ extra_css : extra_css. as_deref ( ) . map ( Cow :: Borrowed ) ,
292
333
preallocate_node_capacity : 32 ,
293
334
resolver : Arc :: new ( DefaultStylesheetResolver ) ,
294
335
} ;
0 commit comments