added blog index generator
This commit is contained in:
parent
5c05bedb71
commit
d83fc3f75a
73
gen.odin
Normal file
73
gen.odin
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:os"
|
||||||
|
import "core:strings"
|
||||||
|
|
||||||
|
BLOG_START_MD := `
|
||||||
|
(\
|
||||||
|
\'\
|
||||||
|
\'\ __________
|
||||||
|
/ '| ()_________)
|
||||||
|
\ '/ \ ~~~~~~~~ \
|
||||||
|
\ \ ~~~~~~ \
|
||||||
|
==). \__________\
|
||||||
|
(__) ()__________)
|
||||||
|
`
|
||||||
|
|
||||||
|
BLOG_TITLE_MD := `
|
||||||
|
# Articles and Blogs
|
||||||
|
`
|
||||||
|
|
||||||
|
BLOG_BACK_MD := `
|
||||||
|
[<= Back](./..)
|
||||||
|
`
|
||||||
|
generate_blog_md_file :: proc(path: string, allocator := context.allocator) {
|
||||||
|
|
||||||
|
blog_list := make([dynamic]string)
|
||||||
|
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)
|
||||||
|
|
||||||
|
//os.write_string(file, BLOG_START)
|
||||||
|
blogs := walk_tree_and_get_md_names(blog_dir)
|
||||||
|
|
||||||
|
for blog in blogs {
|
||||||
|
b := strings.split(blog, blog_dir)
|
||||||
|
trim_b := strings.trim_right(b[1], ".md")
|
||||||
|
if trim_b == "index" {continue}
|
||||||
|
if trim_b == "index_non_blank" {continue}
|
||||||
|
if trim_b == "test" {continue}
|
||||||
|
if trim_b == "test_blog" {continue}
|
||||||
|
append(&blog_list, blog)
|
||||||
|
}
|
||||||
|
|
||||||
|
for blog_entry in blog_list {
|
||||||
|
b := strings.split(blog_entry, blog_dir)
|
||||||
|
trim_b := strings.trim_right(b[1], ".md")
|
||||||
|
strings.write_string(&blog_md_file, fmt.aprintf("* [%s](./%s.html)", trim_b, trim_b))
|
||||||
|
strings.write_string(&blog_md_file, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 open file %s: %s",
|
||||||
|
index_file,
|
||||||
|
os.error_string(err),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
os.write_string(index_file, "```")
|
||||||
|
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)
|
||||||
|
}
|
||||||
29
main.odin
29
main.odin
@ -3,6 +3,7 @@ package main
|
|||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import vmem "core:mem/virtual"
|
import vmem "core:mem/virtual"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
|
import "core:sort"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
import "core:time"
|
import "core:time"
|
||||||
|
|
||||||
@ -26,14 +27,14 @@ main :: proc() {
|
|||||||
arena_alloc := vmem.arena_allocator(&arena)
|
arena_alloc := vmem.arena_allocator(&arena)
|
||||||
defer vmem.arena_destroy(&arena) //clean
|
defer vmem.arena_destroy(&arena) //clean
|
||||||
|
|
||||||
start := time.tick_now()
|
start := time.tick_now() //start timer for footer
|
||||||
|
|
||||||
|
generate_blog_md_file(directory)
|
||||||
|
|
||||||
md_files := walk_tree_and_get_md_names(directory, arena_alloc)
|
md_files := walk_tree_and_get_md_names(directory, arena_alloc)
|
||||||
end := time.tick_since(start)
|
|
||||||
|
|
||||||
for file, i in md_files {
|
for file, i in md_files {
|
||||||
os.change_directory(directory)
|
os.change_directory(directory)
|
||||||
fmt.printfln("INFO: Parsing %s from .MD to HTML...", file)
|
//fmt.printfln("INFO: Parsing %s from .MD to HTML...", file)
|
||||||
|
|
||||||
html, parse_err := parse_file_md_to_html(file, arena_alloc)
|
html, parse_err := parse_file_md_to_html(file, arena_alloc)
|
||||||
if parse_err != nil {
|
if parse_err != nil {
|
||||||
@ -45,24 +46,25 @@ main :: proc() {
|
|||||||
trimmed_filename := strings.trim_right(filename[1], ".md")
|
trimmed_filename := strings.trim_right(filename[1], ".md")
|
||||||
//fmt.printfln("INFO: trimmed_filename: %s", trimmed_filename)
|
//fmt.printfln("INFO: trimmed_filename: %s", trimmed_filename)
|
||||||
|
|
||||||
|
//Change to output directory...
|
||||||
os.change_directory(output_dir)
|
os.change_directory(output_dir)
|
||||||
|
|
||||||
sb := strings.builder_make(arena_alloc)
|
//print html file name
|
||||||
|
sb := strings.builder_make(arena_alloc) //sb for writing to files, resets ever iteration
|
||||||
strings.write_string(&sb, fmt.aprintf("%v", trimmed_filename, allocator = arena_alloc))
|
strings.write_string(&sb, fmt.aprintf("%v", trimmed_filename, allocator = arena_alloc))
|
||||||
strings.write_string(&sb, ".html")
|
strings.write_string(&sb, ".html")
|
||||||
full_filename := strings.to_string(sb)
|
|
||||||
|
|
||||||
|
//get "full" name for creation + writing to html file
|
||||||
|
full_filename := strings.to_string(sb)
|
||||||
file, file_err := os.create(full_filename)
|
file, file_err := os.create(full_filename)
|
||||||
if file_err != nil {
|
if file_err != nil {
|
||||||
fmt.eprintfln("ERROR: Could not open file %s: %s", file, os.error_string(file_err))
|
fmt.eprintfln("ERROR: Could not open file %s: %s", file, os.error_string(file_err))
|
||||||
}
|
}
|
||||||
|
write_prefixs_to_html_file(file)
|
||||||
os.write_string(file, UTF8_PREFIX)
|
|
||||||
os.write_string(file, CSS_PREFIX)
|
|
||||||
os.write_string(file, MASTODON_PREFIX)
|
|
||||||
os.write_string(file, html)
|
os.write_string(file, html)
|
||||||
|
|
||||||
if trimmed_filename == "index" {
|
switch trimmed_filename {
|
||||||
|
case "index":
|
||||||
buf: [1024]u8
|
buf: [1024]u8
|
||||||
date := time.now()
|
date := time.now()
|
||||||
end := time.tick_since(start)
|
end := time.tick_since(start)
|
||||||
@ -71,8 +73,9 @@ main :: proc() {
|
|||||||
os.write_string(file, fmt.aprintf("%s ", time.to_string_dd_mm_yy(date, buf[:])))
|
os.write_string(file, fmt.aprintf("%s ", time.to_string_dd_mm_yy(date, buf[:])))
|
||||||
os.write_string(file, fmt.aprintf("in %v ", time.duration_round(end, time.Nanosecond)))
|
os.write_string(file, fmt.aprintf("in %v ", time.duration_round(end, time.Nanosecond)))
|
||||||
os.write_string(file, "<font color=#ea76cb><3</font> </footer><br>")
|
os.write_string(file, "<font color=#ea76cb><3</font> </footer><br>")
|
||||||
|
|
||||||
|
}
|
||||||
|
//fmt.printfln("INFO: Wrote %s into HTML!", md_files[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.printfln("INFO: Wrote %s into HTML!", md_files[i])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user