Created
May 11, 2020 04:07
-
-
Save max1220/2e4fd13cc4db019cb4bceb005456d43d to your computer and use it in GitHub Desktop.
Generate simple HTML directory indexes (recursive)!
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
#!/usr/bin/env lua | |
-- return stdout of command as a table containg lines | |
local function stdout_lines(cmd) | |
local proc = io.popen(cmd, "r") | |
local lines = {} | |
for line in proc:lines() do | |
lines[#lines+1] = line | |
end | |
proc:close() | |
return lines | |
end | |
-- strip directory from paths | |
local function basename(path) | |
return (path:gsub("(.*/)(.*)", "%2")) | |
end | |
-- collect file and directory infos | |
local dirs = stdout_lines("find . -maxdepth 1 -type d") | |
local files = stdout_lines("find . -maxdepth 1 -type f") | |
table.sort(dirs) | |
table.sort(files) | |
-- open index file | |
local index_file = io.open("index.html", "w") | |
-- write directories to index file | |
index_file:write('<h2>Directories</h2>\n<ul id="dirs">\n') | |
for _,dir in ipairs(dirs) do | |
if basename(dir):sub(1,1) ~= "." then | |
index_file:write(('\t<li><a href="%s">%s</a></li>\n'):format(dir, basename(dir))) | |
end | |
end | |
-- write files to index file | |
index_file:write('</ul>\n\n<h2>Files</h2>\n<ul id="files">\n') | |
for _,file in ipairs(files) do | |
if (basename(file):sub(1,1) ~= ".") and (file ~= "./index.html") then | |
index_file:write(('\t<li><a href="%s">%s</a></li>\n'):format(file, basename(file))) | |
end | |
end | |
index_file:write('</ul>\n') | |
-- perform recursion(call for each directory) | |
for _, dir in ipairs(dirs) do | |
if basename(dir):sub(1,1) ~= "." then | |
local cmd = ("cd %q; %s"):format(dir, arg[0]) | |
os.execute(cmd) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment