113 lines
2.4 KiB
Odin
113 lines
2.4 KiB
Odin
package main
|
|
|
|
import "base:runtime"
|
|
import "core:fmt"
|
|
import "core:os"
|
|
import "core:strings"
|
|
import cm "vendor:commonmark"
|
|
|
|
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(path: string, allocator: runtime.Allocator) -> [dynamic]string {
|
|
|
|
files := make([dynamic]string, allocator)
|
|
|
|
w := os.walker_create(path)
|
|
defer os.walker_destroy(&w)
|
|
|
|
for info in os.walker_walk(&w) {
|
|
//handle errors
|
|
if path, err := os.walker_error(&w); err != nil {
|
|
fmt.eprintfln("failed walking %s: %s", path, err)
|
|
continue
|
|
}
|
|
|
|
//Skip a dir
|
|
if strings.has_suffix(info.fullpath, ".git") {
|
|
os.walker_skip_dir(&w)
|
|
continue
|
|
}
|
|
|
|
if !strings.has_suffix(info.name, MD_SUFFIX) {
|
|
continue //skip
|
|
}
|
|
|
|
append(&files, strings.clone(info.fullpath, allocator))
|
|
|
|
}
|
|
// Handle error if one happened during iteration at the end:
|
|
if path, err := os.walker_error(&w); err != nil {
|
|
fmt.eprintfln("failed walking %s: %v", path, err)
|
|
}
|
|
return files
|
|
}
|
|
|
|
walk_tree :: proc(path: string) -> []u8 {
|
|
filename: string
|
|
md_file: []u8
|
|
|
|
w := os.walker_create(path)
|
|
defer os.walker_destroy(&w)
|
|
|
|
for info in os.walker_walk(&w) {
|
|
//handle errors
|
|
if path, err := os.walker_error(&w); err != nil {
|
|
fmt.eprintfln("failed walking %s: %s", path, err)
|
|
continue
|
|
}
|
|
//lets play around
|
|
filename = info.name
|
|
if !strings.has_suffix(info.name, MD_SUFFIX) {
|
|
continue
|
|
} else {
|
|
return md_file
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
/*
|
|
parse_file_md_to_html :: proc(
|
|
filename: string,
|
|
allocator: runtime.Allocator,
|
|
) -> (
|
|
parsed: cstring,
|
|
err: os.Error,
|
|
) {
|
|
str: []u8
|
|
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)
|
|
|
|
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
|
|
}
|