vss/vss.v

113 lines
2.4 KiB
Coq
Raw Normal View History

2022-07-22 19:12:02 +02:00
module main
import os
import cli
2022-07-29 05:46:36 +02:00
import toml
import regex
2022-07-22 19:12:02 +02:00
import markdown
import template
2022-07-22 19:12:02 +02:00
2022-07-29 05:46:36 +02:00
const default_config = 'config.toml'
// Allowed parameters
2022-08-12 14:59:31 +02:00
const config_params = ['title', 'description', 'baseUrl']
2022-08-12 13:40:42 +02:00
const default_template = 'layouts/index.html'
2022-08-12 13:40:42 +02:00
const defautl_static = 'static'
2022-08-10 03:16:52 +02:00
2022-07-27 07:54:44 +02:00
const default_index = 'index.md'
2022-07-26 19:17:00 +02:00
const default_dist = 'dist'
2022-07-22 19:12:02 +02:00
fn main() {
mut app := cli.Command{
name: 'vss'
2022-08-13 13:35:05 +02:00
version: '0.0.7'
description: 'static site generator'
2022-07-22 19:12:02 +02:00
execute: fn (cmd cli.Command) ? {
2022-08-10 03:16:52 +02:00
println(cmd.help_message())
2022-07-22 19:12:02 +02:00
}
2022-08-10 03:16:52 +02:00
commands: [
cli.Command{
name: 'build'
description: 'build your site'
usage: 'vss build'
execute: fn (cmd cli.Command) ? {
2022-08-12 13:40:42 +02:00
build()?
2022-08-10 03:16:52 +02:00
}
},
]
2022-07-22 19:12:02 +02:00
}
app.setup()
app.parse(os.args)
2022-07-22 19:12:02 +02:00
}
2022-08-12 14:59:31 +02:00
fn get_config_map() ?map[string]string {
mut config_map := map[string]string{}
2022-07-29 19:24:29 +02:00
// https://modules.vlang.io/toml.html
2022-08-12 14:59:31 +02:00
config := toml.parse_file(default_config)?
for param in config_params {
2022-08-12 16:18:32 +02:00
v := config.value_opt(param) or { continue }
2022-08-12 14:59:31 +02:00
config_map[param] = v.string()
}
return config_map
2022-07-29 19:24:29 +02:00
}
2022-08-01 19:21:31 +02:00
fn get_html_filename(md_path string) string {
mut file_name := os.file_name(md_path)
file_name = file_name.replace('.md', '')
return file_name + '.html'
}
2022-07-26 19:17:00 +02:00
2022-08-02 19:29:03 +02:00
// pre_proc_md_to_html convert markdown relative links to html relative links
2022-08-12 14:59:31 +02:00
fn pre_proc_md_to_html(contents string) ?string {
2022-08-02 06:00:14 +02:00
lines := contents.split_into_lines()
mut parsed_lines := []string{len: lines.len}
2022-08-12 14:59:31 +02:00
mut re := regex.regex_opt(r'\[.+\]\(.+\.md\)')?
2022-08-12 14:59:31 +02: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 06:00:14 +02:00
}
return parsed_lines.join('\n')
2022-08-02 06:00:14 +02:00
}
2022-08-12 13:40:42 +02:00
fn build() ? {
2022-07-26 19:17:00 +02:00
dist := default_dist
2022-08-12 14:59:31 +02:00
if os.exists(dist) {
os.rmdir_all(dist)?
os.mkdir_all(dist)?
} else {
os.mkdir_all(dist)?
2022-07-26 19:17:00 +02:00
}
2022-08-01 19:21:31 +02:00
2022-08-12 13:40:42 +02:00
// copy static files
if os.exists(defautl_static) {
os.cp_all(defautl_static, dist, false)?
}
2022-08-12 13:40:42 +02:00
2022-08-01 19:21:31 +02:00
template_content := os.read_file(default_template)?
2022-08-12 14:59:31 +02:00
mut config_map := get_config_map()?
2022-08-01 19:21:31 +02:00
2022-08-12 13:40:42 +02:00
md_paths := os.walk_ext('.', '.md')
2022-08-01 19:21:31 +02:00
for path in md_paths {
2022-08-02 19:29:03 +02:00
mut md := os.read_file(path)?
2022-08-12 14:59:31 +02:00
md = pre_proc_md_to_html(md)?
2022-08-01 19:21:31 +02:00
contents := markdown.to_html(md)
config_map['contents'] = contents
html := template.parse(template_content, config_map)
filename := get_html_filename(path)
html_path := os.join_path(dist, filename)
os.write_file(html_path, html)?
}
2022-07-26 19:17:00 +02:00
return
}