aboutsummaryrefslogtreecommitdiffstats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-11-15 17:06:53 +0100
committerbptato <nincsnevem662@gmail.com>2024-11-15 17:06:53 +0100
commitdef4ceccfb77afd74b1dd1273b66d036b766fd5f (patch)
tree8fb976f0ebe632029afe2ee9542d31b88e4f23d1 /src/types
parentfd10019d8b8a1b5dcb0026521fa8f5f072c3a10a (diff)
downloadchawan-def4ceccfb77afd74b1dd1273b66d036b766fd5f.tar.gz
js: reorganize modules, update docs
most of it has already been moved to monoucha, and the rest fits better in other directories. also, move urimethodmap to config
Diffstat (limited to 'src/types')
-rw-r--r--src/types/jscolor.nim56
-rw-r--r--src/types/urimethodmap.nim84
2 files changed, 56 insertions, 84 deletions
diff --git a/src/types/jscolor.nim b/src/types/jscolor.nim
new file mode 100644
index 00000000..20f04b0a
--- /dev/null
+++ b/src/types/jscolor.nim
@@ -0,0 +1,56 @@
+import std/strutils
+
+import monoucha/fromjs
+import monoucha/javascript
+import monoucha/quickjs
+import monoucha/tojs
+import types/color
+import types/opt
+import utils/twtstr
+
+func parseLegacyColor*(s: string): Result[RGBColor, cstring] =
+ if s == "":
+ return err(cstring"color value must not be the empty string")
+ let s = s.strip(chars = AsciiWhitespace).toLowerAscii()
+ if s == "transparent":
+ return err(cstring"color must not be transparent")
+ return ok(parseLegacyColor0(s))
+
+proc toJS*(ctx: JSContext; rgb: RGBColor): JSValue =
+ var res = "#"
+ res.pushHex(rgb.r)
+ res.pushHex(rgb.g)
+ res.pushHex(rgb.b)
+ return toJS(ctx, res)
+
+proc fromJS*(ctx: JSContext; val: JSValue; res: var RGBColor): Err[void] =
+ var s: string
+ ?ctx.fromJS(val, s)
+ let x = parseLegacyColor(s)
+ if x.isNone:
+ JS_ThrowTypeError(ctx, x.error)
+ return err()
+ res = x.get
+ return ok()
+
+proc toJS*(ctx: JSContext; rgba: ARGBColor): JSValue =
+ var res = "#"
+ res.pushHex(rgba.r)
+ res.pushHex(rgba.g)
+ res.pushHex(rgba.b)
+ res.pushHex(rgba.a)
+ return toJS(ctx, res)
+
+proc fromJS*(ctx: JSContext; val: JSValue; res: var ARGBColor): Err[void] =
+ if JS_IsNumber(val):
+ # as hex
+ ?ctx.fromJS(val, uint32(res))
+ return ok()
+ # parse
+ var s: string
+ ?ctx.fromJS(val, s)
+ if (let x = parseARGBColor(s); x.isSome):
+ res = x.get
+ return ok()
+ JS_ThrowTypeError(ctx, "unrecognized color")
+ return err()
diff --git a/src/types/urimethodmap.nim b/src/types/urimethodmap.nim
deleted file mode 100644
index 81876c26..00000000
--- a/src/types/urimethodmap.nim
+++ /dev/null
@@ -1,84 +0,0 @@
-# w3m's URI method map format.
-
-import std/strutils
-import std/tables
-
-import types/opt
-import types/url
-import utils/twtstr
-
-type URIMethodMap* = object
- map*: Table[string, string]
- imageProtos*: seq[string]
-
-func rewriteURL(pattern, surl: string): string =
- result = ""
- var was_perc = false
- for c in pattern:
- if was_perc:
- if c == '%':
- result &= '%'
- elif c == 's':
- result &= surl
- else:
- result &= '%'
- result &= c
- was_perc = false
- elif c != '%':
- result &= c
- else:
- was_perc = true
- if was_perc:
- result &= '%'
-
-type URIMethodMapResult* = enum
- ummrNotFound, ummrSuccess, ummrWrongURL
-
-proc findAndRewrite*(this: URIMethodMap; url: var URL): URIMethodMapResult =
- let protocol = url.protocol
- if protocol in this.map:
- let surl = this.map[protocol].rewriteURL($url)
- let x = newURL(surl)
- if x.isNone:
- return ummrWrongURL
- url = x.get
- return ummrSuccess
- return ummrNotFound
-
-proc insert(this: var URIMethodMap; k, v: string) =
- if not this.map.hasKeyOrPut(k, v) and k.startsWith("img-codec+"):
- this.imageProtos.add(k.until(':'))
-
-proc parseURIMethodMap*(this: var URIMethodMap; s: string) =
- for line in s.split('\n'):
- if line.len == 0 or line[0] == '#':
- continue # comments
- var k = ""
- var i = 0
- while i < line.len and line[i] notin AsciiWhitespace + {':'}:
- k &= line[i].toLowerAscii()
- inc i
- if i >= line.len or line[i] != ':':
- continue # invalid
- k &= ':'
- inc i # skip colon
- while i < line.len and line[i] in AsciiWhitespace:
- inc i
- var v = line.until(AsciiWhitespace, i)
- # Basic w3m compatibility.
- # If needed, w3m-cgi-compat covers more cases.
- if v.startsWith("file:/cgi-bin/"):
- v = "cgi-bin:" & v.substr("file:/cgi-bin/".len)
- elif v.startsWith("file:///cgi-bin/"):
- v = "cgi-bin:" & v.substr("file:///cgi-bin/".len)
- elif v.startsWith("/cgi-bin/"):
- v = "cgi-bin:" & v.substr("/cgi-bin/".len)
- this.insert(k, v)
-
-proc parseURIMethodMap*(s: string): URIMethodMap =
- result = URIMethodMap()
- result.parseURIMethodMap(s)
-
-proc append*(this: var URIMethodMap; that: URIMethodMap) =
- for k, v in that.map:
- this.insert(k, v)