103 lines
3.0 KiB
Odin
103 lines
3.0 KiB
Odin
#+vet explicit-allocators
|
|
package main
|
|
|
|
import "core:fmt"
|
|
import "core:os"
|
|
import "core:strings"
|
|
|
|
/*
|
|
2026 Simon Kellet
|
|
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.
|
|
|
|
The final step is to write the blog in order.
|
|
*/
|
|
|
|
BLOG_START_MD := `
|
|
(\
|
|
\'\
|
|
\'\ __________
|
|
/ '| ()_________)
|
|
\ '/ \ ~~~~~~~~ \
|
|
\ \ ~~~~~~ \
|
|
==). \__________\
|
|
(__) ()__________)
|
|
`
|
|
|
|
BLOG_TITLE_MD := `
|
|
# Articles and Blogs
|
|
`
|
|
|
|
BLOG_BACK_MD := `
|
|
[<= Back](./..)
|
|
`
|
|
|
|
generate_blog_index_md_file :: proc(path: string, allocator := context.allocator) {
|
|
blog_list := make([dynamic]string, allocator)
|
|
defer delete(blog_list)
|
|
|
|
sb := strings.builder_make(allocator)
|
|
blog_md_file := strings.builder_make(allocator)
|
|
defer strings.builder_destroy(&sb)
|
|
defer strings.builder_destroy(&blog_md_file)
|
|
|
|
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 trim_b == ".DS_Store" {continue} //Ignore annoying MacOS files
|
|
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 /blog/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)) //CONTENT OF BLOG
|
|
os.write_string(index_file, "\n")
|
|
os.write_string(index_file, BLOG_BACK_MD)
|
|
}
|