-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory-tree.lua
82 lines (75 loc) · 2.03 KB
/
directory-tree.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
tex = require "tex";
function _print(...)
tex.print(...);
print(...);
end
local directoryTree = {
exclude = "^%.%l";
}
directoryTree.label = function (attr, name)
local label = "\\textit{WARNING: NO LABEL}";
label = name:gsub("_","\\_");
if attr.mode == "directory" then
label = "\\dtDirLabel{" .. label .. '/}';
else
label = "\\dtFileLabel{" .. label .. '}';
end
return label;
end
directoryTree.style = function (attr, path)
local style = "";
if attr.mode == "directory" then
style = "directory";
else
style = "file";
end
return style;
end
directoryTree.printNode = function (path, level, listoffiles)
local node_cnt = 0;
if not level then level = 0; end
if not listoffiles then listoffiles = {} end
local attr, msg = lfs.attributes(path);
if (not attr) and msg then
tex.error(msg);
else
local name = path:gsub(".*/","");
if name:find(directoryTree.exclude) then
return node_cnt, listoffiles;
end
local label = directoryTree.label(attr, name);
local style = directoryTree.style(attr, path);
if level == 0 then
_print("\\node[" .. style .. "] {" .. label .. "}");
else
_print("child{ node[" .. style .. "] {" .. label .. "}");
end
if attr.mode == "directory" then
for entry in lfs.dir(path) do
if entry ~= "." and entry ~= ".." then
local n_cnt, listoffiles = directoryTree.printNode(path .. '/' .. entry, level+1, listoffiles);
node_cnt = node_cnt + n_cnt;
end
end
else
table.insert(listoffiles, path);
end
if level == 0 then
_print(";");
else
_print("}");
for _=1,node_cnt do
_print("child[missing] {}");
end
end
end
return node_cnt+1, listoffiles;
end
directoryTree.createTree = function(path, name)
local _, listoffiles = directoryTree.printNode(path);
local files = table.concat(listoffiles, ", ");
if name then
_print('\\makeatletter\\gdef\\dt@listoffiles@'..name..'{'..files..'}\\makeatother');
end
end
return directoryTree;