diff --git a/Taskfile.yml b/Taskfile.yml index a83c843..38fa096 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -30,7 +30,7 @@ tasks: test: desc: Run test cmds: - - v test commands/ + - v test . vet: desc: Report suspicious code constructs @@ -41,8 +41,9 @@ tasks: desc: Format .v files cmds: - v fmt -w *.v - - v fmt -w commands/*.v - - v fmt -w template/*.v + - v fmt -w commands + - v fmt -w internal/template + - v fmt -w internal/config/ clean: desc: Clean test files @@ -61,6 +62,11 @@ tasks: cmds: - v . -o {{.TARGET}} + prod-build: + desc: Build vss for production + cmds: + - v -prod . -o {{.TARGET}} + example: desc: Setup for example cmds: diff --git a/commands/build.v b/commands/build.v index 4e19c91..01f81b3 100644 --- a/commands/build.v +++ b/commands/build.v @@ -4,16 +4,13 @@ import os import cli import log import time -import toml import regex import markdown -import template +import internal.template +import internal.config const default_config = 'config.toml' -// Allowed parameters -const config_params = ['title', 'description', 'baseUrl'] - const default_template = 'layouts/index.html' const defautl_static = 'static' @@ -43,19 +40,6 @@ fn read_file(filename string) ?string { return contents } -fn get_config_map() ?map[string]string { - mut config_map := map[string]string{} - - // https://modules.vlang.io/toml.html - toml_str := read_file(commands.default_config)? - config := toml.parse_text(toml_str)? - for param in commands.config_params { - v := config.value_opt(param) or { continue } - config_map[param] = v.string() - } - return config_map -} - fn get_html_path(md_path string) string { mut file_name := os.file_name(md_path) file_name = file_name.replace('.md', '.html') @@ -112,11 +96,19 @@ fn build(mut logger log.Log) ? { } template_content := os.read_file(commands.default_template)? - mut config_map := get_config_map()? + + toml_text := read_file(commands.default_config)? + config := config.load(toml_text)? + mut config_map := config.as_map() md_paths := normalise_paths(os.walk_ext('.', '.md')) logger.info('start md to html') for path in md_paths { + file_name := os.file_name(path) + if file_name in 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) diff --git a/commands/build_test.v b/commands/build_test.v index f070b99..f7d253e 100644 --- a/commands/build_test.v +++ b/commands/build_test.v @@ -1,7 +1,5 @@ module commands -import os - fn test_get_html_filename() { test_path := 'index.md' mut html_name := get_html_path(test_path) diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..93fb8e7 --- /dev/null +++ b/example/README.md @@ -0,0 +1,3 @@ +# Example project + +This is vss example project. diff --git a/example/config.toml b/example/config.toml index 0048965..7cb55ce 100644 --- a/example/config.toml +++ b/example/config.toml @@ -1,6 +1,6 @@ title = "Open Sea" description = "Takumi Tsuruta's home page" -# baseUrl = 'https://zztkm.github.io/vss/' +# base_url = 'https://zztkm.github.io/vss/' [build] -ignoreFiles = ["ignore.md", "README.md"] \ No newline at end of file +ignore_files = ["ignore.md", "README.md"] \ No newline at end of file diff --git a/internal/config/config.v b/internal/config/config.v new file mode 100644 index 0000000..7876f07 --- /dev/null +++ b/internal/config/config.v @@ -0,0 +1,40 @@ +module config + +import toml + +// template_params list of field names to convert as_map +const template_params = ['title', 'description', 'base_url'] + +// Build settings for build +struct Build { +pub mut: + ignore_files []string +} + +// Config general settings for vss +struct Config { +pub mut: + build Build + title string + description string + base_url string +} + +// load +pub fn load(toml_text string) ?Config { + doc := toml.parse_text(toml_text)? + + mut config := doc.reflect() + config.build = doc.value('build').reflect() + + return config +} + +// as_map for template.parse +pub fn (c Config) as_map() map[string]string { + mut mp := map[string]string{} + mp['title'] = c.title + mp['description'] = c.description + mp['base_url'] = c.base_url + return mp +} diff --git a/internal/config/config_test.v b/internal/config/config_test.v new file mode 100644 index 0000000..968ce49 --- /dev/null +++ b/internal/config/config_test.v @@ -0,0 +1,22 @@ +module config + +const toml_text = '# for test +title = "test site" +description = "test page" +base_url = "https://vss.github.io/vss/" + +[build] +ignore_files = ["ignore.md", "README.md"] +' + +fn test_load() { + config := load(config.toml_text) or { + eprintln(err) + return + } + + assert config.title == 'test site' + assert config.description == 'test page' + assert config.base_url == 'https://vss.github.io/vss/' + assert config.build.ignore_files == ['ignore.md', 'README.md'] +} diff --git a/template/template.v b/internal/template/template.v similarity index 100% rename from template/template.v rename to internal/template/template.v