layout/_index.html を 外部から読み込めるように変更

main
zztkm 2 years ago
parent b432961097
commit 315baaf552

@ -1,15 +1,12 @@
/*
*/
module template module template
struct Template { pub fn parse(template string, target map[string]string) string {
template string mut content := template
} for key in target.keys() {
at_key := '@' + key
fn parse(template string, target map[string]any) string { value := target[key]
// assign to content
content = content.replace(at_key, value)
}
return content
} }
fn execute() {}

26
vss.v

@ -4,9 +4,14 @@ import os
import cli import cli
import toml import toml
import markdown import markdown
import template
const default_config = 'config.toml' const default_config = 'config.toml'
const config_params = ['title']
const default_template = 'layouts/_index.html'
const default_index = 'index.md' const default_index = 'index.md'
const default_dist = 'dist' const default_dist = 'dist'
@ -31,22 +36,27 @@ fn get_paths(path string) []string {
} }
fn get_config_map() map[string]string { fn get_config_map() map[string]string {
mut confg_map := map[string]string{} mut config_map := map[string]string{}
// https://modules.vlang.io/toml.html // https://modules.vlang.io/toml.html
config := toml.parse_file(default_config)? config := toml.parse_file(default_config) or { return config_map }
for param in config_params {
config_map[param] = config.value(param).string()
}
return config_map
} }
fn generate_index_page() ? { fn generate_index_page() ? {
index_md := os.read_file(default_index)? index_md := os.read_file(default_index)?
// for $tmpl value
title := config.value('title').string()
contents := markdown.to_html(index_md) contents := markdown.to_html(index_md)
mut config_map := get_config_map()
config_map['contents'] = contents
template_content := os.read_file(default_template)?
index_html := template.parse(template_content, config_map)
// tmpl に変数を割り当てるのは今の所無理
// https://github.com/vlang/v/discussions/15068
index_html := $tmpl('layouts/_index.html')
dist := default_dist dist := default_dist
if !os.exists(dist) { if !os.exists(dist) {

Loading…
Cancel
Save