Skip to content

Commit 3f31a02

Browse files
committed
lua: Abstract data parsing code out into reusable library/module
1 parent e55a8dd commit 3f31a02

File tree

2 files changed

+96
-90
lines changed

2 files changed

+96
-90
lines changed

lib/data/lua.data.js

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,8 @@
1-
import { getVitepressMd } from '../markdown.js'
2-
import { addWatchPaths, loadData } from '../utility.js'
3-
4-
async function normalizeLuaConstants(lua) {
5-
const md = await getVitepressMd()
6-
const out = {}
7-
8-
for (const v of lua.values()) {
9-
if (v.text) {
10-
v.text = md.render(v.text)
11-
}
12-
13-
for (const tag of v.tags) {
14-
const v2 = structuredClone(v)
15-
v2.tags = tag
16-
out[tag + '.' + v.name] = v2
17-
}
18-
}
19-
20-
return out
21-
}
22-
23-
async function normalizeLuaFunctions(lua) {
24-
const md = await getVitepressMd()
25-
let set = false
26-
const out = {}
27-
28-
for (const v of lua.values()) {
29-
if (v.args) {
30-
for (const [k2, v2] of Object.entries(v.args)) {
31-
/* Merge information from Dovecot settings. */
32-
if (v2.dovecot_setting) {
33-
if (!set) {
34-
set = structuredClone(loadData('settings').settings)
35-
}
36-
37-
if (!v2.type) {
38-
v2.type = set[v2.dovecot_setting].values?.label
39-
}
40-
41-
if (!v2.text) {
42-
v2.text = set[v2.dovecot_setting].text.trim()
43-
}
44-
45-
if (v2.default === undefined) {
46-
v2.default = set[v2.dovecot_setting].default
47-
}
48-
}
49-
50-
v2.text = md.render(v2.text)
51-
}
52-
}
53-
54-
v.text = md.render(v.text)
55-
56-
for (const tag of v.tags) {
57-
const v2 = structuredClone(v)
58-
v2.tags = tag
59-
out[tag + '.' + v.name] = v2
60-
}
61-
}
62-
63-
return out
64-
}
65-
66-
async function normalizeLuaVariables(lua) {
67-
const md = await getVitepressMd()
68-
const out = {}
69-
70-
for (const v of lua.values()) {
71-
if (v.text) {
72-
v.text = md.render(v.text)
73-
}
74-
75-
for (const tag of v.tags) {
76-
const v2 = structuredClone(v)
77-
v2.tags = tag
78-
out[tag + '.' + v.name] = v2
79-
}
80-
}
81-
82-
return out
83-
}
1+
import { addWatchPaths } from '../utility.js'
2+
import { loadLua } from '../lua.js'
843

854
export default addWatchPaths({
865
async load() {
87-
const data = loadData('lua')
88-
89-
return {
90-
constants: await normalizeLuaConstants(data.lua_constants),
91-
functions: await normalizeLuaFunctions(data.lua_functions),
92-
variables: await normalizeLuaVariables(data.lua_variables)
93-
}
6+
return await loadLua()
947
}
958
})

lib/lua.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { getVitepressMd } from './markdown.js'
2+
import { loadData } from './utility.js'
3+
4+
async function normalizeLuaConstants(lua) {
5+
const md = await getVitepressMd()
6+
const out = {}
7+
8+
for (const v of lua.values()) {
9+
if (v.text) {
10+
v.text = md.render(v.text)
11+
}
12+
13+
for (const tag of v.tags) {
14+
const v2 = structuredClone(v)
15+
v2.tags = tag
16+
out[tag + '.' + v.name] = v2
17+
}
18+
}
19+
20+
return out
21+
}
22+
23+
async function normalizeLuaFunctions(lua) {
24+
const md = await getVitepressMd()
25+
let set = false
26+
const out = {}
27+
28+
for (const v of lua.values()) {
29+
if (v.args) {
30+
for (const [k2, v2] of Object.entries(v.args)) {
31+
/* Merge information from Dovecot settings. */
32+
if (v2.dovecot_setting) {
33+
if (!set) {
34+
set = structuredClone(loadData('settings').settings)
35+
}
36+
37+
if (!v2.type) {
38+
v2.type = set[v2.dovecot_setting].values?.label
39+
}
40+
41+
if (!v2.text) {
42+
v2.text = set[v2.dovecot_setting].text.trim()
43+
}
44+
45+
if (v2.default === undefined) {
46+
v2.default = set[v2.dovecot_setting].default
47+
}
48+
}
49+
50+
v2.text = md.render(v2.text)
51+
}
52+
}
53+
54+
v.text = md.render(v.text)
55+
56+
for (const tag of v.tags) {
57+
const v2 = structuredClone(v)
58+
v2.tags = tag
59+
out[tag + '.' + v.name] = v2
60+
}
61+
}
62+
63+
return out
64+
}
65+
66+
async function normalizeLuaVariables(lua) {
67+
const md = await getVitepressMd()
68+
const out = {}
69+
70+
for (const v of lua.values()) {
71+
if (v.text) {
72+
v.text = md.render(v.text)
73+
}
74+
75+
for (const tag of v.tags) {
76+
const v2 = structuredClone(v)
77+
v2.tags = tag
78+
out[tag + '.' + v.name] = v2
79+
}
80+
}
81+
82+
return out
83+
}
84+
85+
export async function loadLua() {
86+
const data = loadData('lua')
87+
88+
return {
89+
constants: await normalizeLuaConstants(data.lua_constants),
90+
functions: await normalizeLuaFunctions(data.lua_functions),
91+
variables: await normalizeLuaVariables(data.lua_variables)
92+
}
93+
}

0 commit comments

Comments
 (0)