fix #36 rewrite base_ur l at local server startup

In addition, replace Optional with Result.
https://github.com/vlang/v/blob/master/doc/docs.md#optionresult-types-and-error-handling
This commit is contained in:
zztkm 2022-11-02 14:00:34 +09:00
parent 29e8082a41
commit bd2e1428a8
4 changed files with 58 additions and 48 deletions

View file

@ -32,6 +32,8 @@ mut:
fn new_builder(logger log.Log) Builder { fn new_builder(logger log.Log) Builder {
return Builder{ return Builder{
logger: logger logger: logger
dist: commands.default_dist
static_dir: commands.defautl_static
} }
} }
@ -40,10 +42,11 @@ fn new_build_cmd() cli.Command {
name: 'build' name: 'build'
description: 'build your site' description: 'build your site'
usage: 'vss build' usage: 'vss build'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
mut logger := log.Log{} mut logger := log.Log{}
logger.set_level(log.Level.info) 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()) logger.error(err.msg())
println('Build failed') 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 { fn get_html_path(md_path string) string {
mut file_name := os.file_name(md_path) mut file_name := os.file_name(md_path)
file_name = file_name.replace('.md', '.html') 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 // 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() lines := contents.split_into_lines()
mut parsed_lines := []string{len: lines.len} 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() { for i, line in contents.split_into_lines() {
start, end := re.find(line) start, end := re.find(line)
@ -91,56 +89,52 @@ fn pre_proc_md_to_html(contents string) ?string {
return parsed_lines.join('\n') return parsed_lines.join('\n')
} }
fn get_md_content(path string) ?string { fn get_md_content(path string) !string {
md := os.read_file(path)? md := os.read_file(path)!
return pre_proc_md_to_html(md) return pre_proc_md_to_html(md)
} }
fn get_content(path string) ?string { fn get_content(path string) !string {
md := get_md_content(path)? md := get_md_content(path)!
return markdown.to_html(md) 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 // get html body content from md
content := get_content(md_path)? content := get_content(md_path)!
// want to change from contents to content // want to change from contents to content
b.config_map['contents'] = content b.config_map['contents'] = content
html := template.parse(b.template_content, b.config_map) html := template.parse(b.template_content, b.config_map)
html_path := get_html_path(md_path) html_path := get_html_path(md_path)
dist_path := os.join_path(b.dist, html_path) dist_path := os.join_path(b.dist, html_path)
if !os.exists(os.dir(dist_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() ? { // load_config loads a toml config file
toml_text := read_file(commands.default_config)? fn load_config(toml_file string) !config.Config {
config := config.load(toml_text)? toml_text := os.read_file(toml_file)!
template_content := os.read_file(commands.default_template)? return config.load(toml_text)
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() ? { // copy_static copy static files to dist
fn (b Builder) copy_static() ! {
if os.exists(b.static_dir) { 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) { if os.exists(b.dist) {
b.logger.info('re-create dist dir') b.logger.info('re-create dist dir')
os.rmdir_all(b.dist)? os.rmdir_all(b.dist)!
os.mkdir_all(b.dist)? os.mkdir_all(b.dist)!
} else { } else {
b.logger.info('create dist dir') 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 return false
} }
fn build(mut logger log.Log) ? { fn build(config config.Config, mut logger log.Log) ! {
println('Start building') println('Start building')
mut sw := time.new_stopwatch() mut sw := time.new_stopwatch()
mut b := new_builder(logger) mut b := new_builder(logger)
b.load_config()? template_content := os.read_file(commands.default_template)!
b.create_dist_dir()? b.template_content = template_content
b.config = config
b.config_map = config.as_map()
b.create_dist_dir()!
// copy static dir files // copy static dir files
logger.info('copy static files') logger.info('copy static files')
b.copy_static()? b.copy_static()!
mds := normalise_paths(os.walk_ext('.', '.md')) mds := normalise_paths(os.walk_ext('.', '.md'))
logger.info('start md to html') 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') logger.info('$path is included in ignore_files, skip build')
continue continue
} }
b.md2html(path)? b.md2html(path)!
} }
logger.info('end md to html') logger.info('end md to html')

View file

@ -4,6 +4,7 @@ import cli
import log import log
import net.http import net.http
import os import os
import internal.config
const cport = 8080 const cport = 8080
@ -12,7 +13,7 @@ fn new_serve_cmd() cli.Command {
name: 'serve' name: 'serve'
description: 'serve dist' description: 'serve dist'
usage: 'vss serve' usage: 'vss serve'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
mut logger := log.Log{} mut logger := log.Log{}
logger.set_level(log.Level.info) logger.set_level(log.Level.info)
serve(mut logger) or { serve(mut logger) or {
@ -72,7 +73,7 @@ mut:
time_stamp i64 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{} mut res := []string{}
os.walk_with_context(path, &res, fn (mut res []string, fpath string) { os.walk_with_context(path, &res, fn (mut res []string, fpath string) {
res << fpath res << fpath
@ -98,7 +99,7 @@ fn watch(path string, mut logger log.Log) {
println('modified file: $w.path') println('modified file: $w.path')
w.time_stamp = now w.time_stamp = now
build(mut logger) or { build(config, mut logger) or {
logger.error(err.msg()) logger.error(err.msg())
println('Build failed') 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{ mut handler := MyHttpHandler{
root: 'dist' root: 'dist'
} }
@ -115,9 +116,20 @@ fn serve(mut logger log.Log) ? {
handler: handler handler: handler
port: commands.cport port: commands.cport
} }
println('http://localhost:$commands.cport')
w := go watch('.', mut logger) local_base_url := 'http://localhost:$commands.cport/'
server.listen_and_serve() or { panic(err) } 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() w.wait()
} }

View file

@ -8,7 +8,7 @@ pub fn execute() {
name: 'vss' name: 'vss'
version: '0.0.12' version: '0.0.12'
description: 'static site generator' description: 'static site generator'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ! {
println(cmd.help_message()) println(cmd.help_message())
} }
} }

View file

@ -21,8 +21,8 @@ pub mut:
} }
// load // load
pub fn load(toml_text string) ?Config { pub fn load(toml_text string) !Config {
doc := toml.parse_text(toml_text)? doc := toml.parse_text(toml_text)!
mut config := doc.reflect<Config>() mut config := doc.reflect<Config>()
config.build = doc.value('build').reflect<Build>() config.build = doc.value('build').reflect<Build>()