diff --git a/main.odin b/main.odin index 05e0222..450f695 100644 --- a/main.odin +++ b/main.odin @@ -1,37 +1,44 @@ package main import "core:fmt" +import vmem "core:mem/virtual" import "core:os" - +import "core:strings" main :: proc() { + directory := "/Users/simon/Odin/cm/test/website/MARKDOWN/" + output_dir := "/Users/simon/Odin/cm/test/website/HTML/" - //steps - //1. parse all md files into html - //2. delete old dir (if exists) - //3. make new dir (HTML) - //4. for each file, write a new file with the name - //5. append css style - //done? + arena: vmem.Arena + arena_err := vmem.arena_init_growing(&arena) + ensure(arena_err == nil) + arena_alloc := vmem.arena_allocator(&arena) + defer vmem.arena_destroy(&arena) //clean - md_files := walk_tree_and_get_md_names("./test/website/MARKDOWN/") - defer delete(md_files) + md_files := walk_tree_and_get_md_names(directory, arena_alloc) - html_files := make(map[string]cstring) - defer delete(html_files) - - //convert each file to html - for file in md_files { + for file, i in md_files { + os.change_directory(directory) fmt.printfln("parsing %s to html...", file) - html, err := parse_file_md_to_html(file) - - if err != nil { - fmt.printfln("ERROR: Could not open file %s: %s", file, os.error_string(err)) + html, parse_err := parse_file_md_to_html(file, arena_alloc) + if parse_err != nil { + fmt.printfln("ERROR: Could not open file %s: %s", file, os.error_string(parse_err)) } - //fmt.println(html) - html_files[file] = html + filename := strings.split(file, directory) + trimmed_filename := strings.trim_right(filename[1], ".md") + + + sb := strings.builder_make() + strings.write_string(&sb, fmt.aprintf("%v", trimmed_filename, allocator = arena_alloc)) + strings.write_string(&sb, ".html") + + os.change_directory(output_dir) + full_filename := strings.to_string(sb) + file, file_err := os.create(full_filename) + + os.write_string(file, CSS_PREFIX) + os.write_string(file, MASTODON_PREFIX) + os.write_string(file, html) } - fmt.println(html_files["/Users/simon/Odin/cm/test/website/MARKDOWN/A5Week4.md"]) - } diff --git a/parse.odin b/parse.odin index 7e7d31b..54ede3a 100644 --- a/parse.odin +++ b/parse.odin @@ -1,5 +1,6 @@ package main +import "base:runtime" import "core:fmt" import "core:os" import "core:strings" @@ -7,11 +8,10 @@ import cm "vendor:commonmark" MD_SUFFIX :: ".md" +CSS_PREFIX :: "" +MASTODON_PREFIX :: "" -walk_tree_and_get_md_names :: proc( - path: string, - allocator := context.allocator, -) -> [dynamic]string { +walk_tree_and_get_md_names :: proc(path: string, allocator: runtime.Allocator) -> [dynamic]string { files := make([dynamic]string, allocator) @@ -70,9 +70,10 @@ walk_tree :: proc(path: string) -> []u8 { } +/* parse_file_md_to_html :: proc( filename: string, - allocator := context.allocator, + allocator: runtime.Allocator, ) -> ( parsed: cstring, err: os.Error, @@ -88,3 +89,24 @@ parse_file_md_to_html :: proc( return html, nil } +*/ + +parse_file_md_to_html :: proc( + filename: string, + allocator: runtime.Allocator, +) -> ( + parsed: string, + err: os.Error, +) { + str := os.read_entire_file_from_path(filename, allocator) or_return + + root := cm.parse_document(raw_data(str), len(str), cm.DEFAULT_OPTIONS) + defer cm.node_free(root) + + html := cm.render_html(root, cm.DEFAULT_OPTIONS) + defer cm.free(html) + + parsed = strings.clone_from_cstring(html, allocator) + + return parsed, nil +}