-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_opt_doc.lua
More file actions
130 lines (111 loc) · 2.47 KB
/
gen_opt_doc.lua
File metadata and controls
130 lines (111 loc) · 2.47 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
local RUNTIME = vim.env.VIMRUNTIME
local opts = {}
local opt
local section = 0
local got_tag = false
local function iter(line)
if line:find('^================') then
section = section + 1
return
elseif section ~= 3 then
return
elseif line:find("^%s*%*'%S+'%*") then
got_tag = true
return
end
if got_tag and line:find("^'") then
opt = { line }
opts[#opts+1] = opt
elseif opt then
opt[#opt+1] = line
end
got_tag = false
end
for line in io.lines(RUNTIME..'/doc/options.txt') do
iter(line)
end
do
-- remove modeline
local last = opts[#opts]
if last and last[#last]:find('^%s*vim:') then
last[#last] = nil
end
end
local res = {}
for _, o in ipairs(opts) do
-- remove lines with just tags
while o[#o] and o[#o]:find("^<?%s*%*'%S+'%*") and o[#o]:gsub("^'%S+'", ''):match('^%s*$') do
if o[#o]:find('^<') then
o[#o] = '<'
else
o[#o] = nil
end
end
-- parse name
local name = assert(assert(o[1]):match("^'(%l+)'"))
-- remove type and default info
if o[1]:find('(', 1, true) and not o[1]:find('%)$') then
for i = 2, #o do
if o[i]:find('%)$') then
for j = i, 2, -1 do
table.remove(o, j)
end
break
end
end
end
-- remove first line with option names
table.remove(o, 1)
-- remove global/local info
if o[1] and (o[1]:find('^\t\t\tglobal') or o[1]:find('^\t\t\tlocal')) then
table.remove(o, 1)
end
--- format code blocks
local closed = true
do
local i = 1
while true do
if o[i] == '>' then
o[i] = '```'
closed = false
i = i + 1
elseif o[i]:find(' >$') then
o[i] = o[i]:gsub('%s*>$', '')
table.insert(o, i + 1, '```')
closed = false
i = i + 2
elseif o[i]:find('^<%s*$') then
o[i] = '```'
closed = true
i = i + 1
elseif o[i]:find('^<') then
o[i] = o[i]:sub(2)
table.insert(o, i, '```')
closed = true
i = i + 2
else
i = i + 1
end
if i > #o then
break
end
end
end
-- remove leading and trailing empty lines
while o[1] and not o[1]:find('%S') do
table.remove(o, 1)
end
while o[#o] and not o[#o]:find('%S') do
o[#o] = nil
end
--- close code blocks
if not closed then
o[#o+1] = '```'
end
-- remove leading indentation (should be just one level)
for i, v in ipairs(o) do
o[i] = v:gsub('^\t', '')
end
res[name] = o
end
return res