Compare commits
2 Commits
main
...
c466e55169
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c466e55169 | ||
|
|
fedfd70c50 |
@@ -1,5 +1,5 @@
|
||||
# Templateless
|
||||
A lightweight, logicless templating library for the
|
||||
# Mustacheless
|
||||
A subset of {{mustache}}, logicless templating library for the
|
||||
[Odin programming language](https://odin-lang.org/). This library allows you to
|
||||
define templates using placeholder variables within strings and replace them
|
||||
with values from a provided dictionary.
|
||||
|
||||
81
decode.odin
Normal file
81
decode.odin
Normal file
@@ -0,0 +1,81 @@
|
||||
package mustache
|
||||
|
||||
import "base:runtime"
|
||||
import "core:reflect"
|
||||
import "core:strconv"
|
||||
import "core:log"
|
||||
|
||||
decode :: proc( v: any, key: string ) -> string {
|
||||
|
||||
if v == nil do return ""
|
||||
|
||||
ti := runtime.type_info_base(type_info_of(v.id))
|
||||
a := any{v.data, ti.id}
|
||||
|
||||
#partial switch info in ti.variant {
|
||||
|
||||
case runtime.Type_Info_Struct:
|
||||
return decode( reflect.struct_field_value_by_name(v, key), "." )
|
||||
|
||||
case runtime.Type_Info_String:
|
||||
if key != "." do return ""
|
||||
|
||||
switch t in a {
|
||||
case string:
|
||||
return t
|
||||
case cstring:
|
||||
return string(t)
|
||||
}
|
||||
|
||||
case runtime.Type_Info_Integer:
|
||||
if key != "." do return ""
|
||||
|
||||
buf: [40]byte
|
||||
u := cast_any_int_to_u128(a)
|
||||
|
||||
return strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
|
||||
case runtime.Type_Info_Enum:
|
||||
return decode(any{v.data, info.base.id}, key)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
@(private)
|
||||
cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
|
||||
u: u128 = 0
|
||||
|
||||
switch i in any_int_value {
|
||||
case i8: u = u128(i)
|
||||
case i16: u = u128(i)
|
||||
case i32: u = u128(i)
|
||||
case i64: u = u128(i)
|
||||
case i128: u = u128(i)
|
||||
case int: u = u128(i)
|
||||
case u8: u = u128(i)
|
||||
case u16: u = u128(i)
|
||||
case u32: u = u128(i)
|
||||
case u64: u = u128(i)
|
||||
case u128: u = u128(i)
|
||||
case uint: u = u128(i)
|
||||
case uintptr: u = u128(i)
|
||||
|
||||
case i16le: u = u128(i)
|
||||
case i32le: u = u128(i)
|
||||
case i64le: u = u128(i)
|
||||
case u16le: u = u128(i)
|
||||
case u32le: u = u128(i)
|
||||
case u64le: u = u128(i)
|
||||
case u128le: u = u128(i)
|
||||
case i16be: u = u128(i)
|
||||
case i32be: u = u128(i)
|
||||
case i64be: u = u128(i)
|
||||
case u16be: u = u128(i)
|
||||
case u32be: u = u128(i)
|
||||
case u64be: u = u128(i)
|
||||
case u128be: u = u128(i)
|
||||
}
|
||||
|
||||
return u;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package template
|
||||
package mustache
|
||||
|
||||
import "core:strings"
|
||||
|
||||
@@ -20,7 +20,7 @@ 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
|
||||
replaces with an empty string.
|
||||
*/
|
||||
template :: proc(fmt: string, dict: map[string]string ) -> string {
|
||||
mustache :: proc(fmt: string, v: any ) -> string {
|
||||
/*
|
||||
template works as a state machine, it manipulates `b` (returned string)
|
||||
and `key` (placeholder string), according to the states. No error
|
||||
@@ -52,7 +52,7 @@ template :: proc(fmt: string, dict: map[string]string ) -> string {
|
||||
strings.write_string(&b, "{}" )
|
||||
s=.writing
|
||||
case .close_bracket:
|
||||
strings.write_string(&b, dict[strings.to_string(key)] )
|
||||
strings.write_string(&b, decode(v, strings.to_string(key) ))
|
||||
strings.builder_reset(&key)
|
||||
s=.writing
|
||||
case .writing:
|
||||
33
testing.odin
33
testing.odin
@@ -1,41 +1,41 @@
|
||||
#+feature dynamic-literals
|
||||
#+private
|
||||
package template
|
||||
package mustache
|
||||
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
test1 :: proc(t: ^testing.T){
|
||||
fmt := "{"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp==fmt, tmp)
|
||||
}
|
||||
@(test)
|
||||
test2 :: proc(t: ^testing.T){
|
||||
fmt := "}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp==fmt, tmp)
|
||||
}
|
||||
@(test)
|
||||
test3 :: proc(t: ^testing.T){
|
||||
fmt := "{{"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp==fmt, tmp)
|
||||
}
|
||||
@(test)
|
||||
test4 :: proc(t: ^testing.T){
|
||||
fmt := "{{}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp==fmt, tmp)
|
||||
}
|
||||
@(test)
|
||||
test5 :: proc(t: ^testing.T){
|
||||
fmt := "{{}}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="", tmp)
|
||||
}
|
||||
@@ -43,66 +43,65 @@ test5 :: proc(t: ^testing.T){
|
||||
test6 :: proc(t: ^testing.T){
|
||||
fmt := "{{foo}}"
|
||||
|
||||
dict := map[string]string{"foo"="var"}
|
||||
defer delete(dict)
|
||||
dict : struct { foo: string } = {"var"}
|
||||
|
||||
tmp := template(fmt, dict)
|
||||
tmp := mustache(fmt, dict)
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="var", tmp)
|
||||
}
|
||||
@(test)
|
||||
test7 :: proc(t: ^testing.T){
|
||||
fmt := "{{{}}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="", tmp)
|
||||
}
|
||||
@(test)
|
||||
test8 :: proc(t: ^testing.T){
|
||||
fmt := "{{}}}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="}", tmp)
|
||||
}
|
||||
@(test)
|
||||
test9 :: proc(t: ^testing.T){
|
||||
fmt := "{{{}}}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="}", tmp)
|
||||
}
|
||||
@(test)
|
||||
test10 :: proc(t: ^testing.T){
|
||||
fmt := "{{} }}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="", tmp)
|
||||
}
|
||||
@(test)
|
||||
test11 :: proc(t: ^testing.T){
|
||||
fmt := "{{{} }}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="", tmp)
|
||||
}
|
||||
@(test)
|
||||
test12 :: proc(t: ^testing.T){
|
||||
fmt := " {{{} }}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp==" ", tmp)
|
||||
}
|
||||
@(test)
|
||||
test13 :: proc(t: ^testing.T){
|
||||
fmt := "{{{} }} "
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp==" ", tmp)
|
||||
}
|
||||
@(test)
|
||||
test14 :: proc(t: ^testing.T){
|
||||
fmt := "{{{}}}"
|
||||
tmp := template(fmt,{})
|
||||
tmp := mustache(fmt,{})
|
||||
defer delete(tmp)
|
||||
testing.expect(t, tmp=="}", tmp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user