vss/commands/build.v

130 lines
3 KiB
Coq
Raw Normal View History

2022-08-13 21:44:21 +09:00
module commands
2022-07-23 02:12:02 +09:00
import os
import cli
2022-08-14 03:52:59 +09:00
import log
import time
import regex
2022-07-23 02:12:02 +09:00
import markdown
2022-09-22 09:47:20 +09:00
import internal.template
2022-09-25 16:09:07 +09:00
import internal.config
2022-07-23 02:12:02 +09:00
2022-07-29 12:46:36 +09:00
const default_config = 'config.toml'
2022-08-12 20:40:42 +09:00
const default_template = 'layouts/index.html'
2022-08-12 20:40:42 +09:00
const defautl_static = 'static'
2022-08-10 10:16:52 +09:00
2022-07-27 14:54:44 +09:00
const default_index = 'index.md'
2022-07-27 02:17:00 +09:00
const default_dist = 'dist'
2022-08-13 21:44:21 +09:00
fn new_build_cmd() cli.Command {
return cli.Command{
name: 'build'
description: 'build your site'
usage: 'vss build'
2022-07-23 02:12:02 +09:00
execute: fn (cmd cli.Command) ? {
2022-08-14 20:01:01 +09:00
mut logger := log.Log{}
logger.set_level(log.Level.info)
build(mut logger) or {
logger.error(err.msg())
2022-08-14 20:02:41 +09:00
println('Build failed')
2022-08-14 20:01:01 +09:00
}
2022-07-23 02:12:02 +09:00
}
}
}
2022-08-14 20:01:01 +09:00
fn read_file(filename string) ?string {
contents := os.read_file(filename.trim_space())?
return contents
}
fn get_html_path(md_path string) string {
2022-08-02 02:21:31 +09:00
mut file_name := os.file_name(md_path)
file_name = file_name.replace('.md', '.html')
dir := os.dir(md_path)
if dir == '.' {
return file_name
}
return os.join_path(dir, file_name)
}
fn normalise_paths(paths []string) []string {
cwd := os.getwd() + os.path_separator
mut res := paths.map(os.abs_path(it).replace(cwd, '').replace(os.path_separator, '/'))
res.sort()
return res
2022-08-02 02:21:31 +09:00
}
2022-07-27 02:17:00 +09:00
2022-08-03 02:29:03 +09:00
// pre_proc_md_to_html convert markdown relative links to html relative links
2022-08-12 21:59:31 +09:00
fn pre_proc_md_to_html(contents string) ?string {
2022-08-02 13:00:14 +09:00
lines := contents.split_into_lines()
mut parsed_lines := []string{len: lines.len}
2022-08-12 21:59:31 +09:00
mut re := regex.regex_opt(r'\[.+\]\(.+\.md\)')?
2022-08-12 21:59:31 +09:00
for i, line in contents.split_into_lines() {
start, end := re.find(line)
if start >= 0 && end > start {
parsed_lines[i] = line.replace('.md', '.html')
} else {
parsed_lines[i] = line
}
2022-08-02 13:00:14 +09:00
}
return parsed_lines.join('\n')
2022-08-02 13:00:14 +09:00
}
2022-08-14 20:01:01 +09:00
fn build(mut logger log.Log) ? {
2022-08-14 20:02:41 +09:00
println('Start building')
2022-08-14 03:52:59 +09:00
mut sw := time.new_stopwatch()
2022-08-13 21:44:21 +09:00
dist := commands.default_dist
2022-08-12 21:59:31 +09:00
if os.exists(dist) {
2022-08-14 20:02:41 +09:00
logger.info('re-create dist dir')
2022-08-12 21:59:31 +09:00
os.rmdir_all(dist)?
os.mkdir_all(dist)?
} else {
2022-08-14 20:02:41 +09:00
logger.info('create dist dir')
2022-08-12 21:59:31 +09:00
os.mkdir_all(dist)?
2022-07-27 02:17:00 +09:00
}
2022-08-02 02:21:31 +09:00
2022-08-12 20:40:42 +09:00
// copy static files
2022-08-13 21:44:21 +09:00
if os.exists(commands.defautl_static) {
2022-08-14 20:02:41 +09:00
logger.info('copy static files')
2022-08-13 21:44:21 +09:00
os.cp_all(commands.defautl_static, dist, false)?
}
2022-08-12 20:40:42 +09:00
2022-08-13 21:44:21 +09:00
template_content := os.read_file(commands.default_template)?
2022-09-25 16:09:07 +09:00
toml_text := read_file(commands.default_config)?
config := config.load(toml_text)?
mut config_map := config.as_map()
2022-08-02 02:21:31 +09:00
md_paths := normalise_paths(os.walk_ext('.', '.md'))
2022-08-14 20:02:41 +09:00
logger.info('start md to html')
2022-08-02 02:21:31 +09:00
for path in md_paths {
2022-09-25 16:09:07 +09:00
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
}
2022-08-03 02:29:03 +09:00
mut md := os.read_file(path)?
2022-08-12 21:59:31 +09:00
md = pre_proc_md_to_html(md)?
2022-08-02 02:21:31 +09:00
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)?
2022-08-02 02:21:31 +09:00
}
2022-08-14 20:02:41 +09:00
logger.info('end md to html')
2022-08-14 03:52:59 +09:00
sw.stop()
2022-08-14 20:02:41 +09:00
println('Total in ' + sw.elapsed().milliseconds().str() + ' ms')
2022-07-27 02:17:00 +09:00
return
}