about summary refs log tree commit diff stats
path: root/src/utils
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-08-13 17:42:34 +0200
committerbptato <nincsnevem662@gmail.com>2023-08-13 17:54:05 +0200
commitd526deb99e44f2a8d1a9c3eea60676703dd64302 (patch)
treef63689ff7654d14ad9bca182a837b3155b2471a0 /src/utils
parentf92e30232252deb194596e7c298cc7fcf56517cb (diff)
downloadchawan-d526deb99e44f2a8d1a9c3eea60676703dd64302.tar.gz
Add mailcap, mime.types & misc refactorings
* add mailcap: works with copiousoutput, needsterminal, etc.
* add mime.types (only works with mailcap)
* refactor pipeBuffer
* remove "dispatcher"
* fix bug in directory display where baseurl would not be used
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mimeguess.nim48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/utils/mimeguess.nim b/src/utils/mimeguess.nim
new file mode 100644
index 00000000..7477c399
--- /dev/null
+++ b/src/utils/mimeguess.nim
@@ -0,0 +1,48 @@
+import algorithm
+import streams
+import tables
+
+import config/mimetypes
+
+const DefaultGuess* = block:
+  let ss = newStringStream(staticRead"res/mime.types")
+  parseMimeTypes(ss)
+
+proc guessContentType*(path: string, fallback = "text/plain",
+    guess = DefaultGuess): string =
+  var i = path.len - 1
+  var n = 0
+  while i > 0:
+    if path[i] == '/':
+      return fallback
+    if path[i] == '.':
+      n = i
+      break
+    dec i
+  if n > 0:
+    let ext = path.substr(n + 1)
+    if ext in guess:
+      return guess[ext]
+  return fallback
+
+const JavaScriptTypes = [
+  "application/ecmascript",
+  "application/javascript",
+  "application/x-ecmascript",
+  "application/x-javascript",
+  "text/ecmascript",
+  "text/javascript",
+  "text/javascript1.0",
+  "text/javascript1.1",
+  "text/javascript1.2",
+  "text/javascript1.3",
+  "text/javascript1.4",
+  "text/javascript1.5",
+  "text/jscript",
+  "text/livescript",
+  "text/x-ecmascript",
+  "text/x-javascript"
+]
+
+proc isJavaScriptType*(s: string): bool =
+  return binarySearch(JavaScriptTypes, s) != -1