package mustache import "core:fmt" import "core:strings" state :: enum { writing, reading_key, open_bracket, close_bracket, } mustache :: proc{mustache_reader, mustache_string} mustache_string :: proc(fmt: string, data: any , section_key: string = "") -> string { r : strings.Reader strings.reader_init(&r, fmt) return mustache(&r, data, section_key) } mustache_reader :: proc(r: ^strings.Reader, data: any, section_key: string = "" ) -> string { /* This is the main parser for mustache templates, it's a recursive decent parser, that works as a state machine, it manipulates the processed template with data, `ret`, and the key to element on data `key`, according to the states. This approach let us process the data as fast as posible. */ ret, key: strings.Builder defer strings.builder_destroy(&key) s:= state.writing for { c, _, err := strings.reader_read_rune(r); if err != nil { break } switch c { case '{': switch s { case .open_bracket: s=.reading_key case .close_bracket: strings.write_string(&ret, "}{" ) s=.writing case .writing: s=.open_bracket case .reading_key: strings.write_rune(&key, '{' ) } case '}': switch s { case .open_bracket: strings.write_string(&ret, "{}" ) s=.writing case .close_bracket: // Work with key skey := strings.to_string(key) strings.builder_reset(&key) s=.writing if len(skey) == 0 { break } switch skey[0] { case '/': if skey[1:] == section_key { return strings.to_string(ret) } case '#': strings.write_string(&ret, section(r, data, skey[1:]) ) case '^': strings.write_string(&ret, section(r, data, skey[1:], true) ) case '&': strings.write_string(&ret, fmt.tprintf("%v",decode(data, skey[1:])) ) case '!': case: dec := decode(data, skey) if dec == nil { // If not decoded write as key strings.write_string(&ret, "{{" ) strings.write_string(&ret, skey ) strings.write_string(&ret, "}}" ) break } clean := _clean_html(fmt.tprintf("%v", dec)) defer delete(clean) strings.write_string(&ret, clean) } case .writing: strings.write_rune(&ret, '}' ) s=.writing case .reading_key: s=.close_bracket } case: switch s { case .open_bracket: strings.write_rune(&ret, '{' ) strings.write_rune(&ret, c ) s=.writing case .close_bracket: strings.write_rune(&key, '}' ) strings.write_rune(&key, c ) s=.reading_key case .writing: strings.write_rune(&ret, c ) s=.writing case .reading_key: strings.write_rune(&key, c ) } } } switch s { case .open_bracket: strings.write_rune(&ret, '{' ) case .reading_key: strings.write_string(&ret,"{{") strings.write_string(&ret,strings.to_string(key)) case .close_bracket: strings.write_string(&ret,"{{") strings.write_string(&ret,strings.to_string(key)) strings.write_rune(&ret, '}' ) case .writing: } return strings.to_string(ret) } /* 1-Clause BSD NON-AI License Copyright (c) 2025 NVIAM. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. The source code and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms, including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives, modifications, or updates based on the Software code. Any usage of the source code in an AI-training dataset is considered a breach of this License. THIS SOFTWARE IS PROVIDED BY NVIAM “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIAM BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */