@@ -3,7 +3,7 @@ use std::borrow::Cow;
3
3
use crate :: { app_config:: AppConfig , utils:: static_filename} ;
4
4
use anyhow:: Context as _;
5
5
use handlebars:: {
6
- handlebars_helper, Context , Handlebars , PathAndJson , RenderError , RenderErrorReason ,
6
+ handlebars_helper, Context , Handlebars , HelperDef , PathAndJson , RenderError , RenderErrorReason ,
7
7
Renderable , ScopedJson ,
8
8
} ;
9
9
use serde_json:: Value as JsonValue ;
@@ -40,10 +40,10 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
40
40
h. register_helper ( "array_contains" , Box :: new ( array_contains) ) ;
41
41
42
42
// static_path helper: generate a path to a static file. Replaces sqpage.js by sqlpage.<hash>.js
43
- register_helper ( h, "static_path" , StaticPathHelper ( site_prefix) ) ;
43
+ register_helper ( h, "static_path" , StaticPathHelper ( site_prefix. clone ( ) ) ) ;
44
44
45
45
// icon helper: generate an image with the specified icon
46
- h. register_helper ( "icon_img" , Box :: new ( icon_img_helper ) ) ;
46
+ h. register_helper ( "icon_img" , Box :: new ( IconImgHelper ( site_prefix ) ) ) ;
47
47
register_helper ( h, "markdown" , markdown_helper as EH ) ;
48
48
register_helper ( h, "buildinfo" , buildinfo_helper as EH ) ;
49
49
register_helper ( h, "typeof" , typeof_helper as H ) ;
@@ -131,6 +131,7 @@ fn to_array_helper(v: &JsonValue) -> JsonValue {
131
131
. into ( )
132
132
}
133
133
134
+ /// Generate the full path to a builtin sqlpage asset. Struct Param is the site prefix
134
135
struct StaticPathHelper ( String ) ;
135
136
136
137
impl CanHelp for StaticPathHelper {
@@ -153,6 +154,37 @@ impl CanHelp for StaticPathHelper {
153
154
}
154
155
}
155
156
157
+ /// Generate an image with the specified icon. Struct Param is the site prefix
158
+ struct IconImgHelper ( String ) ;
159
+ impl HelperDef for IconImgHelper {
160
+ fn call < ' reg : ' rc , ' rc > (
161
+ & self ,
162
+ helper : & handlebars:: Helper < ' rc > ,
163
+ _r : & ' reg Handlebars < ' reg > ,
164
+ _ctx : & ' rc Context ,
165
+ _rc : & mut handlebars:: RenderContext < ' reg , ' rc > ,
166
+ writer : & mut dyn handlebars:: Output ,
167
+ ) -> handlebars:: HelperResult {
168
+ let null = handlebars:: JsonValue :: Null ;
169
+ let params = [ 0 , 1 ] . map ( |i| helper. params ( ) . get ( i) . map_or ( & null, PathAndJson :: value) ) ;
170
+ let name = match params[ 0 ] {
171
+ JsonValue :: String ( s) => s,
172
+ other => {
173
+ log:: debug!( "icon_img: {other:?} is not an icon name, not rendering anything" ) ;
174
+ return Ok ( ( ) ) ;
175
+ }
176
+ } ;
177
+ let size = params[ 1 ] . as_u64 ( ) . unwrap_or ( 24 ) ;
178
+ write ! (
179
+ writer,
180
+ "<svg width={size} height={size}><use href=\" {}{}#tabler-{name}\" /></svg>" ,
181
+ self . 0 ,
182
+ static_filename!( "tabler-icons.svg" )
183
+ ) ?;
184
+ Ok ( ( ) )
185
+ }
186
+ }
187
+
156
188
fn typeof_helper ( v : & JsonValue ) -> JsonValue {
157
189
match v {
158
190
JsonValue :: Null => "null" ,
@@ -297,31 +329,6 @@ fn sum_helper<'reg, 'rc>(
297
329
Ok ( ( ) )
298
330
}
299
331
300
- fn icon_img_helper < ' reg , ' rc > (
301
- helper : & handlebars:: Helper < ' rc > ,
302
- _r : & ' reg Handlebars < ' reg > ,
303
- _ctx : & ' rc Context ,
304
- _rc : & mut handlebars:: RenderContext < ' reg , ' rc > ,
305
- writer : & mut dyn handlebars:: Output ,
306
- ) -> handlebars:: HelperResult {
307
- let null = handlebars:: JsonValue :: Null ;
308
- let params = [ 0 , 1 ] . map ( |i| helper. params ( ) . get ( i) . map_or ( & null, PathAndJson :: value) ) ;
309
- let name = match params[ 0 ] {
310
- JsonValue :: String ( s) => s,
311
- other => {
312
- log:: debug!( "icon_img: {other:?} is not an icon name, not rendering anything" ) ;
313
- return Ok ( ( ) ) ;
314
- }
315
- } ;
316
- let size = params[ 1 ] . as_u64 ( ) . unwrap_or ( 24 ) ;
317
- write ! (
318
- writer,
319
- "<svg width={size} height={size}><use href=\" /{}#tabler-{name}\" /></svg>" ,
320
- static_filename!( "tabler-icons.svg" )
321
- ) ?;
322
- Ok ( ( ) )
323
- }
324
-
325
332
trait CanHelp : Send + Sync + ' static {
326
333
fn call ( & self , v : & [ PathAndJson ] ) -> Result < JsonValue , String > ;
327
334
}
0 commit comments