Wed, 31 Jan 2018 19:51:10 +0000
added markdown: true/false meta-tag
2 | 1 | |
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
2 | --@module scan |
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
3 | local scan={} |
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
4 | |
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
5 | local lfs=require('lfs') |
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
6 | local path=require('path') |
1 | 7 | |
8 | local function filtered_dir(d) | |
9 | local f, s_, v_ = lfs.dir(d) | |
10 | local function g(s, v) | |
11 | while true do | |
12 | local vn=f(s, v) | |
13 | if vn~='.' and vn~='..' then | |
14 | return vn | |
15 | end | |
16 | end | |
17 | end | |
18 | return g, s_, v_ | |
19 | end | |
20 | ||
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
21 | function scan.scan(d) |
1 | 22 | local h={} |
23 | for f in filtered_dir(d) do | |
24 | local n=d..'/'..f | |
25 | local a=lfs.attributes(n) | |
26 | if a.mode=='directory' then | |
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
27 | local nh=scan.scan(n) |
3 | 28 | h[f]=nh |
1 | 29 | elseif a.mode=='file' then |
2 | 30 | h[f]=true |
1 | 31 | end |
32 | end | |
33 | return h | |
34 | end | |
35 | ||
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
36 | function scan.hfold(h, a, f, g) |
1 | 37 | for n, c in pairs(h) do |
38 | if type(c)=='table' then | |
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
39 | scan.hfold(c, f(a, n), f, g) |
1 | 40 | else |
41 | g(a, n) | |
42 | end | |
43 | end | |
44 | end | |
2 | 45 | |
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
46 | function scan.map(h, g, d) |
2 | 47 | local function dir(prefix, name) |
3 | 48 | local p=path.join(prefix, name) |
49 | if d then | |
50 | d(p) | |
51 | end | |
52 | return p | |
2 | 53 | end |
54 | ||
55 | local function file(prefix, name) | |
56 | local f=path.join(prefix, name) | |
57 | g(f) | |
58 | end | |
59 | ||
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
60 | scan.hfold(h, "", dir, file) |
2 | 61 | end |
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
62 | |
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
63 | return scan |