aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/external.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/muwiki/external.lua')
-rw-r--r--lua/muwiki/external.lua59
1 files changed, 59 insertions, 0 deletions
diff --git a/lua/muwiki/external.lua b/lua/muwiki/external.lua
new file mode 100644
index 0000000..cf26aca
--- /dev/null
+++ b/lua/muwiki/external.lua
@@ -0,0 +1,59 @@
+local config = require('muwiki.config')
+local paths = require('muwiki.paths')
+
+local M = {}
+
+function M.open(url)
+ if type(url) ~= 'string' then
+ vim.notify('Invalid URL type', vim.log.levels.ERROR)
+ return false
+ end
+
+ local valid, err = paths.validate_url_scheme(url)
+ if not valid then
+ vim.notify(err, vim.log.levels.ERROR)
+ return false
+ end
+
+ vim.system({ 'xdg-open', url }, { detach = true })
+ return true
+end
+
+function M.execute(handler, url)
+ if type(handler.cmd) == 'function' then
+ handler.cmd(url)
+ else
+ vim.system({ handler.cmd, url }, { detach = true })
+ end
+end
+
+function M.matches(handler, url)
+ local pattern = handler.pattern
+
+ if pattern == nil then
+ return true
+ end
+
+ if type(pattern) == 'string' then
+ return url:match(pattern) ~= nil
+ end
+
+ for _, p in ipairs(pattern) do
+ if url:match(p) then
+ return true
+ end
+ end
+ return false
+end
+
+function M.get_matching(url)
+ local matching = {}
+ for _, handler in ipairs(config.options.external_handlers) do
+ if M.matches(handler, url) then
+ table.insert(matching, handler)
+ end
+ end
+ return matching
+end
+
+return M