diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 128215b..e2db91a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,14 +22,11 @@ jobs: with: check-latest: true - - name: Install Task - uses: arduino/setup-task@v1 - - name: Checkout ${{ github.event.repository.name }} uses: actions/checkout@v2 - name: Install deps - run: task setup + run: v install - name: Check if code is formatted run: | @@ -37,7 +34,7 @@ jobs: v fmt -verify . - name: Build ${{ github.event.repository.name }} - run: task build + run: v . - name: Run Tests - run: task test \ No newline at end of file + run: v test . \ No newline at end of file diff --git a/commands/build.v b/commands/build.v index 15d4d94..1f52839 100644 --- a/commands/build.v +++ b/commands/build.v @@ -32,6 +32,8 @@ mut: fn new_builder(logger log.Log) Builder { return Builder{ logger: logger + dist: commands.default_dist + static_dir: commands.defautl_static } } @@ -40,10 +42,11 @@ fn new_build_cmd() cli.Command { name: 'build' description: 'build your site' usage: 'vss build' - execute: fn (cmd cli.Command) ? { + execute: fn (cmd cli.Command) ! { mut logger := log.Log{} logger.set_level(log.Level.info) - build(mut logger) or { + config := load_config(commands.default_config)! + build(config, mut logger) or { logger.error(err.msg()) println('Build failed') } @@ -51,11 +54,6 @@ fn new_build_cmd() cli.Command { } } -fn read_file(filename string) ?string { - contents := os.read_file(filename.trim_space())? - return contents -} - fn get_html_path(md_path string) string { mut file_name := os.file_name(md_path) file_name = file_name.replace('.md', '.html') @@ -75,10 +73,10 @@ fn normalise_paths(paths []string) []string { } // pre_proc_md_to_html convert markdown relative links to html relative links -fn pre_proc_md_to_html(contents string) ?string { +fn pre_proc_md_to_html(contents string) !string { lines := contents.split_into_lines() mut parsed_lines := []string{len: lines.len} - mut re := regex.regex_opt(r'\[.+\]\(.+\.md\)')? + mut re := regex.regex_opt(r'\[.+\]\(.+\.md\)') or { return err } for i, line in contents.split_into_lines() { start, end := re.find(line) @@ -91,56 +89,52 @@ 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)? +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)? +fn get_content(path string) !string { + md := get_md_content(path)! return markdown.to_html(md) } -fn (mut b Builder) md2html(md_path string) ? { +fn (mut b Builder) md2html(md_path string) ! { // get html body content from md - content := get_content(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) 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.mkdir_all(os.dir(dist_path))! } - os.write_file(dist_path, html)? + 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() +// load_config loads a toml config file +fn load_config(toml_file string) !config.Config { + toml_text := os.read_file(toml_file)! + return config.load(toml_text) } -fn (b Builder) copy_static() ? { +// copy_static copy static files to dist +fn (b Builder) copy_static() ! { if os.exists(b.static_dir) { - os.cp_all(b.static_dir, b.dist, false)? + os.cp_all(b.static_dir, b.dist, false)! } } -fn (mut b Builder) create_dist_dir() ? { +// create_dist_dir create build output destination +fn (mut b Builder) create_dist_dir() ! { if os.exists(b.dist) { b.logger.info('re-create dist dir') - os.rmdir_all(b.dist)? - os.mkdir_all(b.dist)? + os.rmdir_all(b.dist)! + os.mkdir_all(b.dist)! } else { b.logger.info('create dist dir') - os.mkdir_all(b.dist)? + os.mkdir_all(b.dist)! } } @@ -154,15 +148,19 @@ fn (mut b Builder) is_ignore(path string) bool { return false } -fn build(mut logger log.Log) ? { +fn build(config config.Config, mut logger log.Log) ! { println('Start building') mut sw := time.new_stopwatch() mut b := new_builder(logger) - b.load_config()? - b.create_dist_dir()? + template_content := os.read_file(commands.default_template)! + b.template_content = template_content + b.config = config + b.config_map = config.as_map() + + b.create_dist_dir()! // copy static dir files logger.info('copy static files') - b.copy_static()? + b.copy_static()! mds := normalise_paths(os.walk_ext('.', '.md')) logger.info('start md to html') @@ -171,7 +169,7 @@ fn build(mut logger log.Log) ? { logger.info('$path is included in ignore_files, skip build') continue } - b.md2html(path)? + b.md2html(path)! } logger.info('end md to html') diff --git a/commands/serve.v b/commands/serve.v index b635fc4..8816348 100644 --- a/commands/serve.v +++ b/commands/serve.v @@ -4,6 +4,7 @@ import cli import log import net.http import os +import internal.config const cport = 8080 @@ -12,7 +13,7 @@ fn new_serve_cmd() cli.Command { name: 'serve' description: 'serve dist' usage: 'vss serve' - execute: fn (cmd cli.Command) ? { + execute: fn (cmd cli.Command) ! { mut logger := log.Log{} logger.set_level(log.Level.info) serve(mut logger) or { @@ -72,7 +73,7 @@ mut: time_stamp i64 } -fn watch(path string, mut logger log.Log) { +fn watch(path string, config config.Config, mut logger log.Log) { mut res := []string{} os.walk_with_context(path, &res, fn (mut res []string, fpath string) { res << fpath @@ -98,7 +99,7 @@ fn watch(path string, mut logger log.Log) { println('modified file: $w.path') w.time_stamp = now - build(mut logger) or { + build(config, mut logger) or { logger.error(err.msg()) println('Build failed') } @@ -107,7 +108,7 @@ fn watch(path string, mut logger log.Log) { } } -fn serve(mut logger log.Log) ? { +fn serve(mut logger log.Log) ! { mut handler := MyHttpHandler{ root: 'dist' } @@ -115,9 +116,20 @@ fn serve(mut logger log.Log) ? { handler: handler port: commands.cport } - println('http://localhost:$commands.cport') - w := go watch('.', mut logger) - server.listen_and_serve() or { panic(err) } + + local_base_url := 'http://localhost:$commands.cport/' + mut config := load_config(default_config)! + config.base_url = local_base_url + println(local_base_url) + + // build before server startup + build(config, mut logger) or { + logger.error(err.msg()) + println('Build failed') + } + + w := go watch('.', config, mut logger) + server.listen_and_serve() w.wait() } diff --git a/commands/vss.v b/commands/vss.v index 4cea76a..5728079 100644 --- a/commands/vss.v +++ b/commands/vss.v @@ -8,7 +8,7 @@ pub fn execute() { name: 'vss' version: '0.0.12' description: 'static site generator' - execute: fn (cmd cli.Command) ? { + execute: fn (cmd cli.Command) ! { println(cmd.help_message()) } } diff --git a/example/config.toml b/example/config.toml index 7cb55ce..a65f338 100644 --- a/example/config.toml +++ b/example/config.toml @@ -1,6 +1,6 @@ title = "Open Sea" description = "Takumi Tsuruta's home page" -# base_url = 'https://zztkm.github.io/vss/' +base_url = 'https://zztkm.github.io/vss/' [build] ignore_files = ["ignore.md", "README.md"] \ No newline at end of file diff --git a/example/layouts/index.html b/example/layouts/index.html index 98ebf7c..1901af5 100644 --- a/example/layouts/index.html +++ b/example/layouts/index.html @@ -3,7 +3,7 @@ @title - + diff --git a/internal/config/config.v b/internal/config/config.v index 618b028..a8b0be0 100644 --- a/internal/config/config.v +++ b/internal/config/config.v @@ -21,8 +21,8 @@ pub mut: } // load -pub fn load(toml_text string) ?Config { - doc := toml.parse_text(toml_text)? +pub fn load(toml_text string) !Config { + doc := toml.parse_text(toml_text)! mut config := doc.reflect() config.build = doc.value('build').reflect() diff --git a/v.mod b/v.mod index a68f788..4ffec4c 100644 --- a/v.mod +++ b/v.mod @@ -3,5 +3,5 @@ Module { description: 'static site generator' version: '0.0.12' license: 'MIT' - dependencies: [] + dependencies: ['markdown'] }