Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions scripts/directory.nim → .scripts/directory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
##
## # Usage:
## * Navigate to repo's root directory
## * Execute and save the output: `nim r scripts/directory.nim > DIRECTORY.md`
## * Execute and save the output: `nim r .scripts/directory.nim > DIRECTORY.md`
## * Check the changes: `git diff directory.md`
##
## # Overview:
## - Walks the current directory for subdirectories.
## - Each directory is treated as a category.
## - Each subdirectory (but not the root) is treated as a category.
## - The title of the category is inferred from the subdir's name.
## - Walks .nim source files in each subdirectory, non-recursively.
## - Looks for algorithms's title on the first line of the file when it's a
Expand All @@ -24,7 +24,6 @@ const
WhitespaceUC = toRunes("_\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000")
Warning = "<!--- DO NOT EDIT: This file is automatically generated by `directory.nim` -->\n"
Header = "# The Algorithms — Nim: Directory Hierarchy\n"
IgnoreSubDirs = ["scripts"]

type
## TODO: Allow nested structure with variant type [Directory | CritBitTree[string]]
Expand All @@ -49,7 +48,7 @@ proc extractTitle(s, fpath: string): Option[string] =
if s.len > 0: some(s)
else: none(string)
else:
stderr.writeLine(&"\u26A0: \"{fpath}\". First line is not a doc comment! Deriving title from a file name.")
stderr.writeLine(&"\u26A0: \"{fpath}\". First line is not a doc comment! Deriving title from the file name.")
none(string)

proc readLn(fpath: string): Option[string] =
Expand All @@ -68,15 +67,15 @@ proc collectDirectory(dir = ""): Directory =
## Walks the subdirectories of `dir` non-recursively, collects `.nim` files
## and their titles into a sorted structure. Dotfiles are skipped.
for (pc, path) in walkDir(dir, relative = true):
if pc == pcDir and path[0] != '.' and path notin IgnoreSubDirs:
if pc == pcDir and path[0] != '.':
var categoryDir: CritBitTree[string]
for (pc, fname) in walkDir(path, relative = true):
if pc == pcFile and fname[0] != '.':
let (_, name, ext) = splitFile(fname)
let fpath = path / fname
if ext == ".nim":
# if can't read the title from the source, derive from the file name
let title = readLn(fpath).get(name.changeFileExt("").canonicalize())
let title = readLn(fpath).get(name.canonicalize())
categoryDir[title] = fname
if categoryDir.len > 0:
result[path] = categoryDir
Expand Down
2 changes: 1 addition & 1 deletion config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import std/[os, sequtils]
from std/strutils import startsWith, endsWith
from std/strformat import `&`

const IgnorePathPrefixes = [".", "scripts"]
const IgnorePathPrefixes = ["."]

func isIgnored(path: string): bool =
IgnorePathPrefixes.mapIt(path.startsWith(it)).anyIt(it)
Expand Down