|
|
@ -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')
|
|
|
|
|
|
|
|
|
|
|
|