2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 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 (
|
2021-02-22 14:18:11 +03:00
|
|
|
str_start = "sb.write_string('"
|
2021-01-13 08:05: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-10-15 13:32:28 +03:00
|
|
|
pub fn compile_file(path string, fn_name string) string {
|
2021-02-28 20:14:19 +03:00
|
|
|
basepath := os.dir(path)
|
2020-12-27 12:38:12 +03:00
|
|
|
html := os.read_file(path) or { panic('html failed') }
|
2021-02-28 20:14:19 +03:00
|
|
|
return compile_template(basepath, html, fn_name)
|
2020-06-04 23:30:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 00:20:48 +03:00
|
|
|
enum State {
|
|
|
|
html
|
|
|
|
css // <style>
|
2020-12-27 12:38:12 +03:00
|
|
|
js // <script>
|
|
|
|
// span // span.{
|
2020-06-10 00:20:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-28 20:14:19 +03:00
|
|
|
pub fn compile_template(basepath string, html_ string, fn_name string) string {
|
2020-06-04 23:30:25 +03:00
|
|
|
// lines := os.read_lines(path)
|
2020-06-11 21:26:46 +03:00
|
|
|
mut html := html_.trim_space()
|
2020-06-12 15:32:09 +03:00
|
|
|
mut header := ''
|
2020-06-22 17:52:03 +03:00
|
|
|
mut footer := ''
|
2021-03-01 12:15:59 +03:00
|
|
|
if os.exists(os.join_path(basepath, 'header.html')) && html.contains('@header') {
|
|
|
|
h := os.read_file(os.join_path(basepath, 'header.html')) or {
|
|
|
|
panic('reading file ${os.join_path(basepath, 'header.html')} failed')
|
2020-06-12 15:32:09 +03:00
|
|
|
}
|
2021-01-13 08:05:27 +03:00
|
|
|
header = h.trim_space().replace("'", '"')
|
2020-06-12 15:32:09 +03:00
|
|
|
html = header + html
|
|
|
|
}
|
2021-03-01 12:15:59 +03:00
|
|
|
if os.exists(os.join_path(basepath, 'footer.html')) && html.contains('@footer') {
|
|
|
|
f := os.read_file(os.join_path(basepath, 'footer.html')) or {
|
|
|
|
panic('reading file ${os.join_path(basepath, 'footer.html')} failed')
|
2020-06-22 17:52:03 +03:00
|
|
|
}
|
2021-01-13 08:05:27 +03:00
|
|
|
footer = f.trim_space().replace("'", '"')
|
2020-06-22 17:52:03 +03:00
|
|
|
html += footer
|
|
|
|
}
|
2020-06-11 12:13:36 +03:00
|
|
|
mut lines := html.split_into_lines()
|
2020-12-27 12:38:12 +03:00
|
|
|
lstartlength := lines.len * 30
|
2019-11-30 13:09:05 +03:00
|
|
|
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-11 12:13:36 +03:00
|
|
|
import strings
|
|
|
|
// === vweb html template ===
|
|
|
|
fn vweb_tmpl_${fn_name}() {
|
2020-12-27 12:38:12 +03:00
|
|
|
mut sb := strings.new_builder($lstartlength)\n
|
2020-06-11 12:13:36 +03:00
|
|
|
header := \' \' // TODO remove
|
|
|
|
_ = header
|
2020-06-22 17:52:03 +03:00
|
|
|
footer := \' \' // TODO remove
|
|
|
|
_ = footer
|
2020-06-11 12:13:36 +03:00
|
|
|
|
|
|
|
")
|
2021-02-22 14:18:11 +03:00
|
|
|
s.write_string(tmpl.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
|
2020-12-27 12:38:12 +03:00
|
|
|
// for _line in lines {
|
|
|
|
for i := 0; i < lines.len; i++ {
|
2020-07-01 22:03:14 +03:00
|
|
|
line := lines[i].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
|
2020-12-27 12:38:12 +03:00
|
|
|
} else if line == '<script>' {
|
2020-06-10 00:20:48 +03:00
|
|
|
state = .js
|
2020-12-27 12:38:12 +03:00
|
|
|
} else if line == '</script>' {
|
2020-06-10 00:20:48 +03:00
|
|
|
state = .html
|
2019-11-30 13:09:05 +03:00
|
|
|
}
|
2021-02-04 19:07:04 +03:00
|
|
|
if line.contains('@include ') {
|
|
|
|
lines.delete(i)
|
2021-02-28 19:52:31 +03:00
|
|
|
mut file_name := line.split("'")[1]
|
|
|
|
mut file_ext := os.file_ext(file_name)
|
|
|
|
if file_ext == '' {
|
|
|
|
file_ext = '.html'
|
|
|
|
}
|
|
|
|
file_name = file_name.replace(file_ext, '')
|
2021-03-01 12:15:59 +03:00
|
|
|
// relative path, starting with the current folder
|
|
|
|
mut templates_folder := os.real_path(basepath)
|
|
|
|
if file_name.contains('/') && file_name.starts_with('/') {
|
|
|
|
// an absolute path
|
|
|
|
templates_folder = ''
|
2021-02-28 19:52:31 +03:00
|
|
|
}
|
|
|
|
file_path := os.real_path(os.join_path(templates_folder, '$file_name$file_ext'))
|
|
|
|
$if trace_tmpl ? {
|
2021-02-28 20:14:19 +03:00
|
|
|
eprintln('>>> basepath: "$basepath" , fn_name: "$fn_name" , @include line: "$line" , file_name: "$file_name" , file_ext: "$file_ext" , templates_folder: "$templates_folder" , file_path: "$file_path"')
|
2021-02-28 19:52:31 +03:00
|
|
|
}
|
2021-02-04 19:07:04 +03:00
|
|
|
file_content := os.read_file(file_path) or {
|
2021-03-01 12:15:59 +03:00
|
|
|
panic('Vweb: reading file $file_name from path: $file_path failed.')
|
2020-06-10 19:53:04 +03:00
|
|
|
}
|
2021-02-04 19:07:04 +03:00
|
|
|
file_splitted := file_content.split_into_lines().reverse()
|
|
|
|
for f in file_splitted {
|
|
|
|
lines.insert(i, f)
|
2020-06-12 15:32:09 +03:00
|
|
|
}
|
2021-02-04 19:07:04 +03:00
|
|
|
i--
|
2020-06-11 21:34:59 +03:00
|
|
|
} else if line.contains('@js ') {
|
2020-12-27 12:38:12 +03:00
|
|
|
pos := line.index('@js') or { continue }
|
2021-02-22 14:18:11 +03:00
|
|
|
s.write_string('<script src="')
|
|
|
|
s.write_string(line[pos + 5..line.len - 1])
|
2020-07-01 22:03:14 +03:00
|
|
|
s.writeln('"></script>')
|
2020-06-11 21:34:59 +03:00
|
|
|
} else if line.contains('@css ') {
|
2020-12-27 12:38:12 +03:00
|
|
|
pos := line.index('@css') or { continue }
|
2021-02-22 14:18:11 +03:00
|
|
|
s.write_string('<link href="')
|
|
|
|
s.write_string(line[pos + 6..line.len - 1])
|
2020-07-01 22:03:14 +03:00
|
|
|
s.writeln('" rel="stylesheet" type="text/css">')
|
2020-06-10 19:53:04 +03:00
|
|
|
} else if line.contains('@if ') {
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_end)
|
2020-12-27 12:38:12 +03:00
|
|
|
pos := line.index('@if') or { continue }
|
2019-12-20 00:29:37 +03:00
|
|
|
s.writeln('if ' + line[pos + 4..] + '{')
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_start)
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line.contains('@end') {
|
2021-03-01 12:15:59 +03:00
|
|
|
// Remove new line byte
|
|
|
|
s.go_back(1)
|
|
|
|
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_end)
|
2019-07-29 19:21:36 +03:00
|
|
|
s.writeln('}')
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_start)
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line.contains('@else') {
|
2021-03-01 12:15:59 +03:00
|
|
|
// Remove new line byte
|
|
|
|
s.go_back(1)
|
|
|
|
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_end)
|
2019-07-29 19:21:36 +03:00
|
|
|
s.writeln(' } else { ')
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_start)
|
2020-06-08 13:44:27 +03:00
|
|
|
} else if line.contains('@for') {
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_end)
|
2020-12-27 12:38:12 +03:00
|
|
|
pos := line.index('@for') or { continue }
|
2019-12-20 00:29:37 +03:00
|
|
|
s.writeln('for ' + line[pos + 4..] + '{')
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.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-12-27 12:38:12 +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`
|
2020-06-24 23:12:33 +03:00
|
|
|
// escaped by cgen, unless it's a `vweb.RawHtml` string
|
2021-03-01 12:15:59 +03:00
|
|
|
s.writeln(line.replace('@', '$').replace('$$', '@').replace("'", "\\'"))
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
s.writeln(tmpl.str_end)
|
2020-06-10 13:17:49 +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()
|
|
|
|
}
|