mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vweb: run vfmt, add it to v test-cleancode
This commit is contained in:
@@ -13,17 +13,15 @@ const (
|
||||
|
||||
// compile_file compiles the content of a file by the given path as a template
|
||||
pub fn compile_file(path string, fn_name string) string {
|
||||
html := os.read_file(path) or {
|
||||
panic('html failed')
|
||||
}
|
||||
html := os.read_file(path) or { panic('html failed') }
|
||||
return compile_template(html, fn_name)
|
||||
}
|
||||
|
||||
enum State {
|
||||
html
|
||||
css // <style>
|
||||
js // <script>
|
||||
//span // span.{
|
||||
js // <script>
|
||||
// span // span.{
|
||||
}
|
||||
|
||||
pub fn compile_template(html_ string, fn_name string) string {
|
||||
@@ -45,15 +43,15 @@ pub fn compile_template(html_ string, fn_name string) string {
|
||||
footer = f.trim_space().replace("\'", '"')
|
||||
html += footer
|
||||
}
|
||||
|
||||
mut lines := html.split_into_lines()
|
||||
lstartlength := lines.len * 30
|
||||
mut s := strings.new_builder(1000)
|
||||
// base := path.all_after_last('/').replace('.html', '')
|
||||
s.writeln("
|
||||
import strings
|
||||
// === vweb html template ===
|
||||
fn vweb_tmpl_${fn_name}() {
|
||||
mut sb := strings.new_builder(${lines.len * 30})\n
|
||||
mut sb := strings.new_builder($lstartlength)\n
|
||||
header := \' \' // TODO remove
|
||||
_ = header
|
||||
footer := \' \' // TODO remove
|
||||
@@ -63,25 +61,21 @@ _ = footer
|
||||
s.write(str_start)
|
||||
mut state := State.html
|
||||
mut in_span := false
|
||||
//for _line in lines {
|
||||
for i := 0; i < lines.len; i ++ {
|
||||
// for _line in lines {
|
||||
for i := 0; i < lines.len; i++ {
|
||||
line := lines[i].trim_space()
|
||||
if line == '<style>' {
|
||||
state = .css
|
||||
} else if line == '</style>' {
|
||||
state = .html
|
||||
}
|
||||
else if line == '<script>' {
|
||||
} else if line == '<script>' {
|
||||
state = .js
|
||||
}
|
||||
else if line == '</script>' {
|
||||
} else if line == '</script>' {
|
||||
state = .html
|
||||
}
|
||||
if line.contains('@include ') && false {
|
||||
// TODO
|
||||
pos := line.index('@include ') or {
|
||||
continue
|
||||
}
|
||||
pos := line.index('@include ') or { continue }
|
||||
file_name := line[pos + 9..]
|
||||
file_path := os.join_path('templates', '${file_name}.html')
|
||||
mut file_content := os.read_file(file_path) or {
|
||||
@@ -90,29 +84,23 @@ _ = footer
|
||||
file_content = file_content.replace("\'", '"')
|
||||
lines2 := file_content.split_into_lines()
|
||||
for l in lines2 {
|
||||
lines.insert(i+1, l)
|
||||
lines.insert(i + 1, l)
|
||||
}
|
||||
continue
|
||||
//s.writeln(file_content)
|
||||
// s.writeln(file_content)
|
||||
} else if line.contains('@js ') {
|
||||
pos := line.index('@js') or {
|
||||
continue
|
||||
}
|
||||
pos := line.index('@js') or { continue }
|
||||
s.write('<script src="')
|
||||
s.write(line[pos + 5..line.len-1])
|
||||
s.write(line[pos + 5..line.len - 1])
|
||||
s.writeln('"></script>')
|
||||
} else if line.contains('@css ') {
|
||||
pos := line.index('@css') or {
|
||||
continue
|
||||
}
|
||||
pos := line.index('@css') or { continue }
|
||||
s.write('<link href="')
|
||||
s.write(line[pos + 6..line.len-1])
|
||||
s.write(line[pos + 6..line.len - 1])
|
||||
s.writeln('" rel="stylesheet" type="text/css">')
|
||||
} else if line.contains('@if ') {
|
||||
s.writeln(str_end)
|
||||
pos := line.index('@if') or {
|
||||
continue
|
||||
}
|
||||
pos := line.index('@if') or { continue }
|
||||
s.writeln('if ' + line[pos + 4..] + '{')
|
||||
s.writeln(str_start)
|
||||
} else if line.contains('@end') {
|
||||
@@ -125,16 +113,14 @@ _ = footer
|
||||
s.writeln(str_start)
|
||||
} else if line.contains('@for') {
|
||||
s.writeln(str_end)
|
||||
pos := line.index('@for') or {
|
||||
continue
|
||||
}
|
||||
pos := line.index('@for') or { continue }
|
||||
s.writeln('for ' + line[pos + 4..] + '{')
|
||||
s.writeln(str_start)
|
||||
} else if state == .html && 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
|
||||
in_span = true
|
||||
} else if state == .html && line.contains('.') && line.ends_with('{') {
|
||||
// `.header {` => `<div class='header'>`
|
||||
class := line.find_between('.', '{').trim_space()
|
||||
@@ -162,4 +148,3 @@ _ = footer
|
||||
s.writeln('// === end of vweb html template ===')
|
||||
return s.str()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user