1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-12-20 00:29:37 +03:00
parent b6fe2ebc0b
commit 6210984c97
54 changed files with 1757 additions and 1993 deletions

View File

@ -1,54 +1,54 @@
// 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.
module tmpl
import os
import strings
const (
STR_START = 'sb.write(\''
STR_END = '\' ) '
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 {
// lines := os.read_lines(path)
mut html := os.read_file(path)or{
panic('html failed')
}
mut header := ''
mut header := ''
if os.exists('header.html') {
h := os.read_file('header.html') or {
h := os.read_file('header.html')or{
panic('html failed')
}
header = h.replace('\'', '"')
header = h.replace("\'", '"')
html = header + html
}
lines := html.split_into_lines()
mut s := strings.new_builder(1000)
//base := path.all_after('/').replace('.html', '')
s.writeln('
// base := path.all_after('/').replace('.html', '')
s.writeln("
mut sb := strings.new_builder(${lines.len * 30})
header := \' \' // TODO remove
_ = header
//footer := \'footer\'
')
")
s.writeln(STR_START)
mut in_css :=true// false
mut in_css := true // false
for _line in lines {
line := _line.trim_space()
if line == '<style>' {
in_css = true
}
else if line == '</style>' {
//in_css = false
// in_css = false
}
if line.contains('@if ') {
s.writeln(STR_END)
pos := line.index('@if') or { continue }
s.writeln('if ' + line[pos+4..] + '{')
pos := line.index('@if') or {
continue
}
s.writeln('if ' + line[pos + 4..] + '{')
s.writeln(STR_START)
}
else if line.contains('@end') {
@ -63,8 +63,10 @@ _ = header
}
else if line.contains('@for') {
s.writeln(STR_END)
pos := line.index('@for') or { continue }
s.writeln('for ' + line[pos+4..] + '{')
pos := line.index('@for') or {
continue
}
s.writeln('for ' + line[pos + 4..] + '{')
s.writeln(STR_START)
}
else if !in_css && line.contains('.') && line.ends_with('{') {
@ -76,7 +78,7 @@ _ = header
}
// HTML, may include `@var`
else {
s.writeln(line.replace('@', '\x24').replace("'", '"') )
s.writeln(line.replace('@', '\x24').replace("'", '"'))
}
}
s.writeln(STR_END)