diff options
author | Juan Carlos <juancarlospaco@gmail.com> | 2020-04-13 10:15:45 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-13 14:15:45 +0100 |
commit | c2699648604cc57cd55ecd7d88ccd55ed38099fb (patch) | |
tree | 7e83b4090e18e3e9c79a455021b1e143bdf2210c | |
parent | 0a84219b3ec3b2deccc141bed4aee0252bc12ca7 (diff) | |
download | Nim-c2699648604cc57cd55ecd7d88ccd55ed38099fb.tar.gz |
Add Data URI Base64, implements RFC-2397 (#13759)
* Add Data URI Base64, implements RFC-2397 * Add Data URI Base64, implements RFC-2397 * Add Data URI Base64, implements RFC-2397 * https://github.com/nim-lang/Nim/pull/13759#issuecomment-611498420 * https://github.com/nim-lang/Nim/pull/13759#issuecomment-611498420 * ReSync changelog * https://github.com/nim-lang/Nim/pull/13759#issuecomment-611498420 Co-authored-by: Dominik Picheta <dominikpicheta@googlemail.com>
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | lib/pure/uri.nim | 36 |
2 files changed, 36 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md index 62db91622..1fc6d19c2 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ ## Standard library additions and changes +- `uri` adds Data URI Base64, implements RFC-2397. - Add [DOM Parser](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser) to the `dom` module for the JavaScript target. diff --git a/lib/pure/uri.nim b/lib/pure/uri.nim index 9112671c7..27fbf2db2 100644 --- a/lib/pure/uri.nim +++ b/lib/pure/uri.nim @@ -36,8 +36,17 @@ ## assert res.port == "4343" ## else: ## echo "Wrong format" +## +## Data URI Base64 +## --------------- +## +## .. code-block::nim +## doAssert getDataUri("Hello World", "text/plain") == "data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQ=" +## doAssert getDataUri("Nim", "text/plain") == "data:text/plain;charset=utf-8;base64,Tmlt" -import strutils, parseutils +include "system/inclrtl" + +import strutils, parseutils, base64 type Url* = distinct string @@ -466,6 +475,18 @@ proc `$`*(u: Uri): string = result.add("#") result.add(u.anchor) +proc getDataUri*(data, mime: string, encoding = "utf-8"): string {.since: (1, 3).} = + ## Convenience proc for `base64.encode` returns a standard Base64 Data URI (RFC-2397) + ## + ## **See also:** + ## * `mimetypes <mimetypes.html>`_ for `mime` argument + ## * https://tools.ietf.org/html/rfc2397 + ## * https://en.wikipedia.org/wiki/Data_URI_scheme + runnableExamples: static: doAssert getDataUri("Nim", "text/plain") == "data:text/plain;charset=utf-8;base64,Tmlt" + assert encoding.len > 0 and mime.len > 0 # Must *not* be URL-Safe, see RFC-2397 + result = "data:" & mime & ";charset=" & encoding & ";base64," & base64.encode(data) + + when isMainModule: block: const test1 = "abc\L+def xyz" @@ -735,4 +756,17 @@ when isMainModule: var foo1 = parseUri("http://example.com/foo?do=do&bar") doAssert foo == foo1 + block dataUriBase64: + doAssert getDataUri("", "text/plain") == "data:text/plain;charset=utf-8;base64," + doAssert getDataUri(" ", "text/plain") == "data:text/plain;charset=utf-8;base64,IA==" + doAssert getDataUri("c\xf7>", "text/plain") == "data:text/plain;charset=utf-8;base64,Y/c+" + doAssert getDataUri("Hello World", "text/plain") == "data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQ=" + doAssert getDataUri("leasure.", "text/plain") == "data:text/plain;charset=utf-8;base64,bGVhc3VyZS4=" + doAssert getDataUri("""!@#$%^&*()_+""", "text/plain") == "data:text/plain;charset=utf-8;base64,IUAjJCVeJiooKV8r" + doAssert(getDataUri("the quick brown dog jumps over the lazy fox", "text/plain") == + "data:text/plain;charset=utf-8;base64,dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA==") + doAssert(getDataUri("""The present is theirs + The future, for which I really worked, is mine.""", "text/plain") == + "data:text/plain;charset=utf-8;base64,VGhlIHByZXNlbnQgaXMgdGhlaXJzCiAgICAgIFRoZSBmdXR1cmUsIGZvciB3aGljaCBJIHJlYWxseSB3b3JrZWQsIGlzIG1pbmUu") + echo("All good!") |