@@ -3,7 +3,7 @@ use std::borrow::Cow;
33use crate :: { app_config:: AppConfig , utils:: static_filename} ;
44use anyhow:: Context as _;
55use handlebars:: {
6- handlebars_helper, Context , Handlebars , PathAndJson , RenderError , RenderErrorReason ,
6+ handlebars_helper, Context , Handlebars , HelperDef , PathAndJson , RenderError , RenderErrorReason ,
77 Renderable , ScopedJson ,
88} ;
99use serde_json:: Value as JsonValue ;
@@ -40,10 +40,10 @@ pub fn register_all_helpers(h: &mut Handlebars<'_>, config: &AppConfig) {
4040 h. register_helper ( "array_contains" , Box :: new ( array_contains) ) ;
4141
4242 // 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 ( ) ) ) ;
4444
4545 // 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 ) ) ) ;
4747 register_helper ( h, "markdown" , markdown_helper as EH ) ;
4848 register_helper ( h, "buildinfo" , buildinfo_helper as EH ) ;
4949 register_helper ( h, "typeof" , typeof_helper as H ) ;
@@ -131,6 +131,7 @@ fn to_array_helper(v: &JsonValue) -> JsonValue {
131131 . into ( )
132132}
133133
134+ /// Generate the full path to a builtin sqlpage asset. Struct Param is the site prefix
134135struct StaticPathHelper ( String ) ;
135136
136137impl CanHelp for StaticPathHelper {
@@ -153,6 +154,37 @@ impl CanHelp for StaticPathHelper {
153154 }
154155}
155156
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+
156188fn typeof_helper ( v : & JsonValue ) -> JsonValue {
157189 match v {
158190 JsonValue :: Null => "null" ,
@@ -297,31 +329,6 @@ fn sum_helper<'reg, 'rc>(
297329 Ok ( ( ) )
298330}
299331
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-
325332trait CanHelp : Send + Sync + ' static {
326333 fn call ( & self , v : & [ PathAndJson ] ) -> Result < JsonValue , String > ;
327334}
0 commit comments