about summary refs log tree commit diff stats
path: root/src/utils/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-29 16:32:04 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-29 16:57:16 +0100
commit75a5bdedbe3299b4a8d58239fd6d4ddaf52c8e5b (patch)
treeb2ac338e388eb4df165fd0332e3489f388c0a82d /src/utils/twtstr.nim
parent9053a9096bfe7845b712989ebfa3b9cba28cd3d5 (diff)
downloadchawan-75a5bdedbe3299b4a8d58239fd6d4ddaf52c8e5b.tar.gz
ansi2html: support passing titles
Use content type attributes so e.g. git.cgi can set the title even with
a text/x-ansi content type.

(This commit also fixes some bugs in content type attribute handling.)
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r--src/utils/twtstr.nim38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 6f40ae7b..f0a64546 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -678,22 +678,26 @@ func strictParseEnum*[T: enum](s: string): Opt[T] =
   return err()
 
 proc getContentTypeAttr*(contentType, attrname: string): string =
-  let kvs = contentType.after(';')
-  var i = kvs.find(attrname)
+  var i = contentType.find(';')
+  if i == -1:
+    return ""
+  i = contentType.find(attrname, i)
+  if i == -1:
+    return ""
+  i = contentType.skipBlanks(i + attrname.len)
+  if i >= contentType.len or contentType[i] != '=':
+    return ""
+  i = contentType.skipBlanks(i + 1)
+  var q = false
   var s = ""
-  if i != -1 and kvs.len > i + attrname.len and
-      kvs[i + attrname.len] == '=':
-    i += attrname.len + 1
-    while i < kvs.len and kvs[i] in AsciiWhitespace:
-      inc i
-    var q = false
-    for j, c in kvs.toOpenArray(i, kvs.high):
-      if q:
-        s &= c
-      elif c == '\\':
-        q = true
-      elif c == ';' or c in AsciiWhitespace:
-        break
-      else:
-        s &= c
+  for c in contentType.toOpenArray(i, contentType.high):
+    if q:
+      s &= c
+      q = false
+    elif c == '\\':
+      q = true
+    elif c in AsciiWhitespace + {';'}:
+      break
+    else:
+      s &= c
   return s