From aeb3813d405eb77e804b350e9f51c88dd4e464c2 Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Tue, 13 Apr 2021 23:24:45 +0200 Subject: pkgs/sternenseemann: add schmecgit, a cgit about / source filter Only does very simple dispatching to pkgs.lowdown and pkgs.chroma, but is at least significantly faster than the default source and about filters bundled with cgit (which is not really a challenge as they use python and pygments). Added to enable my cgit setup until we can have TVL's //tools/cheddar. --- pkgs/sternenseemann/default.nix | 4 ++ pkgs/sternenseemann/schmecgit/default.nix | 37 +++++++++++ pkgs/sternenseemann/schmecgit/main.c | 105 ++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 pkgs/sternenseemann/schmecgit/default.nix create mode 100644 pkgs/sternenseemann/schmecgit/main.c (limited to 'pkgs/sternenseemann') diff --git a/pkgs/sternenseemann/default.nix b/pkgs/sternenseemann/default.nix index da3dcb7e..b133df65 100644 --- a/pkgs/sternenseemann/default.nix +++ b/pkgs/sternenseemann/default.nix @@ -57,6 +57,10 @@ lib.fix (self: { temp ; + schmecgit = callPackage ./schmecgit { + inherit (pkgs.llvmPackages_latest) stdenv; + }; + scripts = dontRecurseIntoAttrs (callPackage ./scripts { inherit (writers) writeBashBin; inherit (self) shakti; diff --git a/pkgs/sternenseemann/schmecgit/default.nix b/pkgs/sternenseemann/schmecgit/default.nix new file mode 100644 index 00000000..982b2c1d --- /dev/null +++ b/pkgs/sternenseemann/schmecgit/default.nix @@ -0,0 +1,37 @@ +{ runCommandWith +, stdenv +, lib +, lowdown +, chroma +, substituteAll +}: + +let + src = substituteAll { + src = ./main.c; + chroma = "${lib.getBin chroma}/bin/chroma"; + lowdown = "${lib.getBin lowdown}/bin/lowdown"; + }; +in + +runCommandWith { + name = "schmecgit"; + inherit stdenv; + derivationArgs.meta = { + description = "schmeck it, about and source filter for cgit"; + license = lib.licenses.gpl2Plus; + platforms = lib.platforms.unix; + }; +} '' + mkdir -p "$out/bin" + + # compile main executable + clang -o "$out/bin/schmecgit" -pedantic -Wall -Wextra -Werror -std=c99 ${src} + + # wrapper script for cgit about filter + cat > "$out/bin/schmecgit-about" < +#include +#include +#include +#include + +enum file_type { + PLAIN, + MARKDOWN, + HTML, + CODE +}; + +void output_xml_escaped_char(char c, FILE *out) { + switch(c) { + case '&': + fputs("&", out); + break; + case '<': + fputs("<", out); + break; + case '>': + fputs(">", out); + break; + case '\'': + fputs("'", out); + break; + case '\"': + fputs(""", out); + break; + default: + fputc(c, out); + break; + } +} + +int main(int argc, char *argv[]) { + bool about_filter; + char *file; + + if(argc == 2) { + about_filter = 0; + file = argv[1]; + } else if(argc == 3 && strcmp(argv[1], "--about") == 0) { + about_filter = 1; + file = argv[2]; + } else { + fprintf(stderr, "Usage: %s [--about] BASENAME\n", argv[0]); + return 1; + } + + char *extension = rindex(file, '.'); + enum file_type ft = PLAIN; + + if(extension != NULL) { + extension++; + + if(about_filter && (strcmp(extension, "md") == 0 || strcmp(extension, "markdown") == 0)) { + ft = MARKDOWN; + } else if(about_filter && (strcmp(extension, "html") == 0 || strcmp(extension, "htm") == 0)) { + ft = HTML; + } else if(strcmp(extension, "txt") == 0) { + ft = PLAIN; + } else { + ft = CODE; + } + } else { + if(strcmp(file, "Makefile") == 0 || strcmp(file, "Doxyfile")) { + ft = CODE; + } + } + + if(ft == PLAIN || ft == HTML) { + char c; + if(ft == PLAIN) { + fputs("
", stdout);
+    }
+
+    while((c = fgetc(stdin)) != EOF) {
+      if(ft == HTML) {
+        fputc(c, stdout);
+      } else {
+        output_xml_escaped_char(c, stdout);
+      }
+    }
+
+    if(ft == PLAIN) {
+      fputs("
", stdout); + } + } else if(ft == MARKDOWN) { + return execl("@lowdown@", "lowdown", NULL); + } else if(ft == CODE) { + return execl( + "@chroma@", "chroma", "--filename", file, + "--html", "--html-tab-width=2", "--html-only", "--html-inline-styles", + "--style=lovelace", + NULL + ); + } else { + return 1; + } + + return 0; +} -- cgit 1.4.1