@@ -4,8 +4,10 @@ use anyhow::{anyhow, bail, Context};
44use base64:: prelude:: BASE64_STANDARD ;
55use base64:: Engine ;
66use serde:: Serialize ;
7+ use serde_json:: Value ;
78use static_files:: resource:: new_resource;
89use static_files:: Resource ;
10+ use std:: cmp:: max;
911use std:: collections:: HashMap ;
1012use std:: str:: from_utf8;
1113use std:: sync:: OnceLock ;
@@ -28,14 +30,31 @@ pub fn openubl_ui_resources() -> HashMap<&'static str, Resource> {
2830 resources
2931}
3032
31- pub fn generate_index_html ( ui : & UI , template_file : String ) -> tera:: Result < String > {
32- let template = template_file. replace ( "<%=" , "{{" ) . replace ( "%>" , "}}" ) ;
33+ pub fn generate_index_html (
34+ ui : & UI ,
35+ template_file : String ,
36+ branding_file_content : String ,
37+ ) -> tera:: Result < String > {
38+ let template = template_file
39+ . replace ( "<%=" , "{{" )
40+ . replace ( "%>" , "}}" )
41+ . replace (
42+ "?? branding.application.title" ,
43+ "| default(value=branding.application.title)" ,
44+ )
45+ . replace (
46+ "?? branding.application.title" ,
47+ "| default(value=branding.application.title)" ,
48+ ) ;
3349
3450 let env_json = serde_json:: to_string ( & ui) ?;
3551 let env_base64 = BASE64_STANDARD . encode ( env_json. as_bytes ( ) ) ;
3652
53+ let branding: Value = serde_json:: from_str ( & branding_file_content) ?;
54+
3755 let mut context = tera:: Context :: new ( ) ;
3856 context. insert ( "_env" , & env_base64) ;
57+ context. insert ( "branding" , & branding) ;
3958
4059 tera:: Tera :: one_off ( & template, & context, true )
4160}
@@ -44,19 +63,28 @@ pub fn openubl_ui(ui: &UI) -> anyhow::Result<HashMap<&'static str, Resource>> {
4463 let mut resources = generate ( ) ;
4564
4665 let template_file = resources. get ( "index.html.ejs" ) ;
66+ let branding_file_content = resources. get ( "branding/strings.json" ) ;
4767
4868 let result = INDEX_HTML . get_or_init ( || {
49- if let Some ( template_file) = template_file {
50- let modified = template_file. modified ;
69+ if let ( Some ( template_file) , Some ( branding_file_content) ) =
70+ ( template_file, branding_file_content)
71+ {
72+ let modified = max ( template_file. modified , branding_file_content. modified ) ;
5173 let template_file =
5274 from_utf8 ( template_file. data ) . context ( "cannot interpret template as UTF-8" ) ?;
75+ let branding_file_content = from_utf8 ( branding_file_content. data )
76+ . context ( "cannot interpret branding as UTF-8" ) ?;
5377 Ok ( (
54- generate_index_html ( ui, template_file. to_string ( ) )
55- . expect ( "cannot generate index.html" ) ,
78+ generate_index_html (
79+ ui,
80+ template_file. to_string ( ) ,
81+ branding_file_content. to_string ( ) ,
82+ )
83+ . expect ( "cannot generate index.html" ) ,
5684 modified,
5785 ) )
5886 } else {
59- bail ! ( "Missing template" ) ;
87+ bail ! ( "Missing template or branding " ) ;
6088 }
6189 } ) ;
6290
0 commit comments