feat: applied the new layout system.

issue: #37
main
zztkm 1 year ago
parent 2c82da969e
commit 2d0745233d

@ -45,8 +45,8 @@ fn new_build_cmd() cli.Command {
execute: fn (cmd cli.Command) ! { execute: fn (cmd cli.Command) ! {
mut logger := log.Log{} mut logger := log.Log{}
logger.set_level(log.Level.info) logger.set_level(log.Level.info)
config := load_config(commands.default_config)! conf := load_config(commands.default_config)!
build(config, mut logger) or { build(conf, mut logger) or {
logger.error(err.msg()) logger.error(err.msg())
println('Build failed') println('Build failed')
} }
@ -99,13 +99,32 @@ fn get_content(path string) !string {
return markdown.to_html(md) return markdown.to_html(md)
} }
fn check_layout(path string) bool {
// check if layout is specified in front matter
// if not, use default layout
// if specified, check if layout file exists
// if not, return error
return true
}
fn (mut b Builder) md2html(md_path string) ! { fn (mut b Builder) md2html(md_path string) ! {
// get html body content from md // get html body content from md
b.logger.info('start md to html: ${md_path}')
content := get_content(md_path)! content := get_content(md_path)!
// want to change from contents to content // want to change from contents to content
b.config_map['contents'] = content b.config_map['contents'] = content
html := template.parse(b.template_content, b.config_map) // parse template
html_path := get_html_path(md_path) html_path := get_html_path(md_path)
mut template_content := ''
if os.exists('layouts/${html_path}') {
b.logger.info('use custom template: layouts/${html_path}')
template_content = os.read_file('layouts/${html_path}')!
} else {
b.logger.info('use default template')
template_content = b.template_content
}
html := template.parse(template_content, b.config_map)
dist_path := os.join_path(b.dist, html_path) dist_path := os.join_path(b.dist, html_path)
if !os.exists(os.dir(dist_path)) { if !os.exists(os.dir(dist_path)) {
os.mkdir_all(os.dir(dist_path))! os.mkdir_all(os.dir(dist_path))!
@ -148,14 +167,14 @@ fn (mut b Builder) is_ignore(path string) bool {
return false return false
} }
fn build(config config.Config, mut logger log.Log) ! { fn build(conf config.Config, mut logger log.Log) ! {
println('Start building') println('Start building')
mut sw := time.new_stopwatch() mut sw := time.new_stopwatch()
mut b := new_builder(logger) mut b := new_builder(logger)
template_content := os.read_file(commands.default_template)! template_content := os.read_file(commands.default_template)!
b.template_content = template_content b.template_content = template_content
b.config = config b.config = conf
b.config_map = config.as_map() b.config_map = conf.as_map()
b.create_dist_dir()! b.create_dist_dir()!
// copy static dir files // copy static dir files

@ -73,7 +73,7 @@ mut:
time_stamp i64 time_stamp i64
} }
fn watch(path string, config config.Config, mut logger log.Log) { fn watch(path string, conf config.Config, mut logger log.Log) {
mut res := []string{} mut res := []string{}
os.walk_with_context(path, &res, fn (mut res []string, fpath string) { os.walk_with_context(path, &res, fn (mut res []string, fpath string) {
res << fpath res << fpath
@ -99,7 +99,7 @@ fn watch(path string, config config.Config, mut logger log.Log) {
println('modified file: ${w.path}') println('modified file: ${w.path}')
w.time_stamp = now w.time_stamp = now
build(config, mut logger) or { build(conf, mut logger) or {
logger.error(err.msg()) logger.error(err.msg())
println('Build failed') println('Build failed')
} }
@ -118,17 +118,17 @@ fn serve(mut logger log.Log) ! {
} }
local_base_url := 'http://localhost:${commands.cport}/' local_base_url := 'http://localhost:${commands.cport}/'
mut config := load_config(default_config)! mut conf := load_config(default_config)!
config.base_url = local_base_url conf.base_url = local_base_url
println(local_base_url) println(local_base_url)
// build before server startup // build before server startup
build(config, mut logger) or { build(conf, mut logger) or {
logger.error(err.msg()) logger.error(err.msg())
println('Build failed') println('Build failed')
} }
w := spawn watch('.', config, mut logger) w := spawn watch('.', conf, mut logger)
server.listen_and_serve() server.listen_and_serve()
w.wait() w.wait()

@ -6,7 +6,7 @@ import cli
pub fn execute() { pub fn execute() {
mut app := cli.Command{ mut app := cli.Command{
name: 'vss' name: 'vss'
version: '0.1.0' version: '0.2.0'
description: 'static site generator' description: 'static site generator'
execute: fn (cmd cli.Command) ! { execute: fn (cmd cli.Command) ! {
println(cmd.help_message()) println(cmd.help_message())

@ -0,0 +1,13 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>@title</title>
<base href="@base_url">
<meta name="description" content="@description" />
<link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet" />
</head>
<body>
@contents
</body>
Loading…
Cancel
Save