From 315baaf5529ad320cbc04ef6e3043b63288be5de Mon Sep 17 00:00:00 2001 From: zztkm Date: Sun, 31 Jul 2022 20:16:16 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20layout/=5Findex.html=20=E3=82=92=20?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89=E8=AA=AD=E3=81=BF=E8=BE=BC?= =?UTF-8?q?=E3=82=81=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/template.v | 21 +++++++++------------ vss.v | 26 ++++++++++++++++++-------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/template/template.v b/template/template.v index 2e291bd..2868b7c 100644 --- a/template/template.v +++ b/template/template.v @@ -1,15 +1,12 @@ -/* -何も作ってない -*/ - module template -struct Template { - template string -} - -fn parse(template string, target map[string]any) string { +pub fn parse(template string, target map[string]string) string { + mut content := template + for key in target.keys() { + at_key := '@' + key + value := target[key] + // assign to content + content = content.replace(at_key, value) + } + return content } - - -fn execute() {} \ No newline at end of file diff --git a/vss.v b/vss.v index 02fee08..8aaf258 100644 --- a/vss.v +++ b/vss.v @@ -4,9 +4,14 @@ import os import cli import toml import markdown +import template const default_config = 'config.toml' +const config_params = ['title'] + +const default_template = 'layouts/_index.html' + const default_index = 'index.md' const default_dist = 'dist' @@ -31,22 +36,27 @@ fn get_paths(path 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 - 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() ? { index_md := os.read_file(default_index)? - - // for $tmpl value - title := config.value('title').string() 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 if !os.exists(dist) {