about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-13 21:16:12 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-25 02:46:21 +0100
commit6e98894199442e2213dc89e0c5fe970029f05b65 (patch)
tree57bf69a6fa825d72be1654482e8865b5e9b82829 /src/config
parentd41d4803b5ed15b7e8461394ee07ce5ab1de143a (diff)
downloadchawan-6e98894199442e2213dc89e0c5fe970029f05b65.tar.gz
Separate ANSI text decoding from main binary
Handling text/plain as ANSI colored text was problematic for two
reasons:

* You couldn't actually look at the real source of HTML pages or text
  files that used ANSI colors in the source.  In general, I only want
  ANSI colors when piping something into my pager, not when viewing any
  random file.
* More importantly, it introduced a separate rendering mode for
  plaintext documents, which resulted in the problem that only some
  buffers had DOMs.  This made it impossible to add functionality
  that would operate on the buffer's DOM, to e.g. implement w3m's
  MARK_URL.  Also, it locked us into the horribly inefficient line-based
  rendering model of entire documents.

Now we solve the problem in two separate parts:

* text/x-ansi is used automatically for documents received through
  stdin. A text/x-ansi handler ansi2html converts ANSI formatting to
  HTML.  text/x-ansi is also used for .ans, .asc file extensions.
* text/plain is a separate input mode in buffer, which places all text
  in a single <plaintext> tag.  Crucially, this does not invoke the HTML
  parser; that would eat NUL characters, which we should avoid.

One blind spot still remains: copiousoutput used to display ANSI colors,
and now it doesn't. To solve this, users can put the x-ansioutput
extension field to their mailcap entries, which behaves like
x-htmloutput except it first pipes the output into ansi2html.
Diffstat (limited to 'src/config')
-rw-r--r--src/config/config.nim18
-rw-r--r--src/config/mailcap.nim3
2 files changed, 15 insertions, 6 deletions
diff --git a/src/config/config.nim b/src/config/config.nim
index 82200334..1ae0a727 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -357,12 +357,12 @@ proc readUserStylesheet(dir, file: string): string =
 # of several individual configuration files known as mailcap files.
 proc getMailcap*(config: Config): tuple[mailcap: Mailcap, errs: seq[string]] =
   let configDir = getConfigDir() / "chawan" #TODO store this in config?
-  const gopherPath0 = ChaPath("${%CHA_LIBEXEC_DIR}/gopher2html -u \\$MAILCAP_URL")
-  let gopherPath = gopherPath0.unquote().get
-  const geminiPath0 = ChaPath("${%CHA_LIBEXEC_DIR}/gmi2html")
-  let geminiPath = geminiPath0.unquote().get
-  const mdPath0 = ChaPath("${%CHA_LIBEXEC_DIR}/md2html")
-  let mdPath = mdPath0.unquote().get
+  template uq(s: string): string =
+    ChaPath(s).unquote.get
+  let gopherPath = "${%CHA_LIBEXEC_DIR}/gopher2html -u \\$MAILCAP_URL".uq
+  let geminiPath = "${%CHA_LIBEXEC_DIR}/gmi2html".uq
+  let mdPath = "${%CHA_LIBEXEC_DIR}/md2html".uq
+  let ansiPath = "${%CHA_LIBEXEC_DIR}/ansi2html".uq
   var mailcap: Mailcap = @[]
   var errs: seq[string]
   var found = false
@@ -393,6 +393,12 @@ proc getMailcap*(config: Config): tuple[mailcap: Mailcap, errs: seq[string]] =
     cmd: mdPath,
     flags: {HTMLOUTPUT}
   ))
+  mailcap.add(MailcapEntry(
+    mt: "text",
+    subt: "x-ansi",
+    cmd: ansiPath,
+    flags: {HTMLOUTPUT}
+  ))
   if not found:
     mailcap.add(MailcapEntry(
       mt: "*",
diff --git a/src/config/mailcap.nim b/src/config/mailcap.nim
index d5d17eae..89d268db 100644
--- a/src/config/mailcap.nim
+++ b/src/config/mailcap.nim
@@ -20,6 +20,7 @@ type
     NEEDSTERMINAL = "needsterminal"
     COPIOUSOUTPUT = "copiousoutput"
     HTMLOUTPUT = "x-htmloutput" # from w3m
+    ANSIOUTPUT = "x-ansioutput" # Chawan extension
 
   MailcapEntry* = object
     mt*: string
@@ -122,6 +123,8 @@ proc parseFieldKey(entry: var MailcapEntry, k: string): NamedField =
     entry.flags.incl(COPIOUSOUTPUT)
   of "x-htmloutput":
     entry.flags.incl(HTMLOUTPUT)
+  of "x-ansioutput":
+    entry.flags.incl(ANSIOUTPUT)
   of "test":
     return NAMED_FIELD_TEST
   of "nametemplate":