uri.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. -- TODO: This is implemented only for files currently.
  2. -- https://tools.ietf.org/html/rfc3986
  3. -- https://tools.ietf.org/html/rfc2732
  4. -- https://tools.ietf.org/html/rfc2396
  5. local M = {}
  6. local sbyte = string.byte
  7. local schar = string.char
  8. local tohex = require('bit').tohex
  9. local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*'
  10. local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*'
  11. local PATTERNS = {
  12. -- RFC 2396
  13. -- https://tools.ietf.org/html/rfc2396#section-2.2
  14. rfc2396 = "^A-Za-z0-9%-_.!~*'()",
  15. -- RFC 2732
  16. -- https://tools.ietf.org/html/rfc2732
  17. rfc2732 = "^A-Za-z0-9%-_.!~*'()[]",
  18. -- RFC 3986
  19. -- https://tools.ietf.org/html/rfc3986#section-2.2
  20. rfc3986 = "^A-Za-z0-9%-._~!$&'()*+,;=:@/",
  21. }
  22. ---Converts hex to char
  23. ---@param hex string
  24. ---@return string
  25. local function hex_to_char(hex)
  26. return schar(tonumber(hex, 16))
  27. end
  28. ---@param char string
  29. ---@return string
  30. local function percent_encode_char(char)
  31. return '%' .. tohex(sbyte(char), 2)
  32. end
  33. ---@param uri string
  34. ---@return boolean
  35. local function is_windows_file_uri(uri)
  36. return uri:match('^file:/+[a-zA-Z]:') ~= nil
  37. end
  38. ---URI-encodes a string using percent escapes.
  39. ---@param str string string to encode
  40. ---@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil
  41. ---@return string encoded string
  42. function M.uri_encode(str, rfc)
  43. local pattern = PATTERNS[rfc] or PATTERNS.rfc3986
  44. return (str:gsub('([' .. pattern .. '])', percent_encode_char)) -- clamped to 1 retval with ()
  45. end
  46. ---URI-decodes a string containing percent escapes.
  47. ---@param str string string to decode
  48. ---@return string decoded string
  49. function M.uri_decode(str)
  50. return (str:gsub('%%([a-fA-F0-9][a-fA-F0-9])', hex_to_char)) -- clamped to 1 retval with ()
  51. end
  52. ---Gets a URI from a file path.
  53. ---@param path string Path to file
  54. ---@return string URI
  55. function M.uri_from_fname(path)
  56. local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') ---@type string?
  57. local is_windows = volume_path ~= nil
  58. if is_windows then
  59. path = volume_path .. M.uri_encode(fname:gsub('\\', '/'))
  60. else
  61. path = M.uri_encode(path)
  62. end
  63. local uri_parts = { 'file://' }
  64. if is_windows then
  65. table.insert(uri_parts, '/')
  66. end
  67. table.insert(uri_parts, path)
  68. return table.concat(uri_parts)
  69. end
  70. ---Gets a URI from a bufnr.
  71. ---@param bufnr integer
  72. ---@return string URI
  73. function M.uri_from_bufnr(bufnr)
  74. local fname = vim.api.nvim_buf_get_name(bufnr)
  75. local volume_path = fname:match('^([a-zA-Z]:).*')
  76. local is_windows = volume_path ~= nil
  77. local scheme ---@type string?
  78. if is_windows then
  79. fname = fname:gsub('\\', '/')
  80. scheme = fname:match(WINDOWS_URI_SCHEME_PATTERN)
  81. else
  82. scheme = fname:match(URI_SCHEME_PATTERN)
  83. end
  84. if scheme then
  85. return fname
  86. else
  87. return M.uri_from_fname(fname)
  88. end
  89. end
  90. ---Gets a filename from a URI.
  91. ---@param uri string
  92. ---@return string filename or unchanged URI for non-file URIs
  93. function M.uri_to_fname(uri)
  94. local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri)
  95. if scheme ~= 'file' then
  96. return uri
  97. end
  98. local fragment_index = uri:find('#')
  99. if fragment_index ~= nil then
  100. uri = uri:sub(1, fragment_index - 1)
  101. end
  102. uri = M.uri_decode(uri)
  103. --TODO improve this.
  104. if is_windows_file_uri(uri) then
  105. uri = uri:gsub('^file:/+', ''):gsub('/', '\\')
  106. else
  107. uri = uri:gsub('^file:/+', '/') ---@type string
  108. end
  109. return uri
  110. end
  111. ---Gets the buffer for a uri.
  112. ---Creates a new unloaded buffer if no buffer for the uri already exists.
  113. ---@param uri string
  114. ---@return integer bufnr
  115. function M.uri_to_bufnr(uri)
  116. return vim.fn.bufadd(M.uri_to_fname(uri))
  117. end
  118. return M