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

{
  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} or { };
      jsOffered = jsSet ? ghc;
      siteSet = config.legacyPackages;

      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);

      manualSteps = map (m: {
        step = lib.elemAt m 0;
        stepSentence = lib.elemAt m 1;
        result = lib.elemAt m 2;
        resultSentence = lib.elemAt m 3;
      }) (captures ''stepThen "(\[[^"]*)" "([^"]*)" "(\[[^"]*)" "([^"]*)"'' ../manual/dreamstop.vx);
      manualRecoveries = map (m: {
        annotation = lib.elemAt m 0;
        sentence = lib.elemAt m 1;
      }) (captures ''recover "(\[[^"]*)" "([^"]*)"'' ../manual/dreamstop.vx);

      # A reading (specification 2.3): clauses in display order, Force · Mood
      # · Aspect · Phase · NEG · Ground · Strength · NEG · Bound · Evidence,
      # each subordinate clause followed by its position and the number of
      # its parent clause, as in [ASR·HAB]/rel1.
      codes = "(ASR|DIR|IRG|DEC|ADM|CMS)(·(HYP|COU))?·(PFV|PRF|PRG|HAB)(·(INC|CSS|CNT|EXP))?(·NEG)?(·(DEO|ABL|TEL)·(NEC|WNC|POS)(·NEG)?)?(·ONE)?(·(OBS|PUP|RPR|INF|REC))?";
      clause = "\\[${codes}](/(rel|adv|cmp)[0-9]+)?";
      slotsOf =
        text:
        let
          g = builtins.match codes text;
          at = lib.elemAt g;
        in
        {
          force = at 0;
          mood = at 2;
          aspect = at 3;
          phase = at 5;
          modal =
            if at 8 == null then
              null
            else
              lib.concatStrings [
                (toString (at 6))
                (at 8)
                (at 9)
                (toString (at 10))
              ];
          outerNeg = at 6 != null;
          innerNeg = at 10 != null;
          ground = at 8;
          strength = at 9;
          bound = at 11;
          evidence = at 13;
        };
      clausesOf =
        a:
        map (m: {
          slots = slotsOf (lib.elemAt m 0);
          position =
            if lib.elemAt m 2 == null then
              "main"
            else
              {
                rel = "relative";
                adv = "adverbial";
                cmp = "complement";
              }
              .${lib.elemAt m 2};
          parent = if lib.elemAt m 3 == null then null else lib.toInt (lib.elemAt m 3) - 1;
        }) (lib.filter builtins.isList (builtins.split "\\[([^]]*)](/(rel|adv|cmp)([0-9]+))?" a));

      # The laws of specification 3.3 that one clause can break.
      clauseLaws = [
        {
          law = "ONE excludes HAB (law 9)";
          holds = x: !(x.bound != null && x.aspect == "HAB");
        }
        {
          law = "evidence on ASR·FAC only (law 10)";
          holds = x: x.evidence == null || (x.force == "ASR" && x.mood == null);
        }
        {
          law = "HYP only on ASR and IRG, COU only on ASR (law 11)";
          holds = x: x.mood == null || x.force == "ASR" || (x.mood == "HYP" && x.force == "IRG");
        }
        {
          law = "no phase on DEC or CMS (law 8)";
          holds =
            x:
            x.phase == null
            || !(lib.elem x.force [
              "DEC"
              "CMS"
            ]);
        }
        {
          law = "NEG·G·NEC·NEG and NEG·G·POS·NEG are written G·POS and G·NEC (rewrite 1)";
          holds = x: !(x.outerNeg && x.innerNeg && x.strength != "WNC");
        }
      ];

      # Document kinds (specification 5.2): a pattern maps each slot it
      # mentions to a list of codes, "required" or "optional". An unmentioned
      # slot is empty. A subordinate pattern names its parent main pattern.
      slotNames = [
        "force"
        "mood"
        "aspect"
        "phase"
        "modal"
        "bound"
        "evidence"
      ];
      matches =
        pattern: x:
        lib.all (
          n:
          let
            spec = pattern.${n} or null;
            v = x.${n};
          in
          if spec == null then
            v == null
          else if spec == "required" then
            v != null
          else if spec == "optional" then
            true
          else
            lib.elem v spec
        ) slotNames;
      allowedBy =
        kind: cs: x:
        if x.position == "main" then
          lib.any (p: matches p x.slots) (lib.attrValues kind.main)
        else
          x.parent != null
          && x.parent < lib.length cs
          && (lib.elemAt cs x.parent).position == "main"
          && lib.any (
            s:
            s.position == x.position
            && matches s.pattern x.slots
            && matches kind.main.${s.parent} (lib.elemAt cs x.parent).slots
          ) kind.subordinate;
      manualKind =
        let
          plain = {
            force = [ "ASR" ];
            aspect = "required";
            modal = "optional";
          };
        in
        {
          main = {
            step = {
              force = [ "DIR" ];
              aspect = [
                "PFV"
                "PRG"
                "HAB"
              ];
              modal = "optional";
            };
            statement = {
              force = [ "ASR" ];
              aspect = [
                "PFV"
                "HAB"
              ];
              modal = "optional";
              evidence = "optional";
            };
            warning = {
              force = [ "ADM" ];
              aspect = [
                "PFV"
                "PRG"
                "HAB"
              ];
              modal = "optional";
            };
          };
          subordinate = [
            {
              position = "adverbial";
              parent = "step";
              pattern = {
                force = [ "ASR" ];
                aspect = [
                  "PRF"
                  "HAB"
                ];
                modal = "optional";
              };
            }
            {
              position = "adverbial";
              parent = "step";
              pattern = {
                force = [ "IRG" ];
                aspect = [ "HAB" ];
                modal = "optional";
              };
            }
          ]
          ++
            lib.concatMap
              (parent: [
                {
                  position = "relative";
                  inherit parent;
                  pattern = plain;
                }
                {
                  position = "complement";
                  inherit parent;
                  pattern = plain;
                }
              ])
              [
                "step"
                "statement"
                "warning"
              ]
          ++
            map
              (parent: {
                position = "adverbial";
                inherit parent;
                pattern = plain;
              })
              [
                "statement"
                "warning"
              ];
        };
      websiteKind =
        let
          description = {
            force = [ "ASR" ];
            aspect = [ "HAB" ];
            modal = "optional";
            evidence = "optional";
          };
        in
        {
          main = {
            claim = description;
            plan = {
              force = [ "CMS" ];
              aspect = [ "PFV" ];
            };
            invitation = {
              force = [ "DIR" ];
              aspect = [ "PFV" ];
            };
          };
          subordinate =
            lib.concatMap
              (parent: [
                {
                  position = "relative";
                  inherit parent;
                  pattern = description;
                }
                {
                  position = "complement";
                  inherit parent;
                  pattern = description;
                }
                {
                  position = "adverbial";
                  inherit parent;
                  pattern = {
                    force = [ "ASR" ];
                    aspect = [
                      "PRF"
                      "HAB"
                    ];
                    modal = "optional";
                  };
                }
              ])
              [
                "claim"
                "plan"
                "invitation"
              ];
        };

      manualReadings =
        map (x: x.annotation) (manualSentences ++ manualRecoveries)
        ++ manualMixed
        ++ lib.concatMap (x: [
          x.step
          x.result
        ]) manualSteps;
      readings =
        map (a: {
          kind = manualKind;
          name = "manual";
          inherit a;
        }) manualReadings
        ++ map (x: {
          kind = websiteKind;
          name = "website";
          a = x.annotation;
        }) frontSentences;
      illFormed = lib.filter (r: builtins.match "${clause}( ${clause})*" r.a == null) readings;
      broken = lib.concatMap (
        r:
        let
          cs = clausesOf r.a;
        in
        lib.concatMap (x: map (l: "${r.a}: ${l.law}") (lib.filter (l: !(l.holds x.slots)) clauseLaws)) cs
        ++ map (_x: "${r.a}: a clause matches no ${r.name} pattern for its position") (
          lib.filter (x: !(allowedBy r.kind cs x)) cs
        )
      ) readings;

      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/&#39;/'"'"'/g; s/&quot;/"/g; s/&amp;/\&/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 + "\n") (
                  map (x: x.sentence) (manualSentences ++ manualRecoveries)
                  ++ lib.concatMap (x: [
                    x.stepSentence
                    x.resultSentence
                  ]) manualSteps
                )
              )
            }
            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 reading that keeps the clause laws
      # and the patterns of its kind: the manual kind for the manual, the
      # website kind for the front page. Every manual paragraph is annotated,
      # no prose has a semicolon, and every procedure has a section for each
      # Manual question.
      # 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 (r: ''
              echo "prose: not a reading in display order: ${lib.escapeShellArg r.a}" >&2
            '') illFormed}
            ${lib.optionalString (illFormed != [ ]) "exit 1"}
            ${lib.concatMapStrings (m: ''
              echo "prose: ${lib.escapeShellArg m}" >&2
            '') broken}
            ${lib.optionalString (broken != [ ]) "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
                + lib.length manualRecoveries
                + 2 * lib.length manualSteps
              )
            } manual and ${toString (lib.length frontSentences)} front-page sentences, ${
              toString (lib.foldl' (n: r: n + lib.length (clausesOf r.a)) 0 readings)
            } 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.mkMerge [
      {
        horizon.overlay =
          if jsOffered then
            {
              basePackages = jsSet;
              overlays = [
                (_final: prev: lib.mapAttrs (name: _: hl.dontHaddock prev.${name}) config.horizon.pkgs)
              ];
            }
          else
            lib.mkForce null;
      }
      (lib.mkIf (!jsOffered) {
        horizon.werrorwolf.enable = lib.mkForce false;
        horizon.devShell.enable = lib.mkForce false;
      })
      (lib.mkIf jsOffered {
        packages = {
          default = site.package;
          inherit manual;
        };

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

        checks = site.checks // {
          card-copy = cardCopy;
          manual-says = manualSays;
          inherit prose;
        };
      })
    ];
}