Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: add archives generater #40

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ tasks:
vet:
desc: Report suspicious code constructs
cmds:
- v vet *.v
- v vet .

format:
desc: Format .v files
cmds:
- v fmt -w *.v
- v fmt -w commands
- v fmt -w internal/template
- v fmt -w internal/config/
- v fmt -w .

clean:
desc: Clean test files
Expand Down
55 changes: 32 additions & 23 deletions commands/build.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ module commands
import os
import cli
import log
import strings
import time
import regex
import markdown
import net.html

import internal.template
import internal.config
import internal.paths

const default_config = 'config.toml'

Expand Down Expand Up @@ -54,24 +58,6 @@ fn new_build_cmd() cli.Command {
}
}

fn get_html_path(md_path string) string {
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
}

// pre_proc_md_to_html convert markdown relative links to html relative links
fn pre_proc_md_to_html(contents string) !string {
lines := contents.split_into_lines()
Expand Down Expand Up @@ -105,10 +91,11 @@ fn (mut b Builder) md2html(md_path string) ! {
// want to change from contents to content
b.config_map['contents'] = content
html := template.parse(b.template_content, b.config_map)
html_path := get_html_path(md_path)
html_path := paths.get_html_path(md_path)
dist_path := os.join_path(b.dist, html_path)
if !os.exists(os.dir(dist_path)) {
os.mkdir_all(os.dir(dist_path))!
target_dir := os.dir(dist_path)
if !os.exists(target_dir) {
os.mkdir_all(target_dir)!
}
os.write_file(dist_path, html)!
}
Expand Down Expand Up @@ -148,6 +135,27 @@ fn (mut b Builder) is_ignore(path string) bool {
return false
}

fn (mut b Builder) generate_archives(mds []string) ! {
mut buider := strings.new_builder(200)
buider.writeln('# コンテンツ一覧')
for path in mds {
if b.is_ignore(path) {
continue
}
buider.writeln('- [${path}](${path})')
}
md := buider.str()
content := markdown.to_html(pre_proc_md_to_html(md)!)
b.config_map['contents'] = content
html := template.parse(b.template_content, b.config_map)
html_path := 'archives/index.html'
dist_path := os.join_path(b.dist, html_path)
if !os.exists(os.dir(dist_path)) {
os.mkdir_all(os.dir(dist_path))!
}
os.write_file(dist_path, html)!
}

fn build(config config.Config, mut logger log.Log) ! {
println('Start building')
mut sw := time.new_stopwatch()
Expand All @@ -162,11 +170,12 @@ fn build(config config.Config, mut logger log.Log) ! {
logger.info('copy static files')
b.copy_static()!

mds := normalise_paths(os.walk_ext('.', '.md'))
mds := paths.normalise_paths(os.walk_ext('.', '.md'))
logger.info('start md to html')
b.generate_archives(mds)!
for path in mds {
if b.is_ignore(path) {
logger.info('$path is included in ignore_files, skip build')
logger.info('${path} is included in ignore_files, skip build')
continue
}
b.md2html(path)!
Expand Down
6 changes: 3 additions & 3 deletions commands/serve.v
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn watch(path string, config config.Config, mut logger log.Log) {
}
now := os.file_last_mod_unix(w.path)
if now > w.time_stamp {
println('modified file: $w.path')
println('modified file: ${w.path}')
w.time_stamp = now

build(config, mut logger) or {
Expand All @@ -117,7 +117,7 @@ fn serve(mut logger log.Log) ! {
port: commands.cport
}

local_base_url := 'http://localhost:$commands.cport/'
local_base_url := 'http://localhost:${commands.cport}/'
mut config := load_config(default_config)!
config.base_url = local_base_url
println(local_base_url)
Expand All @@ -128,7 +128,7 @@ fn serve(mut logger log.Log) ! {
println('Build failed')
}

w := go watch('.', config, mut logger)
w := spawn watch('.', config, mut logger)
server.listen_and_serve()

w.wait()
Expand Down
2 changes: 1 addition & 1 deletion commands/vss.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cli
pub fn execute() {
mut app := cli.Command{
name: 'vss'
version: '0.1.0'
version: '0.2.0'
description: 'static site generator'
execute: fn (cmd cli.Command) ! {
println(cmd.help_message())
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub mut:
pub fn load(toml_text string) !Config {
doc := toml.parse_text(toml_text)!

mut config := doc.reflect<Config>()
config.build = doc.value('build').reflect<Build>()
mut config := doc.reflect[Config]()
config.build = doc.value('build').reflect[Build]()

return config
}
Expand Down
31 changes: 31 additions & 0 deletions internal/paths/path.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module paths

import os

struct Files {
pub mut:
base_dir string
files []string
dirs []string
}

fn generate_path_map(path_list []string) {}

pub fn get_html_path(md_path string) string {
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)
}

pub 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, '/'))
// sort the array in decending order
res.sort(a > b)
return res
}
7 changes: 7 additions & 0 deletions internal/paths/url.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module paths

import net.urllib

fn add_context_root(base_url string, relative_path string) string {
return ''
}
2 changes: 1 addition & 1 deletion v.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Module {
name: 'vss'
description: 'static site generator'
version: '0.1.0'
version: '0.2.0'
license: 'MIT'
dependencies: ['markdown']
}