parses md files and adds css. doesnt make dirs tho

This commit is contained in:
Simon Kellet 2026-03-07 13:02:26 +00:00
parent 023ae00e71
commit 6f3e0899bb
2 changed files with 57 additions and 28 deletions

View File

@ -1,37 +1,44 @@
package main package main
import "core:fmt" import "core:fmt"
import vmem "core:mem/virtual"
import "core:os" import "core:os"
import "core:strings"
main :: proc() { main :: proc() {
directory := "/Users/simon/Odin/cm/test/website/MARKDOWN/"
output_dir := "/Users/simon/Odin/cm/test/website/HTML/"
//steps arena: vmem.Arena
//1. parse all md files into html arena_err := vmem.arena_init_growing(&arena)
//2. delete old dir (if exists) ensure(arena_err == nil)
//3. make new dir (HTML) arena_alloc := vmem.arena_allocator(&arena)
//4. for each file, write a new file with the name defer vmem.arena_destroy(&arena) //clean
//5. append css style
//done?
md_files := walk_tree_and_get_md_names("./test/website/MARKDOWN/") md_files := walk_tree_and_get_md_names(directory, arena_alloc)
defer delete(md_files)
html_files := make(map[string]cstring) for file, i in md_files {
defer delete(html_files) os.change_directory(directory)
//convert each file to html
for file in md_files {
fmt.printfln("parsing %s to html...", file) fmt.printfln("parsing %s to html...", file)
html, err := parse_file_md_to_html(file) html, parse_err := parse_file_md_to_html(file, arena_alloc)
if parse_err != nil {
if err != nil { fmt.printfln("ERROR: Could not open file %s: %s", file, os.error_string(parse_err))
fmt.printfln("ERROR: Could not open file %s: %s", file, os.error_string(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"])
} }

View File

@ -1,5 +1,6 @@
package main package main
import "base:runtime"
import "core:fmt" import "core:fmt"
import "core:os" import "core:os"
import "core:strings" import "core:strings"
@ -7,11 +8,10 @@ import cm "vendor:commonmark"
MD_SUFFIX :: ".md" MD_SUFFIX :: ".md"
CSS_PREFIX :: "<head><link rel=\"stylesheet\" href=\"./css/style.css\">"
MASTODON_PREFIX :: "<a rel=me href=https://linuxrocks.online/@simonkellet></a>"
walk_tree_and_get_md_names :: proc( walk_tree_and_get_md_names :: proc(path: string, allocator: runtime.Allocator) -> [dynamic]string {
path: string,
allocator := context.allocator,
) -> [dynamic]string {
files := make([dynamic]string, allocator) files := make([dynamic]string, allocator)
@ -70,9 +70,10 @@ walk_tree :: proc(path: string) -> []u8 {
} }
/*
parse_file_md_to_html :: proc( parse_file_md_to_html :: proc(
filename: string, filename: string,
allocator := context.allocator, allocator: runtime.Allocator,
) -> ( ) -> (
parsed: cstring, parsed: cstring,
err: os.Error, err: os.Error,
@ -88,3 +89,24 @@ parse_file_md_to_html :: proc(
return html, nil 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
}