about summary refs log tree commit diff stats
path: root/adapter/format
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 /adapter/format
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 'adapter/format')
-rw-r--r--adapter/format/ansi2html.nim32
1 files changed, 30 insertions, 2 deletions
diff --git a/adapter/format/ansi2html.nim b/adapter/format/ansi2html.nim
index 4266a3ba..9abc6925 100644
--- a/adapter/format/ansi2html.nim
+++ b/adapter/format/ansi2html.nim
@@ -356,11 +356,39 @@ proc processData(state: var State, buf: openArray[char]) =
     of '\0': state.puts("\uFFFD") # HTML eats NUL, so replace it here
     else: state.putc(c)
 
+proc usage() =
+  stderr.write("Usage: ansihtml [-s] [-t title]\n")
+  quit(1)
+
 proc main() =
   var state = State()
-  let standalone = paramCount() >= 1 and paramStr(1) == "-s"
+  # parse args
+  let H = paramCount()
+  var i = 1
+  var standalone = false
+  var title = ""
+  while i <= H:
+    let s = paramStr(i)
+    if s == "":
+      inc i
+    if s[0] != '-':
+      usage()
+    for j in 1 ..< s.len:
+      case s[j]
+      of 's':
+        standalone = true
+      of 't':
+        inc i
+        if i > H: usage()
+        title = paramStr(i).percentDecode()
+      else: discard
+    inc i
+  if standalone:
+    state.puts("<!DOCTYPE html>\n")
+  if title != "":
+    state.puts("<title>" & title.htmlEscape() & "</title>\n")
   if standalone:
-    state.puts("<!DOCTYPE html>\n<body>")
+    state.puts("<body>\n")
   state.puts("<pre style='margin: 0'>\n")
   let ofl = fcntl(STDIN_FILENO, F_GETFL, 0)
   doAssert ofl != -1