This commit is contained in:
PedroEdiaz
2025-06-19 01:42:08 -06:00
parent 65c6a901d2
commit 774d1fd8eb
2 changed files with 41 additions and 15 deletions

View File

@@ -1,16 +1,14 @@
package mustache package mustache
import "base:runtime" import "base:runtime"
import "core:mem"
import "core:reflect" import "core:reflect"
import "core:strings" import "core:strings"
import "core:log" import "core:mem"
import "core:fmt" import "core:fmt"
decode :: proc( v: any, key: string ) -> any { import "core:log"
if key == "." do return v
decode :: proc( v: any, key: string ) -> any {
v := reflect.any_base(v) v := reflect.any_base(v)
ti := type_info_of(v.id) ti := type_info_of(v.id)
@@ -33,7 +31,7 @@ decode :: proc( v: any, key: string ) -> any {
newkey_0 := newkey[0] newkey_0 := newkey[0]
newkey_1 := "." newkey_1 := "."
if len(newkey) == 2 { if len(newkey) != 2 {
newkey_0 = newkey[0][:len(newkey[0])-1] newkey_0 = newkey[0][:len(newkey[0])-1]
newkey_1 = newkey[1] newkey_1 = newkey[1]
} }
@@ -79,6 +77,10 @@ decode :: proc( v: any, key: string ) -> any {
return "" return ""
} }
if key == "." {
return v
}
return "" return ""
} }
@@ -86,25 +88,47 @@ decode_string :: proc( v: any, key: string ) -> string {
return fmt.tprintf("%v", decode(v, key)) return fmt.tprintf("%v", decode(v, key))
} }
section :: proc( r: ^strings.Reader, v: any, key: string ) -> string { @private
mustache_section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) -> string {
tmp := mustache(r, v, key)
if inv {
return tmp
}
defer delete(tmp)
return ""
}
section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) -> string {
save := r.i save := r.i
t := reflect.any_base(decode(v, key)) t := reflect.any_base(decode(v, key))
ti := type_info_of(t.id) ti := type_info_of(t.id)
ret : string= ""
#partial switch i in runtime.type_info_base(ti).variant { #partial switch i in runtime.type_info_base(ti).variant {
case runtime.Type_Info_Slice: case runtime.Type_Info_Slice:
ret : string= ""
for i in 0..<reflect.length(t) { for i in 0..<reflect.length(t) {
elem :=reflect.index(t, i); elem :=reflect.index(t, i);
strings.reader_seek(r, save, .Start) strings.reader_seek(r, save, .Start)
tmp := mustache(r, elem, key)
tmp := mustache_section(r, elem, key, !inv)
defer delete(tmp) defer delete(tmp)
ret = strings.concatenate({ret, tmp}) ret = strings.concatenate({ret, tmp})
} }
return ret
case runtime.Type_Info_Boolean:
b, _ := reflect.as_bool(t)
return mustache_section(r, t, key, b~inv)
} }
return ret if t == nil {
return mustache_section(r, t, key, inv)
}
return mustache_section(r, t, key, !inv)
} }

View File

@@ -21,13 +21,13 @@ A new string with all placeholders replaced by their corresponding values from
the dictionary. If a key is missing in the dictionary, the placeholder is the dictionary. If a key is missing in the dictionary, the placeholder is
replaces with an empty string. replaces with an empty string.
*/ */
mustache_string :: proc(fmt: string, v: any ) -> string { mustache_string :: proc(fmt: string, v: any , end_block: string = "") -> string {
r : strings.Reader r : strings.Reader
strings.reader_init(&r, fmt) strings.reader_init(&r, fmt)
return mustache(&r, v, "") return mustache(&r, v, end_block)
} }
mustache_reader :: proc(r: ^strings.Reader, v: any, end_block: string ) -> string { mustache_reader :: proc(r: ^strings.Reader, v: any, end_block: string = "" ) -> string {
/* /*
template works as a state machine, it manipulates `b` (returned string) template works as a state machine, it manipulates `b` (returned string)
and `key` (placeholder string), according to the states. No error and `key` (placeholder string), according to the states. No error
@@ -82,6 +82,8 @@ mustache_reader :: proc(r: ^strings.Reader, v: any, end_block: string ) -> strin
} }
case '#': case '#':
strings.write_string(&b, section(r, v, skey[1:]) ) strings.write_string(&b, section(r, v, skey[1:]) )
case '^':
strings.write_string(&b, section(r, v, skey[1:], true) )
case: case:
strings.write_string(&b, decode_string(v, skey) ) strings.write_string(&b, decode_string(v, skey) )
} }