Merge pull request #49 from vssio/feat#37

feat: applied the new layout system.
main
zztkm 1 year ago committed by GitHub
commit 52ebd47d3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,37 @@ 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)
dir := os.dir(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 if os.exists('layouts/${dir}/index.html') {
b.logger.info('use custom template: layouts/${dir}/index.html')
template_content = os.read_file('layouts/${dir}/index.html')!
} 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 +172,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
@ -166,7 +190,7 @@ fn build(config config.Config, mut logger log.Log) ! {
logger.info('start md to html') logger.info('start md to html')
for path in mds { for path in mds {
if b.is_ignore(path) { if b.is_ignore(path) {
logger.info('$path is included in ignore_files, skip build') logger.info('${path} is included in ignore_files, skip build')
continue continue
} }
b.md2html(path)! b.md2html(path)!

@ -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
@ -96,10 +96,10 @@ fn watch(path string, config config.Config, mut logger log.Log) {
} }
now := os.file_last_mod_unix(w.path) now := os.file_last_mod_unix(w.path)
if now > w.time_stamp { if now > w.time_stamp {
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')
} }
@ -117,18 +117,18 @@ fn serve(mut logger log.Log) ! {
port: commands.cport port: commands.cport
} }
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 := go 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>

@ -0,0 +1,14 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>@title</title>
<base href="@base_url">
<meta name="description" content="@description" />
</head>
<body>
<header> Post </header>
@contents
</body>

@ -24,8 +24,8 @@ pub mut:
pub fn load(toml_text string) !Config { pub fn load(toml_text string) !Config {
doc := toml.parse_text(toml_text)! doc := toml.parse_text(toml_text)!
mut config := doc.reflect<Config>() mut config := doc.reflect[Config]()
config.build = doc.value('build').reflect<Build>() config.build = doc.value('build').reflect[Build]()
return config return config
} }

Loading…
Cancel
Save