commit
29e8082a41
2 changed files with 91 additions and 39 deletions
126
commands/build.v
126
commands/build.v
|
@ -19,6 +19,22 @@ const default_index = 'index.md'
|
||||||
|
|
||||||
const default_dist = 'dist'
|
const default_dist = 'dist'
|
||||||
|
|
||||||
|
struct Builder {
|
||||||
|
mut:
|
||||||
|
config config.Config
|
||||||
|
logger log.Log
|
||||||
|
dist string
|
||||||
|
static_dir string
|
||||||
|
template_content string
|
||||||
|
config_map map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_builder(logger log.Log) Builder {
|
||||||
|
return Builder{
|
||||||
|
logger: logger
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn new_build_cmd() cli.Command {
|
fn new_build_cmd() cli.Command {
|
||||||
return cli.Command{
|
return cli.Command{
|
||||||
name: 'build'
|
name: 'build'
|
||||||
|
@ -75,52 +91,88 @@ fn pre_proc_md_to_html(contents string) ?string {
|
||||||
return parsed_lines.join('\n')
|
return parsed_lines.join('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(mut logger log.Log) ? {
|
fn get_md_content(path string) ?string {
|
||||||
println('Start building')
|
md := os.read_file(path)?
|
||||||
mut sw := time.new_stopwatch()
|
return pre_proc_md_to_html(md)
|
||||||
|
|
||||||
dist := commands.default_dist
|
|
||||||
if os.exists(dist) {
|
|
||||||
logger.info('re-create dist dir')
|
|
||||||
os.rmdir_all(dist)?
|
|
||||||
os.mkdir_all(dist)?
|
|
||||||
} else {
|
|
||||||
logger.info('create dist dir')
|
|
||||||
os.mkdir_all(dist)?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy static files
|
fn get_content(path string) ?string {
|
||||||
if os.exists(commands.defautl_static) {
|
md := get_md_content(path)?
|
||||||
logger.info('copy static files')
|
return markdown.to_html(md)
|
||||||
os.cp_all(commands.defautl_static, dist, false)?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template_content := os.read_file(commands.default_template)?
|
fn (mut b Builder) md2html(md_path string) ? {
|
||||||
|
// get html body content from md
|
||||||
toml_text := read_file(commands.default_config)?
|
content := get_content(md_path)?
|
||||||
config := config.load(toml_text)?
|
// want to change from contents to content
|
||||||
mut config_map := config.as_map()
|
b.config_map['contents'] = content
|
||||||
|
html := template.parse(b.template_content, b.config_map)
|
||||||
md_paths := normalise_paths(os.walk_ext('.', '.md'))
|
html_path := get_html_path(md_path)
|
||||||
logger.info('start md to html')
|
dist_path := os.join_path(b.dist, html_path)
|
||||||
for path in md_paths {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
mut md := os.read_file(path)?
|
|
||||||
md = pre_proc_md_to_html(md)?
|
|
||||||
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)) {
|
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() ? {
|
||||||
|
toml_text := read_file(commands.default_config)?
|
||||||
|
config := config.load(toml_text)?
|
||||||
|
template_content := os.read_file(commands.default_template)?
|
||||||
|
|
||||||
|
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() ? {
|
||||||
|
if os.exists(b.static_dir) {
|
||||||
|
os.cp_all(b.static_dir, b.dist, false)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut b Builder) create_dist_dir() ? {
|
||||||
|
if os.exists(b.dist) {
|
||||||
|
b.logger.info('re-create dist dir')
|
||||||
|
os.rmdir_all(b.dist)?
|
||||||
|
os.mkdir_all(b.dist)?
|
||||||
|
} else {
|
||||||
|
b.logger.info('create dist dir')
|
||||||
|
os.mkdir_all(b.dist)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut b Builder) is_ignore(path string) bool {
|
||||||
|
// e.g. README.md
|
||||||
|
file_name := os.file_name(path)
|
||||||
|
// notify user that build was skipped
|
||||||
|
if file_name in b.config.build.ignore_files {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build(mut logger log.Log) ? {
|
||||||
|
println('Start building')
|
||||||
|
mut sw := time.new_stopwatch()
|
||||||
|
mut b := new_builder(logger)
|
||||||
|
b.load_config()?
|
||||||
|
b.create_dist_dir()?
|
||||||
|
// copy static dir files
|
||||||
|
logger.info('copy static files')
|
||||||
|
b.copy_static()?
|
||||||
|
|
||||||
|
mds := normalise_paths(os.walk_ext('.', '.md'))
|
||||||
|
logger.info('start md to html')
|
||||||
|
for path in mds {
|
||||||
|
if b.is_ignore(path) {
|
||||||
|
logger.info('$path is included in ignore_files, skip build')
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b.md2html(path)?
|
||||||
|
}
|
||||||
logger.info('end md to html')
|
logger.info('end md to html')
|
||||||
|
|
||||||
sw.stop()
|
sw.stop()
|
||||||
|
|
|
@ -12,7 +12,7 @@ pub mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config general settings for vss
|
// Config general settings for vss
|
||||||
struct Config {
|
pub struct Config {
|
||||||
pub mut:
|
pub mut:
|
||||||
build Build
|
build Build
|
||||||
title string
|
title string
|
||||||
|
|
Loading…
Add table
Reference in a new issue