Wed, 08 Dec 2021 23:32:16 +0200
highlight.js changes update
3 | 1 | -- |
2 | -- ion/share/ioncore_luaext.lua | |
3 | -- | |
4 | -- Copyright (c) Tuomo Valkonen 2004-2009. | |
5 | -- | |
6 | -- See the included file LICENSE for details. | |
7 | -- | |
8 | ||
35
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
9 | local lfs=require("lfs") |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
10 | local path=require("mypath") |
3 | 11 | |
12 | --DOC | |
13 | -- Make \var{str} shell-safe. | |
14 | function string.shell_safe(str) | |
15 | return "'"..string.gsub(str, "'", "'\\''").."'" | |
16 | end | |
17 | ||
18 | ||
19 | --DOC | |
20 | -- Make copy of \var{table}. If \var{deep} is unset, shallow one-level | |
21 | -- copy is made, otherwise a deep copy is made. | |
22 | function table.copy(t, deep) | |
23 | local function docopy(t, deep, seen) | |
24 | local nt={} | |
25 | for k, v in pairs(t) do | |
26 | local v2=v | |
27 | if deep and type(v)=="table" then | |
28 | if seen[v] then | |
29 | error(TR("Recursive table - unable to deepcopy")) | |
30 | end | |
31 | seen[v]=true | |
32 | v2=docopy(v, deep, seen) | |
33 | seen[v]=nil | |
34 | end | |
35 | nt[k]=v2 | |
36 | end | |
37 | return nt | |
38 | end | |
39 | return docopy(t, deep, deep and {}) | |
40 | end | |
41 | ||
42 | ||
43 | --DOC | |
44 | -- Add entries that do not exist in \var{t1} from \var{t2} to \var{t1}. | |
45 | function table.append(t1, t2) | |
46 | for k, v in pairs(t2) do | |
47 | if t1[k]==nil then | |
48 | t1[k]=v | |
49 | end | |
50 | end | |
51 | return t1 | |
52 | end | |
53 | ||
54 | ||
55 | --DOC | |
56 | -- Create a table containing all entries from \var{t1} and those from | |
57 | -- \var{t2} that are missing from \var{t1}. | |
58 | function table.join(t1, t2) | |
59 | return table.append(table.copy(t1, false), t2) | |
60 | end | |
61 | ||
62 | ||
63 | --DOC | |
64 | -- Insert all positive integer entries from t2 into t1. | |
65 | function table.icat(t1, t2) | |
66 | for _, v in ipairs(t2) do | |
67 | table.insert(t1, v) | |
68 | end | |
69 | return t1 | |
70 | end | |
71 | ||
72 | ||
73 | --DOC | |
74 | -- Map all entries of \var{t} by \var{f}. | |
75 | function table.map(f, t) | |
76 | local res={} | |
77 | for k, v in pairs(t) do | |
78 | res[k]=f(v) | |
79 | end | |
80 | return res | |
81 | end | |
82 | ||
83 | ||
84 | --DOC | |
85 | -- Export a list of functions from \var{lib} into global namespace. | |
86 | function export(lib, ...) | |
87 | for k, v in pairs({...}) do | |
88 | _G[v]=lib[v] | |
89 | end | |
90 | end | |
91 | ||
35
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
92 | function io.openX(file, mode) |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
93 | if mode=='w' then |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
94 | local dn = path.dirname(file) |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
95 | if not lfs.attributes(dn) then |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
96 | print("Ensure directory", dn) |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
97 | lfs.mkdir(dn) |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
98 | end |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
99 | end |
2f927eae429b
Don't auto-create directories that will be empty
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
100 | local f, err = io.open(file, mode) |
3 | 101 | if not f then |
102 | error(file..": "..err) | |
103 | end | |
104 | return f | |
105 | end |