forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.lua
More file actions
105 lines (92 loc) · 2.62 KB
/
resources.lua
File metadata and controls
105 lines (92 loc) · 2.62 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
local async = require('plenary.async')
local utils = require('CopilotChat.utils')
local curl = require('CopilotChat.utils.curl')
local files = require('CopilotChat.utils.files')
local file_cache = {}
local url_cache = {}
local M = {}
--- Get data for a file
---@param filename string
---@return string?, string?
function M.get_file(filename)
local filetype = files.filetype(filename)
if not filetype then
return nil
end
local err, stat = async.uv.fs_stat(filename)
if err or not stat then
return nil
end
local modified = stat.mtime.sec
local data = file_cache[filename]
if not data or data._modified < modified then
local content = files.read_file(filename)
if not content or content == '' then
return nil
end
data = {
content = content,
_modified = modified,
}
file_cache[filename] = data
end
return data.content, files.filetype_to_mimetype(filetype)
end
--- Get data for a buffer
---@param bufnr number
---@return string?, string?
function M.get_buffer(bufnr)
if not utils.buf_valid(bufnr) then
return nil
end
local content = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
if not content or #content == 0 then
return nil
end
return table.concat(content, '\n'), files.filetype_to_mimetype(vim.bo[bufnr].filetype)
end
--- Get the content of an URL
---@param url string
---@return string?, string?
function M.get_url(url)
if not url or url == '' then
return nil
end
local ft = files.filetype(url)
local content = url_cache[url]
if not content then
local ok, out = async.util.apcall(utils.system, { 'lynx', '-dump', url })
if ok and out and out.code == 0 then
-- Use lynx to fetch content
content = out.stdout
else
-- Fallback to curl if lynx fails
local response = curl.get(url, { raw = { '-L' } })
if not response or not response.body then
return nil
end
content = vim.trim(response
.body
-- Remove script, style tags and their contents first
:gsub('<script.-</script>', '')
:gsub('<style.-</style>', '')
-- Remove XML/CDATA in one go
:gsub('<!?%[?[%w%s]*%]?>', '')
-- Remove all HTML tags (both opening and closing) in one go
:gsub('<%/?%w+[^>]*>', ' ')
-- Handle common HTML entities
:gsub('&(%w+);', {
nbsp = ' ',
lt = '<',
gt = '>',
amp = '&',
quot = '"',
})
-- Remove any remaining HTML entities (numeric or named)
:gsub('&#?%w+;', ''))
end
url_cache[url] = content
end
return content, files.filetype_to_mimetype(ft)
end
return M