protocol-url 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/protocol/url/path.rb +39 -0
- data/lib/protocol/url/pattern.rb +21 -0
- data/lib/protocol/url/reference.rb +1 -0
- data/lib/protocol/url/version.rb +1 -1
- data/lib/protocol/url.rb +1 -14
- data/readme.md +8 -0
- data/releases.md +8 -0
- data.tar.gz.sig +0 -0
- metadata +2 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12a81d3a05c932207c77a583df784431d7120f3f1f9ee68c9ea593a47c666831
|
|
4
|
+
data.tar.gz: 3307c15e0d16d4907259cd186c0483d46d78358e0b1bfee99e3cf3186a1c74d2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8e948fff9470b6676b5b28638af7fdc64eaa31e3565c980071a539eadbf3651b8c10037ed1bcec70b43360a30fbb595f14bf965898e6ab4f1b4e8baf1e942491
|
|
7
|
+
data.tar.gz: 86de1d30699c4c3f7c85878ca3c0edf45da61991593d4600bfc83be2b667525ccc8bb01ea5a51ddf82248045933b49a3ef8879947100a5ac88d67d94a2947786
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/lib/protocol/url/path.rb
CHANGED
|
@@ -119,6 +119,45 @@ module Protocol
|
|
|
119
119
|
return join(simplify(components))
|
|
120
120
|
end
|
|
121
121
|
|
|
122
|
+
# Calculate the relative path from one absolute path to another.
|
|
123
|
+
#
|
|
124
|
+
# This is useful for generating relative URLs from one location to another,
|
|
125
|
+
# such as creating page-specific import maps or relative links.
|
|
126
|
+
#
|
|
127
|
+
# @parameter target [String] The destination path (where you want to go).
|
|
128
|
+
# @parameter from [String] The source path (where you are starting from).
|
|
129
|
+
# @returns [String] The relative path from `from` to `target`.
|
|
130
|
+
#
|
|
131
|
+
# @example Calculate relative path between pages.
|
|
132
|
+
# Path.relative("/_components/app.js", "/foo/bar/")
|
|
133
|
+
# # => "../../_components/app.js"
|
|
134
|
+
#
|
|
135
|
+
# @example Calculate relative path in same directory.
|
|
136
|
+
# Path.relative("/docs/guide.html", "/docs/index.html")
|
|
137
|
+
# # => "guide.html"
|
|
138
|
+
def self.relative(target, from)
|
|
139
|
+
target_components = split(target)
|
|
140
|
+
from_components = split(from)
|
|
141
|
+
|
|
142
|
+
# Remove the last component from 'from' to get the directory
|
|
143
|
+
from_components = from_components[0...-1] if from_components.size > 0
|
|
144
|
+
|
|
145
|
+
# Find the common prefix
|
|
146
|
+
common_length = 0
|
|
147
|
+
[target_components.size, from_components.size].min.times do |i|
|
|
148
|
+
break if target_components[i] != from_components[i]
|
|
149
|
+
common_length = i + 1
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Calculate how many levels to go up
|
|
153
|
+
up_levels = from_components.size - common_length
|
|
154
|
+
|
|
155
|
+
# Build the relative path components
|
|
156
|
+
relative_components = [".."] * up_levels + target_components[common_length..-1]
|
|
157
|
+
|
|
158
|
+
return join(relative_components)
|
|
159
|
+
end
|
|
160
|
+
|
|
122
161
|
# Convert a URL path to a local file system path using the platform's file separator.
|
|
123
162
|
#
|
|
124
163
|
# This method splits the URL path on `/` characters, unescapes each component using
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
|
5
|
+
|
|
6
|
+
module Protocol
|
|
7
|
+
module URL
|
|
8
|
+
# RFC 3986 URI pattern with named capture groups.
|
|
9
|
+
# Matches: [scheme:][//authority][path][?query][#fragment]
|
|
10
|
+
# Rejects strings containing whitespace or control characters (matching standard URI behavior).
|
|
11
|
+
PATTERN = %r{
|
|
12
|
+
\A
|
|
13
|
+
(?:(?<scheme>[a-z][a-z0-9+.-]*):)? # scheme (optional)
|
|
14
|
+
(?://(?<authority>[^/?#\s]*))? # authority (optional, without //, no whitespace)
|
|
15
|
+
(?<path>[^?#\s]*) # path (no whitespace)
|
|
16
|
+
(?:\?(?<query>[^#\s]*))? # query (optional, no whitespace)
|
|
17
|
+
(?:\#(?<fragment>[^\s]*))? # fragment (optional, no whitespace)
|
|
18
|
+
\z
|
|
19
|
+
}ix
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/protocol/url/version.rb
CHANGED
data/lib/protocol/url.rb
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
# Copyright, 2025, by Samuel Williams.
|
|
5
5
|
|
|
6
6
|
require_relative "url/version"
|
|
7
|
+
require_relative "url/pattern"
|
|
7
8
|
require_relative "url/encoding"
|
|
8
9
|
require_relative "url/reference"
|
|
9
10
|
require_relative "url/relative"
|
|
@@ -11,20 +12,6 @@ require_relative "url/absolute"
|
|
|
11
12
|
|
|
12
13
|
module Protocol
|
|
13
14
|
module URL
|
|
14
|
-
# RFC 3986 URI pattern with named capture groups.
|
|
15
|
-
# Matches: [scheme:][//authority][path][?query][#fragment]
|
|
16
|
-
# Rejects strings containing whitespace or control characters (matching standard URI behavior).
|
|
17
|
-
PATTERN = %r{
|
|
18
|
-
\A
|
|
19
|
-
(?:(?<scheme>[a-z][a-z0-9+.-]*):)? # scheme (optional)
|
|
20
|
-
(?://(?<authority>[^/?#\s]*))? # authority (optional, without //, no whitespace)
|
|
21
|
-
(?<path>[^?#\s]*) # path (no whitespace)
|
|
22
|
-
(?:\?(?<query>[^#\s]*))? # query (optional, no whitespace)
|
|
23
|
-
(?:\#(?<fragment>[^\s]*))? # fragment (optional, no whitespace)
|
|
24
|
-
\z
|
|
25
|
-
}ix
|
|
26
|
-
private_constant :PATTERN
|
|
27
|
-
|
|
28
15
|
# Coerce a value into an appropriate URL type (Absolute or Relative).
|
|
29
16
|
#
|
|
30
17
|
# @parameter value [String, Absolute, Relative, nil] The value to coerce.
|
data/readme.md
CHANGED
|
@@ -34,6 +34,14 @@ This project is best served by a collaborative and respectful environment. Treat
|
|
|
34
34
|
|
|
35
35
|
Please see the [project releases](https://github.com/socketry/protocol-urlreleases/index) for all releases.
|
|
36
36
|
|
|
37
|
+
### v0.3.0
|
|
38
|
+
|
|
39
|
+
- Add `relative(target, from)` for computing relative paths between URLs.
|
|
40
|
+
|
|
41
|
+
### v0.2.0
|
|
42
|
+
|
|
43
|
+
- Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
|
|
44
|
+
|
|
37
45
|
### v0.1.0
|
|
38
46
|
|
|
39
47
|
- Initial implementation.
|
data/releases.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Releases
|
|
2
2
|
|
|
3
|
+
## v0.3.0
|
|
4
|
+
|
|
5
|
+
- Add `relative(target, from)` for computing relative paths between URLs.
|
|
6
|
+
|
|
7
|
+
## v0.2.0
|
|
8
|
+
|
|
9
|
+
- Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
|
|
10
|
+
|
|
3
11
|
## v0.1.0
|
|
4
12
|
|
|
5
13
|
- Initial implementation.
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: protocol-url
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Samuel Williams
|
|
@@ -46,6 +46,7 @@ files:
|
|
|
46
46
|
- lib/protocol/url/absolute.rb
|
|
47
47
|
- lib/protocol/url/encoding.rb
|
|
48
48
|
- lib/protocol/url/path.rb
|
|
49
|
+
- lib/protocol/url/pattern.rb
|
|
49
50
|
- lib/protocol/url/reference.rb
|
|
50
51
|
- lib/protocol/url/relative.rb
|
|
51
52
|
- lib/protocol/url/version.rb
|
metadata.gz.sig
CHANGED
|
Binary file
|