URL parsing, building, and relative-resolution utilities implemented as an mq module.
- Parses a URL into
{scheme, userinfo, host, port, path, query, fragment} - Builds a URL string back from that same components dict
- Parses/builds query strings to and from dicts (URL-decoded/encoded)
resolve(base, rel)resolves a relative reference against a base URL the way a browser resolves a link on a page — handles./,../, absolute paths, scheme-relative (//host/...), and fragment/query-only referencesis_absolute/is_relativepredicates
Copy url.mq to your mq module directory, or place it anywhere and reference it with -L.
cp url.mq ~/.local/mq/config/If mq was built with the http-import feature, you can import directly from GitHub without any local setup:
mq -I raw 'import "github.com/harehare/url.mq" | url::parse(.)' <<< "https://example.com/path"Pin to a specific release with @vX.Y.Z:
mq -I raw 'import "github.com/harehare/url.mq@v0.1.0" | ...'mq -L /path/to/modules -I raw \
'import "url" | url::parse(.)' <<< "https://example.com/path"If you copied it to the mq built-in module directory:
mq -I raw 'import "url" | url::parse(.)' <<< "https://example.com/path"| Function | Description |
|---|---|
parse(u) |
Parses u into {scheme, userinfo, host, port, path, query, fragment} (each None if absent; path defaults to ""; port is a number) |
build(parts) |
Builds a URL string from a components dict (inverse of parse) |
parse_query(qs) |
Parses a raw query string into a dict of decoded key/value pairs |
build_query(d) |
Builds a raw, URL-encoded query string from a dict |
resolve(base, rel) |
Resolves rel against base |
remove_dot_segments(path) |
Collapses ./.. segments in a path |
is_absolute(u) |
true if u has a scheme |
is_relative(u) |
true if u has no scheme |
mq -I raw 'import "url" | url::resolve("https://example.com/docs/guide.md", "../images/foo.png")' <<< "x"
# => "https://example.com/images/foo.png"
mq -I raw 'import "url" | url::parse_query("a=1&b=hello%20world")' <<< "x"
# => {"a": "1", "b": "hello world"}Requires mq v0.6 or later (uses named regex capture groups via capture).
MIT