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
ec1e4025b5ba84b97bf40e4cd47100dc56fbd29enix/site.nix
local:

{
  perSystem =
    { 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} or { };
      siteSet = jsSet.extend (
        import ../overlay.nix {
          inherit pkgs;
          f =
            _final: _prev: _n:
            hl.dontHaddock;
        }
      );

      inkwellSet = local.inputs.inkwell-dsl.legacyPackages.${system};
      inkwellEnvironment = inkwellSet.shellFor {
        packages = _: [ ];
        extraDependencies = p: {
          libraryHaskellDepends = [ p.inkwell-dsl ];
        };
      };
      manualFonts = "${pkgs.dejavu_fonts}/share/fonts";

      # Every sentence of the front page and the manual, with its dreamstop
      # annotation. The manual keeps each sentence beside its annotation in
      # `s "ANNOTATION" "SENTENCE"` and `sx "ANNOTATION" [...]`. The front
      # page's are in prose/front.vx as `sentence "ANNOTATION" "SENTENCE"`.
      captures =
        pattern: file: lib.filter builtins.isList (builtins.split pattern (builtins.readFile file));
      manualSentences = map (m: {
        annotation = lib.elemAt m 0;
        sentence = lib.elemAt m 1;
      }) (captures ''s "(\[[^"]*])" "([^"]*)"'' ../manual/dreamstop.vx);
      manualMixed = map (m: lib.elemAt m 0) (captures ''sx "(\[[^"]*])" \['' ../manual/dreamstop.vx);
      questionSections = map (m: lib.elemAt m 0) (
        captures ''answers "[^"]*" "([^"]*)"'' ../prose/questions.vx
      );
      manualSections = map (m: lib.elemAt m 0) (captures ''Identifier "([^"]*)"'' ../manual/dreamstop.vx);
      unanswered = lib.filter (q: !(lib.elem q manualSections)) questionSections;
      frontSentences = map (m: {
        annotation = lib.elemAt m 0;
        sentence = lib.elemAt m 1;
      }) (captures ''sentence "(\[[^"]*])" "([^"]*)"'' ../prose/front.vx);

      # One clause vector in slot order: force, aspect, phase, outer negation,
      # modality, inner negation, bound, evidence. The Manual rules allow only Assert and
      # Direct, and the front page is held to the same rules.
      vector = "\\[(Assert|Direct)·(Did|Done|Doing|Does)(·(New|Gone|Kept|Due))?(·Neg)?(·(Must|Should|Can|Nec|Poss)(·Neg)?)?(·(Once|Each))?(·(Measured|Claimed))?]";
      wellFormed = a: builtins.match "${vector}( ${vector})*" a != null;
      clauseCount = a: lib.length (lib.filter builtins.isList (builtins.split "\\[" a));
      illFormed = lib.filter (a: !wellFormed a) (
        map (x: x.annotation) (manualSentences ++ frontSentences) ++ manualMixed
      );

      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)" > dreamstop.typ
            typst compile --font-path ${manualFonts} dreamstop.typ $out/manual/dreamstop.pdf
            typst compile --features html --format html --font-path ${manualFonts} \
              dreamstop.typ $out/manual/index.html
          '';

      # The manual's HTML says every annotated sentence, with typographic
      # quotes and apostrophes folded to ASCII and whitespace collapsed.
      manualSays =
        pkgs.runCommand "dreamstop.openfp.net-manual-says"
          {
            LANG = "C.UTF-8";
            LC_ALL = "C.UTF-8";
          }
          ''
            sed -e 's/'/'"'"'/g; s/"/"/g; s/&/\&/g; s/’/'"'"'/g; s/‘/'"'"'/g' \
              ${manual}/manual/index.html | tr -s ' \n' '  ' > manual.txt
            while IFS= read -r t; do
              grep -qF -- "$t" manual.txt || { echo "manual: the HTML does not say: $t" >&2; exit 1; }
            done < ${
              pkgs.writeText "manual-sentences" (lib.concatMapStrings (x: x.sentence + "\n") manualSentences)
            }
            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
          '';

      # Every sentence carries a well-formed annotation under the Manual
      # rules, every manual paragraph is annotated and no prose has a
      # semicolon, and every procedure resolves the four Manual questions.
      # Whether an annotation is the right reading is for review.
      prose =
        pkgs.runCommand "dreamstop.openfp.net-prose"
          {
            LANG = "C.UTF-8";
            LC_ALL = "C.UTF-8";
          }
          ''
            ${lib.concatMapStrings (a: ''
              echo "prose: not a Manual annotation in slot order: ${lib.escapeShellArg a}" >&2
            '') illFormed}
            ${lib.optionalString (illFormed != [ ]) "exit 1"}
            ${lib.concatMapStrings (q: ''
              echo "prose: prose/questions.vx names a section the manual lacks: ${lib.escapeShellArg q}" >&2
            '') unanswered}
            ${lib.optionalString (unanswered != [ ] || lib.length questionSections != 12) "exit 1"}
            if grep -nE 'paragraph \[|paragraph \(t ' ${../manual/dreamstop.vx} | grep -v 'quotation'; then
              echo "prose: a manual paragraph above is not annotated" >&2; exit 1
            fi
            if grep -n ';' ${../manual/dreamstop.vx} ${../prose/front.vx}; then
              echo "prose: no semicolons" >&2; exit 1
            fi
            echo "prose: ${
              toString (lib.length manualSentences + lib.length manualMixed)
            } manual and ${toString (lib.length frontSentences)} front-page sentences, ${
              toString (lib.foldl' (n: x: n + clauseCount x.annotation) 0 (manualSentences ++ frontSentences))
            } clauses" > $out
          '';

      name = "dreamstop";
      title = "dreamstop — a language for continuous disambiguation";
      description = "dreamstop is a language for continuous disambiguation. Readers annotate each sentence, and authors rewrite any sentence that is not clear.";
      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 ];
        card = {
          svg = ../og.svg;
          extraFiles = [ ../dreamstop.svg ];
        };
        flakeLock = ../flake.lock;
        horizonVx = ../horizon.vx;
        expect = [
          ''<a class="btn" id="manual" href="manual/">Read the manual</a>''
          { absent = "Ithkuil"; }
          { 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 = map (x: x.sentence) frontSentences ++ [ "Read the manual" ];
      };

      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 continuous disambiguation" ] || { 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
    lib.mkIf (jsSet ? ghc) {
      packages = {
        default = site.package;
        inherit (siteSet) dreamstop-openfp-net-web;
        inherit manual;
      };

      legacyPackages = siteSet;

      devShells.default = siteSet.shellFor {
        packages = p: [ p.dreamstop-openfp-net-web ];
        nativeBuildInputs = [
          pkgs.cabal-install
          pkgs.nodejs-slim
        ];
      };

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

      checks = site.checks // {
        card-copy = cardCopy;
        manual-says = manualSays;
        inherit prose;
        dreamstop-openfp-net-web-werror = kit.werror siteSet.dreamstop-openfp-net-web;
        dreamstop-openfp-net-web-fresh = siteSet.dreamstop-openfp-net-web.fresh;
      };
    };
}