diff --git a/commands/build.v b/commands/build.v index 1f52839..e8b40cd 100644 --- a/commands/build.v +++ b/commands/build.v @@ -45,8 +45,8 @@ fn new_build_cmd() cli.Command { execute: fn (cmd cli.Command) ! { mut logger := log.Log{} logger.set_level(log.Level.info) - config := load_config(commands.default_config)! - build(config, mut logger) or { + conf := load_config(commands.default_config)! + build(conf, mut logger) or { logger.error(err.msg()) println('Build failed') } @@ -99,13 +99,37 @@ fn get_content(path string) !string { 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) ! { // get html body content from md + b.logger.info('start md to html: ${md_path}') content := get_content(md_path)! // want to change from contents to content b.config_map['contents'] = content - html := template.parse(b.template_content, b.config_map) + + // parse template 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) if !os.exists(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 } -fn build(config config.Config, mut logger log.Log) ! { +fn build(conf config.Config, mut logger log.Log) ! { println('Start building') mut sw := time.new_stopwatch() mut b := new_builder(logger) template_content := os.read_file(commands.default_template)! b.template_content = template_content - b.config = config - b.config_map = config.as_map() + b.config = conf + b.config_map = conf.as_map() b.create_dist_dir()! // copy static dir files @@ -166,7 +190,7 @@ fn build(config config.Config, mut logger log.Log) ! { logger.info('start md to html') for path in mds { 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 } b.md2html(path)! diff --git a/commands/serve.v b/commands/serve.v index 8816348..046fcfb 100644 --- a/commands/serve.v +++ b/commands/serve.v @@ -73,7 +73,7 @@ mut: 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{} os.walk_with_context(path, &res, fn (mut res []string, fpath string) { 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) if now > w.time_stamp { - println('modified file: $w.path') + println('modified file: ${w.path}') w.time_stamp = now - build(config, mut logger) or { + build(conf, mut logger) or { logger.error(err.msg()) println('Build failed') } @@ -117,18 +117,18 @@ fn serve(mut logger log.Log) ! { port: commands.cport } - local_base_url := 'http://localhost:$commands.cport/' - mut config := load_config(default_config)! - config.base_url = local_base_url + local_base_url := 'http://localhost:${commands.cport}/' + mut conf := load_config(default_config)! + conf.base_url = local_base_url println(local_base_url) // build before server startup - build(config, mut logger) or { + build(conf, mut logger) or { logger.error(err.msg()) println('Build failed') } - w := go watch('.', config, mut logger) + w := spawn watch('.', conf, mut logger) server.listen_and_serve() w.wait() diff --git a/commands/vss.v b/commands/vss.v index b5dae5c..f63864e 100644 --- a/commands/vss.v +++ b/commands/vss.v @@ -6,7 +6,7 @@ import cli pub fn execute() { mut app := cli.Command{ name: 'vss' - version: '0.1.0' + version: '0.2.0' description: 'static site generator' execute: fn (cmd cli.Command) ! { println(cmd.help_message()) diff --git a/example/layouts/about.html b/example/layouts/about.html new file mode 100644 index 0000000..ce23532 --- /dev/null +++ b/example/layouts/about.html @@ -0,0 +1,13 @@ + + + + + @title + + + + + + + @contents + diff --git a/example/layouts/post/index.html b/example/layouts/post/index.html new file mode 100644 index 0000000..f12fe69 --- /dev/null +++ b/example/layouts/post/index.html @@ -0,0 +1,14 @@ + + + + + @title + + + + + +
Post
+ + @contents + diff --git a/internal/config/config.v b/internal/config/config.v index a8b0be0..d68349c 100644 --- a/internal/config/config.v +++ b/internal/config/config.v @@ -24,8 +24,8 @@ pub mut: pub fn load(toml_text string) !Config { doc := toml.parse_text(toml_text)! - mut config := doc.reflect() - config.build = doc.value('build').reflect() + mut config := doc.reflect[Config]() + config.build = doc.value('build').reflect[Build]() return config }