From 2c82da969ee41b1d38a1a9f25a582fb8bc0c9b72 Mon Sep 17 00:00:00 2001 From: zztkm Date: Wed, 17 May 2023 23:15:31 +0900 Subject: [PATCH 1/3] fmt: v fmt --- commands/build.v | 2 +- commands/serve.v | 6 +++--- internal/config/config.v | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/commands/build.v b/commands/build.v index 1f52839..cfa0052 100644 --- a/commands/build.v +++ b/commands/build.v @@ -166,7 +166,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..6c3655f 100644 --- a/commands/serve.v +++ b/commands/serve.v @@ -96,7 +96,7 @@ 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 { @@ -117,7 +117,7 @@ fn serve(mut logger log.Log) ! { port: commands.cport } - local_base_url := 'http://localhost:$commands.cport/' + local_base_url := 'http://localhost:${commands.cport}/' mut config := load_config(default_config)! config.base_url = local_base_url println(local_base_url) @@ -128,7 +128,7 @@ fn serve(mut logger log.Log) ! { println('Build failed') } - w := go watch('.', config, mut logger) + w := spawn watch('.', config, mut logger) server.listen_and_serve() w.wait() 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 } From 2d0745233d0a811d2b07431de45eb62cee212dbc Mon Sep 17 00:00:00 2001 From: zztkm Date: Thu, 18 May 2023 00:11:16 +0900 Subject: [PATCH 2/3] feat: applied the new layout system. issue: #37 --- commands/build.v | 31 +++++++++++++++++++++++++------ commands/serve.v | 12 ++++++------ commands/vss.v | 2 +- example/layouts/about.html | 13 +++++++++++++ 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 example/layouts/about.html diff --git a/commands/build.v b/commands/build.v index cfa0052..ed8257a 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,32 @@ 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) + 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) if !os.exists(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 } -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 diff --git a/commands/serve.v b/commands/serve.v index 6c3655f..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 @@ -99,7 +99,7 @@ fn watch(path string, config config.Config, mut logger log.Log) { 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') } @@ -118,17 +118,17 @@ fn serve(mut logger log.Log) ! { } local_base_url := 'http://localhost:${commands.cport}/' - mut config := load_config(default_config)! - config.base_url = local_base_url + 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 := spawn 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 + From dc848ecc283b39c86fcd6ed00eeb6055713fcb7b Mon Sep 17 00:00:00 2001 From: zztkm Date: Thu, 18 May 2023 23:25:32 +0900 Subject: [PATCH 3/3] feat: added handling of cases where only the directory --- commands/build.v | 5 +++++ example/layouts/post/index.html | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 example/layouts/post/index.html diff --git a/commands/build.v b/commands/build.v index ed8257a..e8b40cd 100644 --- a/commands/build.v +++ b/commands/build.v @@ -113,12 +113,17 @@ fn (mut b Builder) md2html(md_path string) ! { content := get_content(md_path)! // want to change from contents to content b.config_map['contents'] = content + // 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 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 +