Files
site-gen/gen.odin
T
2026-08-28 10:19:52 +01:00

92 lines
2.8 KiB
Odin

#+vet explicit-allocators
package main
import "core:fmt"
import "core:os"
import "core:strings"
BLOG_START_MD := `
(\
\'\
\'\ __________
/ '| ()_________)
\ '/ \ ~~~~~~~~ \
\ \ ~~~~~~ \
==). \__________\
(__) ()__________)
`
BLOG_TITLE_MD := `
# Articles and Blogs
`
BLOG_BACK_MD := `
[<= Back](./..)
`
// Generates the blog index markdown file (blog/index.md) complete with all
// current blogs/articles. This is then passed to the 'parse_file_md_to_html'
// We do this to make updating the blog index much easier
generate_blog_md_file :: proc(path: string, allocator := context.allocator) {
blog_list := make([dynamic]string, allocator)
sb := strings.builder_make(allocator)
blog_md_file := strings.builder_make(allocator)
strings.write_string(&sb, path)
strings.write_string(&sb, "blog/")
blog_dir := strings.to_string(sb)
blogs := walk_tree_and_get_md_names(blog_dir, allocator)
// *********** BLOG PARSING **************
/*
* WIP files are denoted with an '!' at the start of the file.
* This stops the files being processed and uploaded to the site
* before you finish writing!
*/
for blog in blogs {
b := strings.split(blog, blog_dir, allocator = context.temp_allocator)
trim_b := strings.trim_right(b[1], ".md")
if trim_b == "index" {continue} //Ignore Index
if strings.starts_with(trim_b, "!") {continue} //Ignore WIP files!
// now add to blog list
append(&blog_list, blog)
free_all(context.temp_allocator)
}
// sck: note we are doing this in reverse - means the blogs are ordered
// from oldest at the bottom and newest at the top
#reverse for blog_entry in blog_list {
b := strings.split(blog_entry, blog_dir, allocator = context.temp_allocator)
trim_b := strings.trim_right(b[1], ".md")
strings.write_string(&blog_md_file, fmt.aprintf("* [%s](./%s.html)", trim_b, trim_b, allocator = context.temp_allocator))
strings.write_string(&blog_md_file, "\n")
free_all(context.temp_allocator)
}
// sck: Now we create the index.md file.
os.change_directory(blog_dir)
index_file, err := os.create("index.md")
if err != nil {
fmt.eprintfln("ERROR in generate_blog_md_file: Could not create file %s: %s", index_file, os.error_string(err))
}
// sck: final step, write everything in the correct order for the index.html page
// note: we generate the MD file for the blog/index.md
// this saves me time from ordering the blog and allows for automagic
// indexing of blogs/articles
os.write_string(index_file, "```text")
os.write_string(index_file, BLOG_START_MD)
os.write_string(index_file, "```\n")
os.write_string(index_file, BLOG_TITLE_MD)
os.write_string(index_file, "\n")
os.write_string(index_file, strings.to_string(blog_md_file))
os.write_string(index_file, "\n")
os.write_string(index_file, BLOG_BACK_MD)
}