2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-07-30 23:33:20 +03:00
|
|
|
// 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 (
|
2020-05-22 18:36:09 +03:00
|
|
|
str_start = "sb.write(\'"
|
2020-06-08 13:44:27 +03:00
|
|
|
str_end = "\' ) "
|
2019-07-29 19:21:36 +03:00
|
|
|
)
|
|
|
|
|
2020-06-04 23:30:25 +03:00
|
|
|
// compile_file compiles the content of a file by the given path as a template
|
2020-06-09 13:43:16 +03:00
|
|
|
pub fn compile_file(path, fn_name string) string {
|
2020-06-08 13:44:27 +03:00
|
|
|
mut 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
|
|
|
}
|
2020-06-09 13:43:16 +03:00
|
|
|
return compile_template(html, fn_name)
|
2020-06-04 23:30:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 00:20:48 +03:00
|
|
|
enum State {
|
|
|
|
html
|
|
|
|
css // <style>
|
|
|
|
js // <script>
|
2020-06-10 02:43:04 +03:00
|
|
|
//span // span.{
|
2020-06-10 00:20:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 13:43:16 +03:00
|
|
|
pub fn compile_template(content, fn_name string) string {
|
2020-06-04 23:30:25 +03:00
|
|
|
// lines := os.read_lines(path)
|
|
|
|
mut html := content
|
2019-12-20 00:29:37 +03:00
|
|
|
mut header := ''
|
2020-02-06 12:41:10 +03:00
|
|
|
if os.exists('header.html') && html.contains('@header') {
|
2020-06-08 13:44:27 +03:00
|
|
|
h := os.read_file('header.html') or {
|
2020-01-05 18:29:33 +03:00
|
|
|
panic('reading file header.html failed')
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
header = h.replace("\'", '"')
|
2019-12-09 23:32:21 +03:00
|
|
|
html = header + html
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
|
|
|
lines := html.split_into_lines()
|
|
|
|
mut s := strings.new_builder(1000)
|
2020-05-20 12:04:28 +03:00
|
|
|
// base := path.all_after_last('/').replace('.html', '')
|
2019-12-20 00:29:37 +03:00
|
|
|
s.writeln("
|
2020-06-07 13:26:45 +03:00
|
|
|
import strings
|
2020-06-06 22:36:24 +03:00
|
|
|
// === vweb html template ===
|
2020-06-09 13:43:16 +03:00
|
|
|
fn vweb_tmpl_${fn_name}() {
|
2020-06-06 22:36:24 +03:00
|
|
|
mut sb := strings.new_builder(${lines.len * 30})
|
|
|
|
header := \' \' // TODO remove
|
|
|
|
_ = header
|
|
|
|
//footer := \'footer\'
|
2019-12-20 00:29:37 +03:00
|
|
|
")
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_start)
|
2020-06-10 00:20:48 +03:00
|
|
|
mut state := State.html
|
2020-06-10 02:43:04 +03:00
|
|
|
mut in_span := 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>' {
|
2020-06-10 00:20:48 +03:00
|
|
|
state = .css
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line == '</style>' {
|
2020-06-10 00:20:48 +03:00
|
|
|
state = .html
|
|
|
|
}
|
|
|
|
else if line == '<script>' {
|
|
|
|
state = .js
|
|
|
|
}
|
|
|
|
else if line == '</script>' {
|
|
|
|
state = .html
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
if line.contains('@if ') {
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_end)
|
2019-12-20 00:29:37 +03:00
|
|
|
pos := line.index('@if') or {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.writeln('if ' + line[pos + 4..] + '{')
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_start)
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line.contains('@end') {
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_end)
|
2019-07-29 19:21:36 +03:00
|
|
|
s.writeln('}')
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_start)
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line.contains('@else') {
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_end)
|
2019-07-29 19:21:36 +03:00
|
|
|
s.writeln(' } else { ')
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_start)
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line.contains('@for') {
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_end)
|
2019-12-20 00:29:37 +03:00
|
|
|
pos := line.index('@for') or {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.writeln('for ' + line[pos + 4..] + '{')
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_start)
|
2020-06-10 00:20:48 +03:00
|
|
|
} else if state == .html && line.contains('span.') && line.ends_with('{') {
|
2020-06-08 13:44:27 +03:00
|
|
|
// `span.header {` => `<span class='header'>`
|
|
|
|
class := line.find_between('span.', '{').trim_space()
|
|
|
|
s.writeln('<span class="$class">')
|
2020-06-10 02:43:04 +03:00
|
|
|
in_span = true
|
2020-06-10 00:20:48 +03:00
|
|
|
} else if state == .html && line.contains('.') && line.ends_with('{') {
|
2020-06-07 20:06:02 +03:00
|
|
|
// `.header {` => `<div class='header'>`
|
2020-06-08 13:44:27 +03:00
|
|
|
class := line.find_between('.', '{').trim_space()
|
2019-11-30 13:09:05 +03:00
|
|
|
s.writeln('<div class="$class">')
|
2020-06-10 00:20:48 +03:00
|
|
|
} else if state == .html && line.contains('#') && line.ends_with('{') {
|
2020-06-08 13:44:27 +03:00
|
|
|
// `#header {` => `<div id='header'>`
|
|
|
|
class := line.find_between('#', '{').trim_space()
|
|
|
|
s.writeln('<div id="$class">')
|
2020-06-10 00:20:48 +03:00
|
|
|
} else if state == .html && line == '}' {
|
2020-06-10 02:43:04 +03:00
|
|
|
if in_span {
|
2020-06-08 13:44:27 +03:00
|
|
|
s.writeln('</span>')
|
2020-06-10 02:43:04 +03:00
|
|
|
in_span = false
|
2020-06-08 13:44:27 +03:00
|
|
|
} else {
|
|
|
|
s.writeln('</div>')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// HTML, may include `@var`
|
2019-12-20 00:29:37 +03:00
|
|
|
s.writeln(line.replace('@', '\x24').replace("'", '"'))
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 18:36:09 +03:00
|
|
|
s.writeln(str_end)
|
2020-06-09 19:47:51 +03:00
|
|
|
s.writeln('tmpl_res_$fn_name := sb.str() ')
|
2020-06-07 13:26:45 +03:00
|
|
|
s.writeln('}')
|
2020-06-06 22:36:24 +03:00
|
|
|
s.writeln('// === end of vweb html template ===')
|
2019-07-29 19:21:36 +03:00
|
|
|
return s.str()
|
|
|
|
}
|