Skip to content

harehare/url.mq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

url.mq

URL parsing, building, and relative-resolution utilities implemented as an mq module.

Features

  • 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 references
  • is_absolute / is_relative predicates

Installation

Copy url.mq to your mq module directory, or place it anywhere and reference it with -L.

cp url.mq ~/.local/mq/config/

HTTP Import (no local installation needed)

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" | ...'

Usage

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"

API

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

Example

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"}

Compatibility

Requires mq v0.6 or later (uses named regex capture groups via capture).

License

MIT

About

URL parsing, building, and relative-resolution utilities implemented as an mq module.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors