-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85ae7e5
commit 221e901
Showing
2 changed files
with
127 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters