@@ -4,8 +4,10 @@ use anyhow::{anyhow, bail, Context};
4
4
use base64:: prelude:: BASE64_STANDARD ;
5
5
use base64:: Engine ;
6
6
use serde:: Serialize ;
7
+ use serde_json:: Value ;
7
8
use static_files:: resource:: new_resource;
8
9
use static_files:: Resource ;
10
+ use std:: cmp:: max;
9
11
use std:: collections:: HashMap ;
10
12
use std:: str:: from_utf8;
11
13
use std:: sync:: OnceLock ;
@@ -28,14 +30,31 @@ pub fn openubl_ui_resources() -> HashMap<&'static str, Resource> {
28
30
resources
29
31
}
30
32
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
+ ) ;
33
49
34
50
let env_json = serde_json:: to_string ( & ui) ?;
35
51
let env_base64 = BASE64_STANDARD . encode ( env_json. as_bytes ( ) ) ;
36
52
53
+ let branding: Value = serde_json:: from_str ( & branding_file_content) ?;
54
+
37
55
let mut context = tera:: Context :: new ( ) ;
38
56
context. insert ( "_env" , & env_base64) ;
57
+ context. insert ( "branding" , & branding) ;
39
58
40
59
tera:: Tera :: one_off ( & template, & context, true )
41
60
}
@@ -44,19 +63,28 @@ pub fn openubl_ui(ui: &UI) -> anyhow::Result<HashMap<&'static str, Resource>> {
44
63
let mut resources = generate ( ) ;
45
64
46
65
let template_file = resources. get ( "index.html.ejs" ) ;
66
+ let branding_file_content = resources. get ( "branding/strings.json" ) ;
47
67
48
68
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 ) ;
51
73
let template_file =
52
74
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" ) ?;
53
77
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" ) ,
56
84
modified,
57
85
) )
58
86
} else {
59
- bail ! ( "Missing template" ) ;
87
+ bail ! ( "Missing template or branding " ) ;
60
88
}
61
89
} ) ;
62
90
0 commit comments