v/vlib/vweb/tmpl/tmpl.v

108 lines
2.9 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-07-30 22:33:20 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module tmpl
2019-07-29 18:21:36 +02:00
import os
import strings
2019-07-29 18:21:36 +02:00
const (
2020-05-22 17:36:09 +02:00
str_start = "sb.write(\'"
2020-06-08 12:44:27 +02:00
str_end = "\' ) "
2019-07-29 18:21:36 +02:00
)
// compile_file compiles the content of a file by the given path as a template
pub fn compile_file(path, fn_name string) string {
2020-06-08 12:44:27 +02:00
mut html := os.read_file(path) or {
2019-07-29 18:21:36 +02:00
panic('html failed')
}
return compile_template(html, fn_name)
}
pub fn compile_template(content, fn_name string) string {
// lines := os.read_lines(path)
mut html := content
2019-12-19 22:29:37 +01:00
mut header := ''
2020-02-06 10:41:10 +01:00
if os.exists('header.html') && html.contains('@header') {
2020-06-08 12:44:27 +02:00
h := os.read_file('header.html') or {
2020-01-05 16:29:33 +01:00
panic('reading file header.html failed')
}
2019-12-19 22:29:37 +01:00
header = h.replace("\'", '"')
html = header + html
}
lines := html.split_into_lines()
mut s := strings.new_builder(1000)
// base := path.all_after_last('/').replace('.html', '')
2019-12-19 22:29:37 +01:00
s.writeln("
import strings
2020-06-06 21:36:24 +02:00
// === vweb html template ===
fn vweb_tmpl_${fn_name}() {
2020-06-06 21:36:24 +02:00
mut sb := strings.new_builder(${lines.len * 30})
header := \' \' // TODO remove
_ = header
//footer := \'footer\'
2019-12-19 22:29:37 +01:00
")
2020-05-22 17:36:09 +02:00
s.writeln(str_start)
2020-06-08 12:44:27 +02:00
mut in_css := false // false
mut in_span := false
2019-07-30 22:33:20 +02:00
for _line in lines {
line := _line.trim_space()
2019-07-30 22:33:20 +02:00
if line == '<style>' {
in_css = true
2020-06-08 12:44:27 +02:00
} else if line == '</style>' {
2019-12-19 22:29:37 +01:00
// in_css = false
}
2019-07-29 18:21:36 +02:00
if line.contains('@if ') {
2020-05-22 17:36:09 +02:00
s.writeln(str_end)
2019-12-19 22:29:37 +01:00
pos := line.index('@if') or {
continue
}
s.writeln('if ' + line[pos + 4..] + '{')
2020-05-22 17:36:09 +02:00
s.writeln(str_start)
2020-06-08 12:44:27 +02:00
} else if line.contains('@end') {
2020-05-22 17:36:09 +02:00
s.writeln(str_end)
2019-07-29 18:21:36 +02:00
s.writeln('}')
2020-05-22 17:36:09 +02:00
s.writeln(str_start)
2020-06-08 12:44:27 +02:00
} else if line.contains('@else') {
2020-05-22 17:36:09 +02:00
s.writeln(str_end)
2019-07-29 18:21:36 +02:00
s.writeln(' } else { ')
2020-05-22 17:36:09 +02:00
s.writeln(str_start)
2020-06-08 12:44:27 +02:00
} else if line.contains('@for') {
2020-05-22 17:36:09 +02:00
s.writeln(str_end)
2019-12-19 22:29:37 +01:00
pos := line.index('@for') or {
continue
}
s.writeln('for ' + line[pos + 4..] + '{')
2020-05-22 17:36:09 +02:00
s.writeln(str_start)
2020-06-08 12:44:27 +02:00
} else if !in_css && line.contains('span.') && line.ends_with('{') {
// `span.header {` => `<span class='header'>`
class := line.find_between('span.', '{').trim_space()
s.writeln('<span class="$class">')
in_span = true
} else if !in_css && line.contains('.') && line.ends_with('{') {
// `.header {` => `<div class='header'>`
2020-06-08 12:44:27 +02:00
class := line.find_between('.', '{').trim_space()
s.writeln('<div class="$class">')
2020-06-08 12:44:27 +02:00
} else if !in_css && line.contains('#') && line.ends_with('{') {
// `#header {` => `<div id='header'>`
class := line.find_between('#', '{').trim_space()
s.writeln('<div id="$class">')
} else if !in_css && line == '}' {
if in_span {
s.writeln('</span>')
in_span = false
} else {
s.writeln('</div>')
}
} else {
// HTML, may include `@var`
2019-12-19 22:29:37 +01:00
s.writeln(line.replace('@', '\x24').replace("'", '"'))
2019-07-29 18:21:36 +02:00
}
}
2020-05-22 17:36:09 +02:00
s.writeln(str_end)
s.writeln('_tmpl_res := sb.str() ')
s.writeln('}')
2020-06-06 21:36:24 +02:00
s.writeln('// === end of vweb html template ===')
2019-07-29 18:21:36 +02:00
return s.str()
}