forked from maskudo/devdocs.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.lua
More file actions
308 lines (280 loc) · 8.47 KB
/
docs.lua
File metadata and controls
308 lines (280 loc) · 8.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
---@class Docs
local M = {}
local C = require('devdocs.constants')
--- @class Devdoc
--- @field db_size number Database size in bytes
--- @field links DevdocLink Table containing links related to the documentation
--- @field mtime number Last modified time (UNIX timestamp)
--- @field name string Name of the documentation/project
--- @field release string Version release
--- @field slug string Unique identifier slug
--- @field type string Type/category of the documentation
--- @field version string Version string (can be empty)
--- @class DevdocLink
--- @field code string? Optional GitHub/Code repository link
--- @field home string? Optional homepage link
--- @class DevdocStatus
--- @field downloaded boolean
--- @field extracted boolean
---@class Doc
---Creates a directory using a shell command native to the platform
---@param dir string Directory to create
M.Mkdir = function(dir)
os.execute('mkdir -p ' .. dir)
end
-- Update for windows
if vim.fn.has('win32') == 1 or vim.fn.has('win64') == 1 or os.getenv('OS') == 'Windows_NT' then
M.Mkdir = function(dir)
os.execute(
"powershell.exe -NoLogo -NonInteractive -NoProfile -Command New-Item -ErrorAction SilentlyContinue -ItemType Directory -Force -Path '"
.. dir
.. "'"
)
end
end
---Initialize DevDocs directories
M.InitializeDirectories = function()
M.Mkdir(C.DEVDOCS_DATA_DIR)
M.Mkdir(C.DOCS_DIR)
local dataDirExists = vim.fn.mkdir(C.DEVDOCS_DATA_DIR, 'p') == 1
local docsDirExists = vim.fn.mkdir(C.DOCS_DIR, 'p') == 1
assert(dataDirExists and docsDirExists, 'Error initializing DevDocs directories')
end
---
---Initialize devdocs for metadata.
---Downloads a list of all available docs
---@param opts { force: boolean } | nil
---@param callback function|nil Function called after fetching metadata
---@return nil
M.InitializeMetadata = function(opts, callback)
if opts and opts.force then
return M.FetchDevdocsMetadata(callback)
end
local metadata = require('devdocs.state'):Get('metadata')
if metadata and metadata.downloaded then
if callback ~= nil then
return callback()
end
end
M.FetchDevdocsMetadata(callback)
end
---Fetches and stores metadata in ${DEVDOCS_DATA_DIR}/metadata.json
---@param onComplete function|nil
M.FetchDevdocsMetadata = function(onComplete)
vim.system({
'curl',
'-s',
'https://devdocs.io/docs/docs.json',
'-o',
C.METADATA_FILE,
}, { text = false }, function(res)
if res.code == 0 then
require('devdocs.state'):Update('metadata', {
downloaded = true,
})
if onComplete ~= nil then
onComplete()
end
else
print('Error Downloading metadata')
end
end)
end
---Returns available dev docs
---@return Devdoc[]
M.GetDownloadableDocs = function()
local file = io.open(C.METADATA_FILE, 'r')
if not file then
vim.notify('No available docs. Use DevDocsFetch to fetch them.')
return {}
end
local text = file:read('*a')
local availableDocs = vim.json.decode(text)
return availableDocs
end
---Constructs download link for a doc(slug)
---@param doc doc
---@return string
M.ConstructDownloadLink = function(doc)
return 'https://documents.devdocs.io/' .. doc .. '/db.json'
end
---Downloads json docs for any specified doc
---@param slug string Doc to be downloaded
---@param callback function Function called after download
M.DownloadDocs = function(slug, callback)
local downloadLink = 'https://documents.devdocs.io/' .. slug .. '/db.json'
vim.system({
'curl',
'-s',
downloadLink,
}, { text = false }, function(res)
assert(res.code == 0, 'Error downloading docs')
vim.system({
'jq',
'-c',
'to_entries[]',
}, { test = false, stdin = res.stdout }, function(ndjson)
assert(ndjson.code == 0, 'Error processing json for ' .. slug)
local f = io.open(C.DOCS_DIR .. '/' .. slug .. '.json', 'w')
assert(f, 'Error creating file for ' .. slug)
local _, err = f:write(ndjson.stdout)
assert(not err, 'Error writing')
f:close()
require('devdocs.state'):Update(slug, {
downloaded = true,
})
callback()
end)
end)
end
---Extracts json docs into markdown files
---@param slug string Doc to be extracted
---@param callback function? Function called after extraction
M.ExtractDocs = function(slug, callback)
local filepath = C.DOCS_DIR .. '/' .. slug .. '.json'
local activeJobs = 0
local MAX_ACTIVE_JOBS = 5
local getDocsData = coroutine.create(function()
for line in io.lines(filepath) do
local entry = vim.json.decode(
line,
{ luanil = {
object = true,
array = true,
} }
)
local title = entry.key
local htmlContent = entry.value
local parts = vim.split(title, '/', { trimempty = true, plain = true })
local filename = table.remove(parts, #parts) .. '.md'
local dir = C.DOCS_DIR
.. '/'
.. slug
.. (#parts > 0 and '/' .. table.concat(parts, '/') or '')
local outputFile = dir .. '/' .. filename
M.Mkdir(dir)
coroutine.yield({ outputFile = outputFile, htmlContent = htmlContent })
end
end)
local function processDocs()
if activeJobs <= MAX_ACTIVE_JOBS and coroutine.status(getDocsData) ~= 'dead' then
local success, job = coroutine.resume(getDocsData)
if success and job then
activeJobs = activeJobs + 1
M.ConvertHtmlToMarkdown(job.htmlContent, job.outputFile, function()
activeJobs = activeJobs - 1
vim.defer_fn(processDocs, 0)
end)
end
elseif coroutine.status(getDocsData) == 'dead' then
if activeJobs == 0 then
require('devdocs.state'):Update(slug, {
downloaded = true,
extracted = true,
})
vim.schedule(function()
vim.notify('Downloaded Docs for ' .. slug .. ' successfully')
end)
if callback ~= nil then
callback()
end
end
else
vim.defer_fn(processDocs, 0)
end
end
for _ = 1, MAX_ACTIVE_JOBS do
processDocs()
end
end
---Downloads json docs for any specified doc
---@param slug doc Doc to be downloaded
M.InstallDocs = function(slug)
M.DownloadDocs(slug, function()
M.ExtractDocs(slug)
end)
end
---Converts html documents to a bunch of markdown files
---@param htmlContent string
---@param outputFile string File the output markdown is stored on
---@param callback function Function called after conversion
M.ConvertHtmlToMarkdown = function(htmlContent, outputFile, callback)
vim.system({
'pandoc',
'-f',
'html',
'-t',
'markdown',
'-o',
outputFile,
}, { stdin = htmlContent }, function(res)
callback()
assert(res.code == 0, 'Error converting to markdown:', res.stderr)
end)
end
---Get downloaded/extracted status of docs
---@param slug string The doc
---@return DevdocStatus {downloaded: string, extracted: string}
M.GetDocStatus = function(slug)
local status = require('devdocs.state'):Get(slug)
return status
end
---Get all available docs
--- @alias doc string
---@return {doc: boolean}
M.GetAvailableDocs = function()
local availableDocs = M.GetDownloadableDocs()
local set = {}
for _, doc in ipairs(availableDocs) do
set[doc.slug] = true
end
return set
end
---Get all installed docs
---@return {doc:doc}
M.GetInstalledDocs = function()
local state = require('devdocs.state').state
local installed = {}
for doc, status in pairs(state) do
if doc ~= 'metadata' and status.extracted then
table.insert(installed, doc)
end
end
return installed
end
---Check if doc is available for download
---@param doc string
---@return boolean
M.ValidateDocAvailability = function(doc)
local availableDocs = M.GetAvailableDocs()
return availableDocs[doc] or false
end
---Separated provided docs into valid and invalid docs
---@param docs doc[]
---@return table {validDocs: string[], invalidDocs: string[]}
M.ValidateDocsAvailability = function(docs)
local availableDocs = M.GetAvailableDocs()
local invalidDocs = {}
local validDocs = {}
for _, doc in ipairs(docs) do
if availableDocs[doc] == true then
table.insert(validDocs, doc)
else
table.insert(invalidDocs, doc)
end
end
return { validDocs = validDocs, invalidDocs = invalidDocs }
end
---Returns doc filepaths
---@param doc doc
---@return string[] | nil
M.GetDocFiles = function(doc)
if not doc then
return nil
end
local files = vim.fs.find(function()
return true
end, { limit = math.huge, type = 'file', path = C.DOCS_DIR .. '/' .. doc })
return files
end
return M