diff --git a/decode.odin b/decode.odin index 69d1ebb..63abbed 100644 --- a/decode.odin +++ b/decode.odin @@ -1,16 +1,14 @@ package mustache import "base:runtime" -import "core:mem" - import "core:reflect" import "core:strings" -import "core:log" +import "core:mem" import "core:fmt" -decode :: proc( v: any, key: string ) -> any { - if key == "." do return v +import "core:log" +decode :: proc( v: any, key: string ) -> any { v := reflect.any_base(v) ti := type_info_of(v.id) @@ -33,7 +31,7 @@ decode :: proc( v: any, key: string ) -> any { newkey_0 := newkey[0] newkey_1 := "." - if len(newkey) == 2 { + if len(newkey) != 2 { newkey_0 = newkey[0][:len(newkey[0])-1] newkey_1 = newkey[1] } @@ -79,32 +77,58 @@ decode :: proc( v: any, key: string ) -> any { return "" } - return "" + if key == "." { + return v + } + + return "" } decode_string :: proc( v: any, key: string ) -> string { 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 t := reflect.any_base(decode(v, key)) ti := type_info_of(t.id) - ret : string= "" #partial switch i in runtime.type_info_base(ti).variant { case runtime.Type_Info_Slice: + ret : string= "" for i in 0.. string { +mustache_string :: proc(fmt: string, v: any , end_block: string = "") -> string { r : strings.Reader 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) 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 '#': strings.write_string(&b, section(r, v, skey[1:]) ) + case '^': + strings.write_string(&b, section(r, v, skey[1:], true) ) case: strings.write_string(&b, decode_string(v, skey) ) }