// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module tmpl
import os
import strings
const (
str_start = "sb.write(\'"
str_end = "\' ) "
)
// compile_file compiles the content of a file by the given path as a template
pub fn compile_file(path, fn_name string) string {
mut html := os.read_file(path) or {
panic('html failed')
}
return compile_template(html, fn_name)
}
enum State {
html
css // ' {
state = .html
}
else if line == '' {
state = .html
}
if line.contains('@include ') {
// TODO
pos := line.index('@include ') or {
continue
}
mut file_name := line[pos + 9..]
file_path := os.join_path('templates', '${file_name}.html')
mut file_content := os.read_file(file_path) or {
panic('reading file $file_name failed')
}
file_content = file_content.replace("\'", '"')
include_file_lines := file_content.split_into_lines()
lines_before := lines[0 .. i].clone()
lines_after := lines[i + 1 .. lines.len].clone()
lines = lines_before
lines << include_file_lines
lines << lines_after
i--
continue
} else if line.contains('@js ') {
pos := line.index('@js') or {
continue
}
s.write('')
} else if line.contains('@css ') {
pos := line.index('@css') or {
continue
}
s.write('')
} else if line.contains('@if ') {
s.writeln(str_end)
pos := line.index('@if') or {
continue
}
s.writeln('if ' + line[pos + 4..] + '{')
s.writeln(str_start)
} else if line.contains('@end') {
s.writeln(str_end)
s.writeln('}')
s.writeln(str_start)
} else if line.contains('@else') {
s.writeln(str_end)
s.writeln(' } else { ')
s.writeln(str_start)
} else if line.contains('@for') {
s.writeln(str_end)
pos := line.index('@for') or {
continue
}
s.writeln('for ' + line[pos + 4..] + '{')
s.writeln(str_start)
} else if state == .html && line.contains('span.') && line.ends_with('{') {
// `span.header {` => `