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.
|
2019-11-30 11:09:05 +01:00
|
|
|
module tmpl
|
2019-07-29 18:21:36 +02:00
|
|
|
|
2019-11-30 11:09:05 +01: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
|
|
|
)
|
|
|
|
|
2020-06-04 22:30:25 +02:00
|
|
|
// compile_file compiles the content of a file by the given path as a template
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn compile_file(path string, fn_name string) string {
|
2020-09-22 05:28:29 +02:00
|
|
|
html := os.read_file(path) or {
|
2019-07-29 18:21:36 +02:00
|
|
|
panic('html failed')
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2020-06-09 12:43:16 +02:00
|
|
|
return compile_template(html, fn_name)
|
2020-06-04 22:30:25 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 23:20:48 +02:00
|
|
|
enum State {
|
|
|
|
html
|
|
|
|
css // <style>
|
|
|
|
js // <script>
|
2020-06-10 01:43:04 +02:00
|
|
|
//span // span.{
|
2020-06-09 23:20:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn compile_template(html_ string, fn_name string) string {
|
2020-06-04 22:30:25 +02:00
|
|
|
// lines := os.read_lines(path)
|
2020-06-11 20:26:46 +02:00
|
|
|
mut html := html_.trim_space()
|
2020-06-12 14:32:09 +02:00
|
|
|
mut header := ''
|
2020-06-22 16:52:03 +02:00
|
|
|
mut footer := ''
|
2020-06-12 14:32:09 +02:00
|
|
|
if os.exists('templates/header.html') && html.contains('@header') {
|
|
|
|
h := os.read_file('templates/header.html') or {
|
|
|
|
panic('reading file templates/header.html failed')
|
|
|
|
}
|
|
|
|
header = h.trim_space().replace("\'", '"')
|
|
|
|
html = header + html
|
|
|
|
}
|
2020-06-22 16:52:03 +02:00
|
|
|
if os.exists('templates/footer.html') && html.contains('@footer') {
|
|
|
|
f := os.read_file('templates/footer.html') or {
|
|
|
|
panic('reading file templates/footer.html failed')
|
|
|
|
}
|
|
|
|
footer = f.trim_space().replace("\'", '"')
|
|
|
|
html += footer
|
|
|
|
}
|
2020-06-12 14:32:09 +02:00
|
|
|
|
2020-06-11 11:13:36 +02:00
|
|
|
mut lines := html.split_into_lines()
|
2019-11-30 11:09:05 +01:00
|
|
|
mut s := strings.new_builder(1000)
|
2020-05-20 11:04:28 +02:00
|
|
|
// base := path.all_after_last('/').replace('.html', '')
|
2019-12-19 22:29:37 +01:00
|
|
|
s.writeln("
|
2020-06-11 11:13:36 +02:00
|
|
|
import strings
|
|
|
|
// === vweb html template ===
|
|
|
|
fn vweb_tmpl_${fn_name}() {
|
|
|
|
mut sb := strings.new_builder(${lines.len * 30})\n
|
|
|
|
header := \' \' // TODO remove
|
|
|
|
_ = header
|
2020-06-22 16:52:03 +02:00
|
|
|
footer := \' \' // TODO remove
|
|
|
|
_ = footer
|
2020-06-11 11:13:36 +02:00
|
|
|
|
|
|
|
")
|
2020-06-13 16:59:03 +02:00
|
|
|
s.write(str_start)
|
2020-06-09 23:20:48 +02:00
|
|
|
mut state := State.html
|
2020-06-10 01:43:04 +02:00
|
|
|
mut in_span := false
|
2020-06-11 11:13:36 +02:00
|
|
|
//for _line in lines {
|
|
|
|
for i := 0; i < lines.len; i ++ {
|
2020-07-01 21:03:14 +02:00
|
|
|
line := lines[i].trim_space()
|
2019-07-30 22:33:20 +02:00
|
|
|
if line == '<style>' {
|
2020-06-09 23:20:48 +02:00
|
|
|
state = .css
|
2020-06-08 12:44:27 +02:00
|
|
|
} else if line == '</style>' {
|
2020-06-09 23:20:48 +02:00
|
|
|
state = .html
|
|
|
|
}
|
|
|
|
else if line == '<script>' {
|
|
|
|
state = .js
|
|
|
|
}
|
|
|
|
else if line == '</script>' {
|
|
|
|
state = .html
|
2019-11-30 11:09:05 +01:00
|
|
|
}
|
2020-06-12 14:32:09 +02:00
|
|
|
if line.contains('@include ') && false {
|
2020-06-11 20:34:59 +02:00
|
|
|
// TODO
|
2020-06-10 18:53:04 +02:00
|
|
|
pos := line.index('@include ') or {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-22 05:28:29 +02:00
|
|
|
file_name := line[pos + 9..]
|
2020-06-10 18:53:04 +02:00
|
|
|
file_path := os.join_path('templates', '${file_name}.html')
|
|
|
|
mut file_content := os.read_file(file_path) or {
|
|
|
|
panic('reading file $file_name failed')
|
|
|
|
}
|
|
|
|
file_content = file_content.replace("\'", '"')
|
2020-06-12 14:32:09 +02:00
|
|
|
lines2 := file_content.split_into_lines()
|
|
|
|
for l in lines2 {
|
|
|
|
lines.insert(i+1, l)
|
|
|
|
}
|
2020-06-11 11:13:36 +02:00
|
|
|
continue
|
2020-06-12 14:32:09 +02:00
|
|
|
//s.writeln(file_content)
|
2020-06-11 20:34:59 +02:00
|
|
|
} else if line.contains('@js ') {
|
|
|
|
pos := line.index('@js') or {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-01 21:03:14 +02:00
|
|
|
s.write('<script src="')
|
|
|
|
s.write(line[pos + 5..line.len-1])
|
|
|
|
s.writeln('"></script>')
|
2020-06-11 20:34:59 +02:00
|
|
|
} else if line.contains('@css ') {
|
|
|
|
pos := line.index('@css') or {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-01 21:03:14 +02:00
|
|
|
s.write('<link href="')
|
|
|
|
s.write(line[pos + 6..line.len-1])
|
|
|
|
s.writeln('" rel="stylesheet" type="text/css">')
|
2020-06-10 18:53:04 +02:00
|
|
|
} else 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-09 23:20:48 +02:00
|
|
|
} else if state == .html && line.contains('span.') && line.ends_with('{') {
|
2020-06-08 12:44:27 +02:00
|
|
|
// `span.header {` => `<span class='header'>`
|
|
|
|
class := line.find_between('span.', '{').trim_space()
|
|
|
|
s.writeln('<span class="$class">')
|
2020-06-10 01:43:04 +02:00
|
|
|
in_span = true
|
2020-06-09 23:20:48 +02:00
|
|
|
} else if state == .html && line.contains('.') && line.ends_with('{') {
|
2020-06-07 19:06:02 +02:00
|
|
|
// `.header {` => `<div class='header'>`
|
2020-06-08 12:44:27 +02:00
|
|
|
class := line.find_between('.', '{').trim_space()
|
2019-11-30 11:09:05 +01:00
|
|
|
s.writeln('<div class="$class">')
|
2020-06-09 23:20:48 +02:00
|
|
|
} else if state == .html && line.contains('#') && line.ends_with('{') {
|
2020-06-08 12:44:27 +02:00
|
|
|
// `#header {` => `<div id='header'>`
|
|
|
|
class := line.find_between('#', '{').trim_space()
|
|
|
|
s.writeln('<div id="$class">')
|
2020-06-09 23:20:48 +02:00
|
|
|
} else if state == .html && line == '}' {
|
2020-06-10 01:43:04 +02:00
|
|
|
if in_span {
|
2020-06-08 12:44:27 +02:00
|
|
|
s.writeln('</span>')
|
2020-06-10 01:43:04 +02:00
|
|
|
in_span = false
|
2020-06-08 12:44:27 +02:00
|
|
|
} else {
|
|
|
|
s.writeln('</div>')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// HTML, may include `@var`
|
2020-06-24 22:12:33 +02:00
|
|
|
// escaped by cgen, unless it's a `vweb.RawHtml` string
|
|
|
|
s.writeln(line.replace('@', '$').replace("'", '"'))
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 17:36:09 +02:00
|
|
|
s.writeln(str_end)
|
2020-06-10 12:17:49 +02:00
|
|
|
s.writeln('_tmpl_res_$fn_name := sb.str() ')
|
2020-06-07 12:26:45 +02:00
|
|
|
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()
|
|
|
|
}
|
2020-06-24 22:12:33 +02:00
|
|
|
|