Skip to content

Commit

Permalink
update directory writer
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 18, 2023
1 parent 85ae7e5 commit 221e901
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 92 deletions.
203 changes: 119 additions & 84 deletions .github/workflows/directory.jule
Original file line number Diff line number Diff line change
@@ -1,110 +1,145 @@
use std::process::{exit}
use std::fs::{FsError, OFlag, open, read_dir}
use std::fs::{DirEntry, FsError, OFlag, open, read_dir}

static mut TABLE: str = ""

static PACKAGES: [...]str = [
"./math",
"./search",
"./sort",
"./string",
"./math",
"./search",
"./sort",
"./string",
]

fn workflow_failed(message: str) {
const FAIL_EXIT_CODE = 1
outln(message)
exit(FAIL_EXIT_CODE)
const FAIL_EXIT_CODE = 1
outln(message)
exit(FAIL_EXIT_CODE)
}

fn write(text: str) { TABLE += text }
fn write_line(text: str) { write(text + "\n") }

fn sort_dirents(mut &dirents: []&DirEntry) {
let less = fn(s1: str, s2: str): bool {
let s1r = ([]rune)(s1)
let s2r = ([]rune)(s2)

let mut n = s1r.len
if n > s2r.len {
n = s2r.len
}

let mut i = 0
for i < n; i++ {
let ir = s1r[i]
let jr = s2r[i]

if ir != jr {
ret ir < jr
}
}

ret s1r.len < s2r.len
}

let mut i = 0
for i < dirents.len-1; i++ {
let mut j = 0
for j < dirents.len-i-1; j++ {
if less(dirents[j+1].name, dirents[j].name) {
dirents[j+1], dirents[j] = dirents[j], dirents[j+1]
}
}
}
}

fn make_name(mut s: str): str {
// Remove all cutset substrings.
let cutset_subs = [
".jule",
".",
"/",
"\\",
]
for _, subs in cutset_subs {
s = s.replace(subs, "", -1)
}

// Replace all underlines with space.
s = s.replace("_", " ", -1)

// Make uppercase first character of all words.
let mut i = 0
let mut wait_space = false
for i < s.len; i++ {
let b = s[i]
if wait_space {
if b == ' ' {
wait_space = false
}
continue
}

s[i] = 'Z' - ('z' - b)

wait_space = true
}

ret s
// Remove all cutset substrings.
let cutset_subs = [
".jule",
".",
"/",
"\\",
]
for _, subs in cutset_subs {
s = s.replace(subs, "", -1)
}

// Replace all underlines with space.
s = s.replace("_", " ", -1)

// Make uppercase first character of all words.
let mut i = 0
let mut wait_space = false
for i < s.len; i++ {
let b = s[i]
if wait_space {
if b == ' ' {
wait_space = false
}
continue
}

s[i] = 'Z' - ('z' - b)

wait_space = true
}

ret s
}

fn append_package(package: str) {
let (dirents, err) = read_dir(package)
if err != FsError.Ok {
workflow_failed("package did not readed: " + package)
}

let package_table_name = make_name(package)

write_line("")
write_line("")
write("## ")
write(package_table_name)
for _, dirent in dirents {
// Ignore sub-diretories.
if dirent.stat.is_dir() {
continue
}

let file_table_name = make_name(dirent.name)
let file_url = package + "/" + dirent.name

write_line("")
write(" - [")
write(file_table_name)
write("](")
write(file_url)
write(")")
}
let (mut dirents, err) = read_dir(package)
if err != FsError.Ok {
workflow_failed("package did not readed: " + package)
}

let package_table_name = make_name(package)

write_line("")
write_line("")
write("## ")
write(package_table_name)
sort_dirents(dirents)
for _, dirent in dirents {
// Ignore sub-diretories.
if dirent.stat.is_dir() {
continue
}

let file_table_name = make_name(dirent.name)
let file_url = package + "/" + dirent.name

write_line("")
write(" - [")
write(file_table_name)
write("](")
write(file_url)
write(")")
}
}

fn create_directory_md() {
const MD_PATH = "./DIRECTORY.md"
let (mut f, mut err) = open(MD_PATH, OFlag.Rdwr|OFlag.Create|OFlag.Trunc, 0o666)
if err != FsError.Ok {
workflow_failed("a problem occurs when creating " + MD_PATH)
}

defer { f.close() }

_, err = f.write(([]byte)(TABLE))
if err != FsError.Ok {
workflow_failed("a problem occurs when creating " + MD_PATH)
}
const MD_PATH = "./DIRECTORY.md"
let (mut f, mut err) = open(MD_PATH, OFlag.Rdwr|OFlag.Create|OFlag.Trunc, 0o666)
if err != FsError.Ok {
workflow_failed("a problem occurs when creating " + MD_PATH)
}

defer { f.close() }

_, err = f.write(([]byte)(TABLE))
if err != FsError.Ok {
workflow_failed("a problem occurs when creating " + MD_PATH)
}
}

fn main() {
write_line("# Table of Contents")
write_line("# Table of Contents")

for _, package in PACKAGES {
append_package(package)
}
for _, package in PACKAGES {
append_package(package)
}

create_directory_md()
create_directory_md()
}
16 changes: 8 additions & 8 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@


## Math
- [Median](./math/median.jule)
- [Abs](./math/abs.jule)
- [Median](./math/median.jule)

## Search
- [Linear Search](./search/linear_search.jule)

## Sort
- [Shell Sort](./sort/shell_sort.jule)
- [Bubble Sort](./sort/bubble_sort.jule)
- [Selection Sort](./sort/selection_sort.jule)
- [Exchange Sort](./sort/exchange_sort.jule)
- [Quick Sort](./sort/quick_sort.jule)
- [Insertion Sort](./sort/insertion_sort.jule)
- [Quick Sort](./sort/quick_sort.jule)
- [Selection Sort](./sort/selection_sort.jule)
- [Shell Sort](./sort/shell_sort.jule)
- [Simple Sort](./sort/simple_sort.jule)

## String
- [Upper Case](./string/upper_case.jule)
- [Is Digit](./string/is_digit.jule)
- [Capitalize](./string/capitalize.jule)
- [Lower Case](./string/lower_case.jule)
- [Is Alpha](./string/is_alpha.jule)
- [Reverse](./string/reverse.jule)
- [Is Digit](./string/is_digit.jule)
- [Lower Case](./string/lower_case.jule)
- [Reverse](./string/reverse.jule)
- [Upper Case](./string/upper_case.jule)

0 comments on commit 221e901

Please sign in to comment.