vweb: use state enum

pull/5311/head
Alexander Medvednikov 2020-06-09 23:20:48 +02:00
parent 4fdb33bb0a
commit e5aba94ae4
1 changed files with 23 additions and 11 deletions

View File

@ -19,6 +19,13 @@ pub fn compile_file(path, fn_name string) string {
return compile_template(html, fn_name) return compile_template(html, fn_name)
} }
enum State {
html
css // <style>
js // <script>
span // span.{
}
pub fn compile_template(content, fn_name string) string { pub fn compile_template(content, fn_name string) string {
// lines := os.read_lines(path) // lines := os.read_lines(path)
mut html := content mut html := content
@ -43,14 +50,19 @@ pub fn compile_template(content, fn_name string) string {
//footer := \'footer\' //footer := \'footer\'
") ")
s.writeln(str_start) s.writeln(str_start)
mut in_css := false // false mut state := State.html
mut in_span := false
for _line in lines { for _line in lines {
line := _line.trim_space() line := _line.trim_space()
if line == '<style>' { if line == '<style>' {
in_css = true state = .css
} else if line == '</style>' { } else if line == '</style>' {
// in_css = false state = .html
}
else if line == '<script>' {
state = .js
}
else if line == '</script>' {
state = .html
} }
if line.contains('@if ') { if line.contains('@if ') {
s.writeln(str_end) s.writeln(str_end)
@ -74,23 +86,23 @@ pub fn compile_template(content, fn_name string) string {
} }
s.writeln('for ' + line[pos + 4..] + '{') s.writeln('for ' + line[pos + 4..] + '{')
s.writeln(str_start) s.writeln(str_start)
} else if !in_css && line.contains('span.') && line.ends_with('{') { } else if state == .html && line.contains('span.') && line.ends_with('{') {
// `span.header {` => `<span class='header'>` // `span.header {` => `<span class='header'>`
class := line.find_between('span.', '{').trim_space() class := line.find_between('span.', '{').trim_space()
s.writeln('<span class="$class">') s.writeln('<span class="$class">')
in_span = true state = .span
} else if !in_css && line.contains('.') && line.ends_with('{') { } else if state == .html && line.contains('.') && line.ends_with('{') {
// `.header {` => `<div class='header'>` // `.header {` => `<div class='header'>`
class := line.find_between('.', '{').trim_space() class := line.find_between('.', '{').trim_space()
s.writeln('<div class="$class">') s.writeln('<div class="$class">')
} else if !in_css && line.contains('#') && line.ends_with('{') { } else if state == .html && line.contains('#') && line.ends_with('{') {
// `#header {` => `<div id='header'>` // `#header {` => `<div id='header'>`
class := line.find_between('#', '{').trim_space() class := line.find_between('#', '{').trim_space()
s.writeln('<div id="$class">') s.writeln('<div id="$class">')
} else if !in_css && line == '}' { } else if state == .html && line == '}' {
if in_span { if state == .span {
s.writeln('</span>') s.writeln('</span>')
in_span = false state = .html
} else { } else {
s.writeln('</div>') s.writeln('</div>')
} }