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-11-30 13:09:05 +03:00
|
|
|
module tmpl
|
2019-07-29 19:21:36 +03:00
|
|
|
|
2019-11-30 13:09:05 +03:00
|
|
|
import os
|
|
|
|
import strings
|
2019-07-29 19:21:36 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
STR_START = 'sb.write(\''
|
|
|
|
STR_END = '\' ) '
|
|
|
|
)
|
|
|
|
|
2019-11-30 13:09:05 +03:00
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
pub fn compile_template(path string) string {
|
|
|
|
//lines := os.read_lines(path)
|
2019-12-06 15:24:53 +03:00
|
|
|
html := os.read_file(path) or {
|
2019-07-29 19:21:36 +03:00
|
|
|
panic('html failed')
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
mut header := ''
|
2019-12-04 23:03:12 +03:00
|
|
|
if os.exists('header.html') {
|
2019-07-29 19:21:36 +03:00
|
|
|
h := os.read_file('header.html') or {
|
|
|
|
panic('html failed')
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
|
|
|
header = h.replace('\'', '"')
|
|
|
|
}
|
|
|
|
lines := html.split_into_lines()
|
|
|
|
mut s := strings.new_builder(1000)
|
2019-12-06 15:24:53 +03:00
|
|
|
//base := path.all_after('/').replace('.html', '')
|
2019-11-30 13:09:05 +03:00
|
|
|
s.writeln('
|
2019-07-29 19:21:36 +03:00
|
|
|
mut sb := strings.new_builder(${lines.len * 30})
|
2019-11-30 13:09:05 +03:00
|
|
|
header := \'$header\'
|
|
|
|
_ = header
|
|
|
|
//footer := \'footer\'
|
|
|
|
')
|
2019-07-29 19:21:36 +03:00
|
|
|
s.writeln(STR_START)
|
2019-11-30 13:09:05 +03:00
|
|
|
mut in_css :=true// false
|
2019-07-30 23:33:20 +03:00
|
|
|
for _line in lines {
|
2019-11-30 13:09:05 +03:00
|
|
|
line := _line.trim_space()
|
2019-07-30 23:33:20 +03:00
|
|
|
if line == '<style>' {
|
2019-11-30 13:09:05 +03:00
|
|
|
in_css = true
|
|
|
|
}
|
2019-07-30 23:33:20 +03:00
|
|
|
else if line == '</style>' {
|
2019-11-30 13:09:05 +03:00
|
|
|
//in_css = false
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if line.contains('@if ') {
|
|
|
|
s.writeln(STR_END)
|
2019-11-30 13:09:05 +03:00
|
|
|
pos := line.index('@if') or { continue }
|
|
|
|
s.writeln('if ' + line[pos+4..] + '{')
|
2019-07-29 19:21:36 +03:00
|
|
|
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)
|
2019-11-30 13:09:05 +03:00
|
|
|
pos := line.index('@for') or { continue }
|
|
|
|
s.writeln('for ' + line[pos+4..] + '{')
|
2019-07-29 19:21:36 +03:00
|
|
|
s.writeln(STR_START)
|
|
|
|
}
|
2019-07-30 23:33:20 +03:00
|
|
|
else if !in_css && line.contains('.') && line.ends_with('{') {
|
2019-11-30 13:09:05 +03:00
|
|
|
class := line.find_between('.', '{')
|
|
|
|
s.writeln('<div class="$class">')
|
|
|
|
}
|
2019-07-30 23:33:20 +03:00
|
|
|
else if !in_css && line == '}' {
|
2019-11-30 13:09:05 +03:00
|
|
|
s.writeln('</div>')
|
|
|
|
}
|
|
|
|
// HTML, may include `@var`
|
|
|
|
else {
|
|
|
|
s.writeln(line.replace('@', '\x24').replace('\'', '"') )
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.writeln(STR_END)
|
2019-11-30 13:09:05 +03:00
|
|
|
s.writeln('tmpl_res := sb.str() }')
|
2019-07-29 19:21:36 +03:00
|
|
|
return s.str()
|
|
|
|
}
|
|
|
|
|