From 2c330a151e3ea2ed2ea7db040739e471ac7f8709 Mon Sep 17 00:00:00 2001 From: zztkm Date: Thu, 29 Sep 2022 02:33:31 +0900 Subject: [PATCH 1/4] internal.config: change Config struct to public --- internal/config/config.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/config/config.v b/internal/config/config.v index 7876f07..618b028 100644 --- a/internal/config/config.v +++ b/internal/config/config.v @@ -12,7 +12,7 @@ pub mut: } // Config general settings for vss -struct Config { +pub struct Config { pub mut: build Build title string From 72087e921a02ec087592b5cd22a9a0e83cc670d3 Mon Sep 17 00:00:00 2001 From: zztkm Date: Thu, 29 Sep 2022 04:08:39 +0900 Subject: [PATCH 2/4] commands: refactor build --- commands/build.v | 99 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 28 deletions(-) diff --git a/commands/build.v b/commands/build.v index 01f81b3..2cd6f82 100644 --- a/commands/build.v +++ b/commands/build.v @@ -19,6 +19,19 @@ const default_index = 'index.md' const default_dist = 'dist' +struct Builder { +mut: + config config.Config + dist string + static_dir string + template_content string + config_map map[string]string +} + +fn new_builder() Builder { + return Builder{} +} + fn new_build_cmd() cli.Command { return cli.Command{ name: 'build' @@ -75,51 +88,81 @@ fn pre_proc_md_to_html(contents string) ?string { return parsed_lines.join('\n') } +fn get_md_content(path string) ?string { + md := os.read_file(path)? + return pre_proc_md_to_html(md) +} + +fn get_content(path string) ?string { + md := get_md_content(path)? + return markdown.to_html(md) +} + +fn (mut b Builder) md2html(md_path string) ? { + // get html body content from md + 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) + html_path := get_html_path(md_path) + dist_path := os.join_path(b.dist, html_path) + if !os.exists(os.dir(dist_path)) { + os.mkdir_all(os.dir(dist_path))? + } + os.write_file(dist_path, html)? +} + +fn (mut b Builder) load_config() ? { + toml_text := read_file(commands.default_config)? + config := config.load(toml_text)? + template_content := os.read_file(commands.default_template)? + + b.config = config + b.dist = commands.default_dist + b.static_dir = commands.defautl_static + b.template_content = template_content + b.config_map = config.as_map() +} + +fn (b Builder) copy_static() ? { + if os.exists(b.static_dir) { + os.cp_all(b.static_dir, b.dist, false)? + } +} + fn build(mut logger log.Log) ? { println('Start building') mut sw := time.new_stopwatch() - dist := commands.default_dist - if os.exists(dist) { + mut b := new_builder() + + // load config for build + b.load_config()? + + if os.exists(b.dist) { logger.info('re-create dist dir') - os.rmdir_all(dist)? - os.mkdir_all(dist)? + os.rmdir_all(b.dist)? + os.mkdir_all(b.dist)? } else { logger.info('create dist dir') - os.mkdir_all(dist)? + os.mkdir_all(b.dist)? } - // copy static files - if os.exists(commands.defautl_static) { - logger.info('copy static files') - os.cp_all(commands.defautl_static, dist, false)? - } - - template_content := os.read_file(commands.default_template)? - - toml_text := read_file(commands.default_config)? - config := config.load(toml_text)? - mut config_map := config.as_map() + // copy static dir files + logger.info('copy static files') + b.copy_static()? md_paths := normalise_paths(os.walk_ext('.', '.md')) logger.info('start md to html') for path in md_paths { + // e.g. README.md file_name := os.file_name(path) - if file_name in config.build.ignore_files { + // notify user that build was skipped + if file_name in b.config.build.ignore_files { logger.info('$file_name is included in ignore_files, skip build') continue } - mut md := os.read_file(path)? - md = pre_proc_md_to_html(md)? - contents := markdown.to_html(md) - config_map['contents'] = contents - html := template.parse(template_content, config_map) - html_path := get_html_path(path) - dist_path := os.join_path(dist, html_path) - if !os.exists(os.dir(dist_path)) { - os.mkdir_all(os.dir(dist_path))? - } - os.write_file(dist_path, html)? + b.md2html(path)? } logger.info('end md to html') From 24edec77821f11f0cbdc4c0c3220938072634030 Mon Sep 17 00:00:00 2001 From: zztkm Date: Thu, 29 Sep 2022 04:20:54 +0900 Subject: [PATCH 3/4] more refactor --- commands/build.v | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/commands/build.v b/commands/build.v index 2cd6f82..77059f8 100644 --- a/commands/build.v +++ b/commands/build.v @@ -22,14 +22,17 @@ const default_dist = 'dist' struct Builder { mut: config config.Config + logger log.Log dist string static_dir string template_content string config_map map[string]string } -fn new_builder() Builder { - return Builder{} +fn new_builder(logger log.Log) Builder { + return Builder{ + logger: logger + } } fn new_build_cmd() cli.Command { @@ -130,31 +133,30 @@ fn (b Builder) copy_static() ? { } } -fn build(mut logger log.Log) ? { - println('Start building') - mut sw := time.new_stopwatch() - - mut b := new_builder() - - // load config for build - b.load_config()? - +fn (mut b Builder) create_dist_dir() ? { if os.exists(b.dist) { - logger.info('re-create dist dir') + b.logger.info('re-create dist dir') os.rmdir_all(b.dist)? os.mkdir_all(b.dist)? } else { - logger.info('create dist dir') + b.logger.info('create dist dir') os.mkdir_all(b.dist)? } +} +fn build(mut logger log.Log) ? { + println('Start building') + mut sw := time.new_stopwatch() + mut b := new_builder(logger) + b.load_config()? + b.create_dist_dir()? // copy static dir files logger.info('copy static files') b.copy_static()? - md_paths := normalise_paths(os.walk_ext('.', '.md')) + mds := normalise_paths(os.walk_ext('.', '.md')) logger.info('start md to html') - for path in md_paths { + for path in mds { // e.g. README.md file_name := os.file_name(path) // notify user that build was skipped From c1b3d75f2e648b9ee60ceeec7dfb4ee336494388 Mon Sep 17 00:00:00 2001 From: zztkm Date: Thu, 29 Sep 2022 12:34:08 +0900 Subject: [PATCH 4/4] add is_ignore method --- commands/build.v | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/commands/build.v b/commands/build.v index 77059f8..15d4d94 100644 --- a/commands/build.v +++ b/commands/build.v @@ -144,6 +144,16 @@ fn (mut b Builder) create_dist_dir() ? { } } +fn (mut b Builder) is_ignore(path string) bool { + // e.g. README.md + file_name := os.file_name(path) + // notify user that build was skipped + if file_name in b.config.build.ignore_files { + return true + } + return false +} + fn build(mut logger log.Log) ? { println('Start building') mut sw := time.new_stopwatch() @@ -157,11 +167,8 @@ fn build(mut logger log.Log) ? { mds := normalise_paths(os.walk_ext('.', '.md')) logger.info('start md to html') for path in mds { - // e.g. README.md - file_name := os.file_name(path) - // notify user that build was skipped - if file_name in b.config.build.ignore_files { - logger.info('$file_name is included in ignore_files, skip build') + if b.is_ignore(path) { + logger.info('$path is included in ignore_files, skip build') continue } b.md2html(path)?