2019-07-30 23:33:20 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
module tmpl
|
|
|
|
|
|
|
|
import os
|
|
|
|
import strings
|
|
|
|
|
|
|
|
const (
|
|
|
|
STR_START = 'sb.write(\''
|
|
|
|
STR_END = '\' ) '
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
pub fn compile_template(path string) string {
|
|
|
|
//lines := os.read_lines(path)
|
|
|
|
mut html := os.read_file(path) or {
|
|
|
|
panic('html failed')
|
|
|
|
}
|
|
|
|
mut header := ''
|
|
|
|
if os.file_exists('header.html') {
|
|
|
|
h := os.read_file('header.html') or {
|
|
|
|
panic('html failed')
|
|
|
|
}
|
2019-07-31 05:00:40 +03:00
|
|
|
header = h.replace('\'', '"')
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
lines := html.split_into_lines()
|
|
|
|
mut s := strings.new_builder(1000)
|
|
|
|
base := path.all_after('/').replace('.html', '')
|
|
|
|
s.writeln('module main import strings fn ${base}_view() string { // this line will get removed becase only function body is embedded
|
|
|
|
mut sb := strings.new_builder(${lines.len * 30})
|
|
|
|
header := \'$header\'
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = header
|
2019-07-29 19:21:36 +03:00
|
|
|
//footer := \'footer\'
|
|
|
|
')
|
|
|
|
s.writeln(STR_START)
|
2019-08-12 21:56:47 +03:00
|
|
|
mut in_css :=true// false
|
2019-07-30 23:33:20 +03:00
|
|
|
for _line in lines {
|
|
|
|
line := _line.trim_space()
|
|
|
|
if line == '<style>' {
|
|
|
|
in_css = true
|
|
|
|
}
|
|
|
|
else if line == '</style>' {
|
2019-08-12 21:56:47 +03:00
|
|
|
//in_css = false
|
2019-07-30 23:33:20 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if line.contains('@if ') {
|
|
|
|
s.writeln(STR_END)
|
|
|
|
pos := line.index('@if')
|
|
|
|
s.writeln('if ' + line.right(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')
|
|
|
|
s.writeln('for ' + line.right(pos + 4) + '{')
|
|
|
|
s.writeln(STR_START)
|
|
|
|
}
|
2019-07-30 23:33:20 +03:00
|
|
|
else if !in_css && line.contains('.') && line.ends_with('{') {
|
|
|
|
class := line.find_between('.', '{')
|
|
|
|
s.writeln('<div class="$class">')
|
|
|
|
}
|
|
|
|
else if !in_css && line == '}' {
|
|
|
|
s.writeln('</div>')
|
|
|
|
}
|
|
|
|
// HTML, may include `@var`
|
2019-07-29 19:21:36 +03:00
|
|
|
else {
|
|
|
|
s.writeln(line.replace('@', '\x24').replace('\'', '"') )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.writeln(STR_END)
|
|
|
|
s.writeln('tmpl_res := sb.str() ')
|
|
|
|
s.writeln('return tmpl_res }')
|
|
|
|
return s.str()
|
|
|
|
}
|
|
|
|
|