NixLabConnectionconnecting
ProjectsProject
web /

dreamstop.openfp.net

publicguest

dreamstop.openfp.net, the website and user manual of dreamstop, a language for continuous disambiguation

git clone git@nixlab.openfp.net:web/dreamstop.openfp.net.git
4f51e0ea1ec396dca25ffa9946827b7f4b420e09nix/site.nix
local:

{
  # The systems horizon-javascript offers a GHC for.
  systems = builtins.filter (s: local.inputs.horizon-javascript.legacyPackages.${s} ? ghc) (
    builtins.attrNames local.inputs.horizon-javascript.legacyPackages
  );

  perSystem =
    {
      config,
      pkgs,
      system,
      ...
    }:
    let
      inherit (local.inputs.nixpkgs) lib;
      inherit (local.inputs) design-kit;
      hl = pkgs.haskell.lib.compose;
      kit = import "${design-kit}/site.nix" { inherit pkgs; };

      jsSet = local.inputs.horizon-javascript.legacyPackages.${system};
      siteSet = config.legacyPackages;

      inkwellSet = local.inputs.inkwell-dsl.legacyPackages.${system};
      inkwellEnvironment = inkwellSet.shellFor {
        packages = _: [ ];
        extraDependencies = p: {
          libraryHaskellDepends = [ p.inkwell-dsl ];
        };
      };
      # The colour of each codepoint position, and the codes in it, as the
      # manual's reference table lists them. The front page's stylesheet and
      # the manual's Typst show rules are both printed from this table. Every
      # colour reaches WCAG AA (4.5:1) on the page's white and zinc-100 surfaces.
      codepoints = {
        layers = {
          Force = {
            colour = "#b91c1c";
            codes = [
              "ASR"
              "DIR"
              "IRG"
              "DEC"
              "ADM"
              "CMS"
            ];
          };
          Mood = {
            colour = "#854d0e";
            codes = [
              "FAC"
              "HYP"
              "COU"
            ];
          };
          Aspect = {
            colour = "#1d4ed8";
            codes = [
              "PFV"
              "PRF"
              "PRG"
              "HAB"
            ];
          };
          Phase = {
            colour = "#7e22ce";
            codes = [
              "INC"
              "CSS"
              "CNT"
              "EXP"
            ];
          };
          Modality = {
            colour = "#047857";
            codes = [
              "DEO"
              "ABL"
              "TEL"
              "NEC"
              "WNC"
              "POS"
            ];
          };
          Negation = {
            colour = "#be185d";
            codes = [ "NEG" ];
          };
          Bound = {
            colour = "#9a3412";
            codes = [ "ONE" ];
          };
          Evidence = {
            colour = "#0e7490";
            codes = [
              "OBS"
              "PUP"
              "RPR"
              "INF"
              "REC"
            ];
          };
          Nominalisation = {
            colour = "#3f6212";
            codes = [
              "BSC"
              "OBJ"
            ];
          };
        };
        # Brackets, separators and places.
        mark = "#52525b";
      };
      layerNames = builtins.attrNames codepoints.layers;
      # Each position's colour as a class, and each code's class in its position's
      # colour, for the front page.
      codepointsCss = pkgs.writeText "codepoints.css" (
        lib.concatMapStrings (
          l:
          lib.concatMapStrings (code: ".cp-${code}, ") codepoints.layers.${l}.codes
          + ".cp-${l} { color: ${codepoints.layers.${l}.colour}; }\n"
        ) layerNames
        + ".cp-mark { color: ${codepoints.mark}; }\n"
      );
      # Show rules that colour every code in the manual PDF.
      codepointsTypst = pkgs.writeText "codepoints.typ" (
        ''
          #let cp(col) = it => text(fill: rgb(col), it)
          #show regex("[\\[\\]·]|/(rel|adv|comp)[0-9]+"): cp("${codepoints.mark}")
        ''
        + lib.concatMapStrings (
          l:
          "#show regex(\"\\\\b(${
            lib.concatStringsSep "|" codepoints.layers.${l}.codes
          })\\\\b\"): cp(\"${codepoints.layers.${l}.colour}\")\n"
        ) layerNames
      );
      manualFonts = "${pkgs.dejavu_fonts}/share/fonts";

      manual =
        pkgs.runCommand "dreamstop.openfp.net-manual"
          {
            inherit (inkwellEnvironment) NIX_GHC NIX_GHCPKG NIX_GHC_LIBDIR;
            nativeBuildInputs = [ pkgs.typst ];
            LANG = "C.UTF-8";
            LC_ALL = "C.UTF-8";
          }
          ''
            mkdir -p $out/manual
            ${lib.getExe' inkwellSet.inkwell-dsl "inkwell-render"} ${../manual/dreamstop.vx} \
              "$("$NIX_GHC" --print-global-package-db)" > body.typ
            cat ${codepointsTypst} body.typ > dreamstop.typ
            typst compile --font-path ${manualFonts} dreamstop.typ $out/manual/dreamstop.pdf
          '';

      # The manual PDF holds the manual and names neither Ithkuil nor Quijada.
      manualSays =
        pkgs.runCommand "dreamstop.openfp.net-manual-says"
          {
            nativeBuildInputs = [ pkgs.poppler-utils ];
            LANG = "C.UTF-8";
            LC_ALL = "C.UTF-8";
          }
          ''
            pdftotext ${manual}/manual/dreamstop.pdf manual.txt
            grep -qF "Reference table" manual.txt || { echo "manual: the PDF holds no reference table" >&2; exit 1; }
            for t in Ithkuil Quijada; do
              if grep -qiF "$t" manual.txt; then echo "manual: it names $t" >&2; exit 1; fi
            done
            touch $out
          '';

      name = "dreamstop";
      title = "dreamstop — a language for disambiguating text";
      description = "dreamstop shows which sentences can be read more than one way, so authors can rewrite them.";
      card = "https://dreamstop.openfp.net/og.png";
      forge = "https://nixlab.openfp.net";
      meta =
        key: n: value:
        ''<meta ${key}="${n}" content="${value}" />'';

      site = kit.mkSite {
        hsPkgs = siteSet;
        name = "dreamstop-openfp-net-web";
        static = [
          ../dreamstop.svg
          manual
        ];
        css = [
          ../dreamstop.css
          codepointsCss
        ];
        card = {
          svg = ../og.svg;
          extraFiles = [ ../dreamstop.svg ];
        };
        flakeLock = ../flake.lock;
        horizonVx = ../horizon.vx;
        expect = [
          ''<div class="tabs" role="tablist" aria-label="Kinds of meaning">''
          ''<a class="btn" id="manual" href="manual/dreamstop.pdf">Read the manual</a>''
          { absent = "Ithkuil"; }
          { absent = "Quality Control Handbook"; }
          { absent = "From the ChangeLog"; }
          { absent = "Continuous Disambiguation"; }
          ''<a href="${forge}/web/dreamstop.openfp.net">Page source</a>''
          ''<link rel="icon" href="dreamstop.svg"''
          ''<script src="site.js" defer></script>''
          (meta "property" "og:type" "website")
          (meta "property" "og:site_name" name)
          (meta "property" "og:title" title)
          (meta "name" "twitter:title" title)
          (meta "name" "description" description)
          (meta "property" "og:description" description)
          (meta "name" "twitter:description" description)
          (meta "property" "og:url" "https://dreamstop.openfp.net/")
          (meta "property" "og:image" card)
          (meta "name" "twitter:image" card)
          (meta "property" "og:image:width" "1200")
          (meta "property" "og:image:height" "627")
          (meta "property" "og:image:type" "image/png")
          (meta "name" "twitter:card" "summary_large_image")
        ];
        nojsText = [
          "Read the manual"
          "A clear ChangeLog entry"
          "Added the required-package presence check"
          "Removed the legacy importer, which reads the old export format."
          "On first start the forge now imports every review from the SQLite database once."
        ];
      };

      # Every code on the page sits in a coloured code element: with those
      # elements, scripts and styles taken out, no code from the table is left.
      codesColoured = pkgs.runCommand "dreamstop.openfp.net-codes-coloured" { } ''
        codes="${lib.concatStringsSep "|" (lib.concatMap (p: p.codes) (lib.attrValues codepoints.layers))}"
        left=$(tr "\n" " " < ${site.package}/index.html \
          | sed -E "s#<(script|style)[^>]*>[^<]*</(script|style)>##g; s#<code>([^<]|<span[^>]*>[^<]*</span>)*</code>##g; s#<[^>]*># #g" \
          | { grep -owE "$codes" || true; } | sort -u | tr "\n" " ")
        [ -z "$left" ] || { echo "codes-coloured: uncoloured codes on the page: $left" >&2; exit 1; }
        touch $out
      '';

      cardCopy = pkgs.runCommand "dreamstop.openfp.net-card-copy" { } ''
        tagline=$(tr '\n' ' ' < ${../og.svg} | grep -oE '<text id="tagline".*' | sed -E 's#</text>.*##; s#<[^>]*># #g' | tr -s ' ' | sed -E 's/^ //; s/ $//')
        [ "$tagline" = "a language for disambiguating text" ] || { echo "card: the card must say the tagline, not '$tagline'" >&2; exit 1; }
        grep -qF '<image href="dreamstop.svg"' ${../og.svg} || { echo "card: the card must show the page's dreamstop.svg" >&2; exit 1; }
        touch $out
      '';
    in
    {
      horizon.overlay = {
        basePackages = jsSet;
        overlays = [
          (_final: prev: lib.mapAttrs (name: _: hl.dontHaddock prev.${name}) config.horizon.pkgs)
        ];
      };

      packages = {
        default = site.package;
        inherit manual;
      };

      apps.serve = {
        type = "app";
        program = toString site.serve;
      };

      checks = site.checks // {
        card-copy = cardCopy;
        codes-coloured = codesColoured;
        manual-says = manualSays;
      };
    };
}