refactor main to make it smaller
This commit is contained in:
@@ -15,6 +15,43 @@ import fleg "flag"
|
|||||||
* 4. (write fancy timings and footer for main page)
|
* 4. (write fancy timings and footer for main page)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
create_html_file :: proc(md_f: string, dir: string, output_dir: string, allocater := context.allocator) -> (file: ^os.File, err: os.Error){
|
||||||
|
sb := strings.builder_make(allocater) //sb for writing to files
|
||||||
|
defer strings.builder_destroy(&sb)
|
||||||
|
|
||||||
|
md_filename := strings.split(md_f, dir, allocater)
|
||||||
|
trimmed_filename := strings.trim_right(md_filename[1], ".md")
|
||||||
|
|
||||||
|
// sck: Change to output directory. Not sure if this also is needed
|
||||||
|
//But messing around with it means I create a lot of
|
||||||
|
//files to clean up. Works for now!
|
||||||
|
os.change_directory(output_dir)
|
||||||
|
|
||||||
|
strings.write_string(&sb, fmt.aprintf("%v", trimmed_filename, allocator = allocater))
|
||||||
|
strings.write_string(&sb, ".html")
|
||||||
|
|
||||||
|
//get "full" name for creation + writing to html file
|
||||||
|
full_filename := strings.to_string(sb)
|
||||||
|
file = os.create(full_filename) or_return
|
||||||
|
|
||||||
|
return file, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
write_footer_and_timings :: proc(trimmed_filename: string, file: ^os.File, start: time.Tick, allocator := context.allocator){
|
||||||
|
switch trimmed_filename {
|
||||||
|
case "index":
|
||||||
|
buf: [1024]u8
|
||||||
|
date := time.now()
|
||||||
|
end := time.tick_since(start)
|
||||||
|
|
||||||
|
os.write_string(file, "<br><footer><hr />This site was generated με αγάπη on ")
|
||||||
|
os.write_string(file, fmt.aprintf("%s ", time.to_string_dd_mm_yy(date, buf[:]), allocator = allocator))
|
||||||
|
os.write_string(file, fmt.aprintf("in %v ", time.duration_round(end, time.Nanosecond), allocator = allocator))
|
||||||
|
os.write_string(file, "<font color=#ea76cb><3</font> </footer><br>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
// Arena used for ALL allocations, easier to drop everything at end of execution.
|
// Arena used for ALL allocations, easier to drop everything at end of execution.
|
||||||
arena: vmem.Arena
|
arena: vmem.Arena
|
||||||
@@ -23,7 +60,7 @@ main :: proc() {
|
|||||||
arena_alloc := vmem.arena_allocator(&arena)
|
arena_alloc := vmem.arena_allocator(&arena)
|
||||||
defer vmem.arena_destroy(&arena) //clean up everything!
|
defer vmem.arena_destroy(&arena) //clean up everything!
|
||||||
|
|
||||||
fleg.init_custom_allocator(arena_alloc) //use arena for flags, why not I made the library!
|
fleg.init_custom_allocator(arena_alloc)
|
||||||
defer fleg.destroy()
|
defer fleg.destroy()
|
||||||
|
|
||||||
directory: string
|
directory: string
|
||||||
@@ -34,63 +71,31 @@ main :: proc() {
|
|||||||
|
|
||||||
start := time.tick_now() //start timer for footer
|
start := time.tick_now() //start timer for footer
|
||||||
|
|
||||||
// First generate the blog index file
|
// First generate the blog index MARKDOWN file
|
||||||
generate_blog_md_file(directory, arena_alloc)
|
generate_blog_index_md_file(directory, arena_alloc)
|
||||||
|
|
||||||
// Walk directory and collect .md files
|
// Walk directory and collect .md files
|
||||||
md_files := walk_tree_and_get_md_names(directory, arena_alloc)
|
md_files := walk_tree_and_get_md_names(directory, arena_alloc)
|
||||||
|
|
||||||
// ************* MAIN LOOP ************
|
// ************* MAIN LOOP ************
|
||||||
for file, _ in md_files {
|
for file, _ in md_files {
|
||||||
|
if file == ".DS_Store" { continue } //Ignore annoying MacOS (TODO: clean up proc?)
|
||||||
|
md_filename := strings.split(file, directory, arena_alloc)
|
||||||
|
trimmed_filename := strings.trim_right(md_filename[1], ".md")
|
||||||
// sck: I am not sure if changing dir's is needed but I am now relying on it lol
|
// sck: I am not sure if changing dir's is needed but I am now relying on it lol
|
||||||
os.change_directory(directory)
|
os.change_directory(directory)
|
||||||
|
|
||||||
// turn all .md files into parsed .html files!
|
// turn all .md files into parsed .html files!
|
||||||
html, parse_err := parse_file_md_to_html(file, arena_alloc)
|
html_content, parse_err := parse_file_md_to_html(file, arena_alloc)
|
||||||
if parse_err != nil {
|
if parse_err != nil {
|
||||||
fmt.eprintfln("ERROR: Could not parse file %s: %s", file, os.error_string(parse_err))
|
fmt.eprintfln("ERROR: Could not parse file %s: %s", file, os.error_string(parse_err))
|
||||||
}
|
}
|
||||||
|
final_html_file, html_file_err := create_html_file(file, directory, output_dir, arena_alloc)
|
||||||
|
|
||||||
filename := strings.split(file, directory, arena_alloc)
|
write_prefixs_to_html_file(final_html_file)
|
||||||
trimmed_filename := strings.trim_right(filename[1], ".md")
|
os.write_string(final_html_file, html_content) //WRITE PAGE
|
||||||
|
|
||||||
// sck: Change to output directory. Not sure if this also is needed
|
defer write_footer_and_timings(trimmed_filename, final_html_file, start, arena_alloc)
|
||||||
//But messing around with it means I create a lot of
|
|
||||||
//files to clean up. Works for now!
|
|
||||||
os.change_directory(output_dir)
|
|
||||||
|
|
||||||
sb := strings.builder_make(arena_alloc) //sb for writing to files
|
|
||||||
defer strings.builder_destroy(&sb)
|
|
||||||
|
|
||||||
strings.write_string(&sb, fmt.aprintf("%v", trimmed_filename, allocator = arena_alloc))
|
|
||||||
strings.write_string(&sb, ".html")
|
|
||||||
|
|
||||||
//get "full" name for creation + writing to html file
|
|
||||||
full_filename := strings.to_string(sb)
|
|
||||||
file, file_err := os.create(full_filename)
|
|
||||||
if file_err != nil {
|
|
||||||
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, html) //WRITE PAGE
|
|
||||||
|
|
||||||
//Defer this as we want the last page written to have
|
|
||||||
//accurate timings
|
|
||||||
defer {
|
|
||||||
switch trimmed_filename {
|
|
||||||
case "index":
|
|
||||||
buf: [1024]u8
|
|
||||||
date := time.now()
|
|
||||||
end := time.tick_since(start)
|
|
||||||
|
|
||||||
os.write_string(file, "<br><footer><hr />This site was generated με αγάπη on ")
|
|
||||||
os.write_string(file, fmt.aprintf("%s ", time.to_string_dd_mm_yy(date, buf[:]), allocator = arena_alloc))
|
|
||||||
os.write_string(file, fmt.aprintf("in %v ", time.duration_round(end, time.Nanosecond), allocator = arena_alloc))
|
|
||||||
os.write_string(file, "<font color=#ea76cb><3</font> </footer><br>")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//TODO: Do timings now?
|
|
||||||
free_all(arena_alloc)
|
free_all(arena_alloc)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user