You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.2 KiB
V

module commands
2 years ago
import os
import cli
import log
import time
import toml
import regex
2 years ago
import markdown
import template
2 years ago
const default_config = 'config.toml'
// Allowed parameters
const config_params = ['title', 'description', 'baseUrl']
const default_template = 'layouts/index.html'
const defautl_static = 'static'
2 years ago
2 years ago
const default_index = 'index.md'
2 years ago
const default_dist = 'dist'
fn new_build_cmd() cli.Command {
return cli.Command{
name: 'build'
description: 'build your site'
usage: 'vss build'
2 years ago
execute: fn (cmd cli.Command) ? {
mut logger := log.Log{}
logger.set_level(log.Level.info)
build(mut logger) or {
logger.error(err.msg())
2 years ago
println('Build failed')
}
2 years ago
}
}
}
fn read_file(filename string) ?string {
contents := os.read_file(filename.trim_space())?
return contents
}
fn get_config_map() ?map[string]string {
mut config_map := map[string]string{}
2 years ago
// 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 {
2 years ago
v := config.value_opt(param) or { continue }
config_map[param] = v.string()
}
return config_map
2 years ago
}
fn get_html_path(md_path string) string {
2 years ago
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
2 years ago
}
2 years ago
2 years ago
// pre_proc_md_to_html convert markdown relative links to html relative links
fn pre_proc_md_to_html(contents string) ?string {
2 years ago
lines := contents.split_into_lines()
mut parsed_lines := []string{len: lines.len}
mut re := regex.regex_opt(r'\[.+\]\(.+\.md\)')?
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
}
2 years ago
}
return parsed_lines.join('\n')
2 years ago
}
fn build(mut logger log.Log) ? {
2 years ago
println('Start building')
mut sw := time.new_stopwatch()
dist := commands.default_dist
if os.exists(dist) {
2 years ago
logger.info('re-create dist dir')
os.rmdir_all(dist)?
os.mkdir_all(dist)?
} else {
2 years ago
logger.info('create dist dir')
os.mkdir_all(dist)?
2 years ago
}
2 years ago
// copy static files
if os.exists(commands.defautl_static) {
2 years ago
logger.info('copy static files')
os.cp_all(commands.defautl_static, dist, false)?
}
template_content := os.read_file(commands.default_template)?
mut config_map := get_config_map()?
2 years ago
md_paths := normalise_paths(os.walk_ext('.', '.md'))
2 years ago
logger.info('start md to html')
2 years ago
for path in md_paths {
2 years ago
mut md := os.read_file(path)?
md = pre_proc_md_to_html(md)?
2 years ago
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)?
2 years ago
}
2 years ago
logger.info('end md to html')
sw.stop()
2 years ago
println('Total in ' + sw.elapsed().milliseconds().str() + ' ms')
2 years ago
return
}