From 75a5bdedbe3299b4a8d58239fd6d4ddaf52c8e5b Mon Sep 17 00:00:00 2001 From: bptato Date: Fri, 29 Mar 2024 16:32:04 +0100 Subject: 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.) --- src/utils/twtstr.nim | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/utils/twtstr.nim') 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 -- cgit 1.4.1-2-gfad0