|
| 1 | +local utils = require('orgmode.utils') |
| 2 | +local fs = require('orgmode.utils.fs') |
| 3 | + |
| 4 | +---@class Url |
| 5 | +---@field str string |
| 6 | +local Url = {} |
| 7 | + |
| 8 | +function Url:init(str) |
| 9 | + self.str = str |
| 10 | +end |
| 11 | + |
| 12 | +function Url.new(str) |
| 13 | + local self = setmetatable({}, { __index = Url }) |
| 14 | + self:init(str) |
| 15 | + return self |
| 16 | +end |
| 17 | + |
| 18 | +---@return boolean |
| 19 | +function Url:is_file_line_number() |
| 20 | + return self:get_linenumber() and true |
| 21 | +end |
| 22 | + |
| 23 | +---@return boolean |
| 24 | +function Url:is_headline() |
| 25 | + return self:is_file_headline() or self:is_internal_headline() |
| 26 | +end |
| 27 | + |
| 28 | +---@return boolean |
| 29 | +function Url:is_file_headline() |
| 30 | + return self:is_file() and self:get_headline() and true |
| 31 | +end |
| 32 | + |
| 33 | +function Url:is_custom_id() |
| 34 | + return self:is_file_custom_id() or self:is_internal_custom_id() |
| 35 | +end |
| 36 | + |
| 37 | +---@return boolean |
| 38 | +function Url:is_file_custom_id() |
| 39 | + return self:is_file() and self:get_custom_id() and true |
| 40 | +end |
| 41 | + |
| 42 | +---@return boolean |
| 43 | +function Url:is_file_anchor() |
| 44 | + return self:get_dedicated_target() and true |
| 45 | +end |
| 46 | + |
| 47 | +---@return boolean |
| 48 | +function Url:is_org_link() |
| 49 | + return (self:get_dedicated_target() or self:get_custom_id() or self:get_headline()) and true |
| 50 | +end |
| 51 | + |
| 52 | +function Url:is_file() |
| 53 | + return self.str:find('^file:') or self.str:find('^./') or self.str:find('^/') |
| 54 | +end |
| 55 | + |
| 56 | +function Url:is_file_plain() |
| 57 | + return self:is_file() and not self:is_org_link() and not self:is_file_line_number() |
| 58 | +end |
| 59 | + |
| 60 | +---@return boolean |
| 61 | +function Url:is_http_url() |
| 62 | + return self:get_http_url() and true |
| 63 | +end |
| 64 | + |
| 65 | +---@return boolean |
| 66 | +function Url:is_internal_headline() |
| 67 | + return self.str:find('^*') and true |
| 68 | +end |
| 69 | + |
| 70 | +function Url:is_internal_custom_id() |
| 71 | + return self.str:find('^#') |
| 72 | +end |
| 73 | + |
| 74 | +function Url:is_dedicated_anchor_or_internal_title() |
| 75 | + return self:get_dedicated_target() ~= nil |
| 76 | +end |
| 77 | + |
| 78 | +---@return string | false |
| 79 | +function Url:extract_path() |
| 80 | + local url = self |
| 81 | + if url:is_file_headline() or url:is_file_custom_id() then |
| 82 | + return url.str:match('^file:([^:]-)::') or url.str:match('^(./[^:]-)::') or url.str:match('^(/[^:]-)::') |
| 83 | + elseif url:is_file_line_number() then |
| 84 | + return url.str:match('^file:([^:]-) %+') or url.str:match('^(./[^:]-) %+') or url.str:match('^(/[^:]-) %+') |
| 85 | + elseif url:is_file_plain() then |
| 86 | + return url.str:match('^file:([^:]-)$') or url.str:match('^(./[^:]-)$') or url.str:match('^(/[^:]-)$') |
| 87 | + else |
| 88 | + return false |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +---@return string | false |
| 93 | +function Url:get_file_real_path() |
| 94 | + local filepath = self:get_filepath() |
| 95 | + return filepath and fs.get_real_path(filepath) |
| 96 | +end |
| 97 | + |
| 98 | +---@return string | false |
| 99 | +function Url:get_headline() |
| 100 | + return self.str:match('^file:[^:]+::%*(.-)$') |
| 101 | + or self.str:match('^./[^:]+::%*(.-)$') |
| 102 | + or self.str:match('^/[^:]+::%*(.-)$') |
| 103 | + or self.str:match('^%*(.-)$') |
| 104 | +end |
| 105 | + |
| 106 | +---@return string | false |
| 107 | +function Url:get_custom_id() |
| 108 | + return self.str:match('^file:[^:]+::#(.-)$') |
| 109 | + or self.str:match('^./[^:]+::#(.-)$') |
| 110 | + or self.str:match('^/[^:]+::#(.-)$') |
| 111 | + or self.str:match('^#(.-)$') |
| 112 | +end |
| 113 | + |
| 114 | +---@return number | false |
| 115 | +function Url:get_linenumber() |
| 116 | + -- official orgmode convention |
| 117 | + return self.str:match('^file:[^:]+::(%d+)$') |
| 118 | + or self.str:match('^./[^:]+::(%d+)$') |
| 119 | + or self.str:match('^/[^:]+::(%d+)$') |
| 120 | + -- for backwards compatibility |
| 121 | + or self.str:match('^file:[^:]+ %+(%d+)$') |
| 122 | + or self.str:match('^./[^:]+ %+(%d+)$') |
| 123 | + or self.str:match('^/[^:]+ %+(%d+)$') |
| 124 | +end |
| 125 | + |
| 126 | +---@return string | false |
| 127 | +function Url:get_filepath() |
| 128 | + return |
| 129 | + -- for backwards compatibility |
| 130 | + self.str:match('^file:([^:]+) %+%d+') |
| 131 | + or self.str:match('^(%./[^:]+) %+%d+') |
| 132 | + or self.str:match('^(/[^:]+) %+%d+') |
| 133 | + -- official orgmode convention |
| 134 | + or self.str:match('^file:([^:]+)::') |
| 135 | + or self.str:match('^(%./[^:]+)::') |
| 136 | + or self.str:match('^(/[^:]+)::') |
| 137 | + or self.str:match('^file:([^:]+)$') |
| 138 | + or self.str:match('^(%./[^:]+)$') |
| 139 | + or self.str:match('^(/[^:]+)$') |
| 140 | + or self.str:match('^(%./)$') |
| 141 | + or self.str:match('^(/)$') |
| 142 | +end |
| 143 | +-- |
| 144 | +---@return string |
| 145 | +function Url:get_headline_completion() |
| 146 | + return self.str:match('^.+::%*(.*)$') or self.str:match('^%*(.*)$') |
| 147 | +end |
| 148 | + |
| 149 | +---@return string |
| 150 | +function Url:get_custom_id_completion() |
| 151 | + return self.str:match('^.+::#(.*)$') or self.str:match('^#(.*)$') |
| 152 | +end |
| 153 | + |
| 154 | +---@return string | false |
| 155 | +function Url:get_dedicated_target() |
| 156 | + return not self:get_filepath() |
| 157 | + and not self:get_linenumber() |
| 158 | + and not self:get_headline() |
| 159 | + and self.str:match('^([%w%s%-%+%=_]+)$') |
| 160 | +end |
| 161 | + |
| 162 | +---@return string | false |
| 163 | +function Url:get_http_url() |
| 164 | + return self.str:match('^https?://.+$') |
| 165 | +end |
| 166 | + |
| 167 | +return Url |
0 commit comments