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
bb0551ecc79400cb783638c02d4fd59574ac92c6nix/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 under the Manual rules (specification 5.4): clauses in
      # display order, Force · Aspect · NEG · Ground · Strength · NEG · Bound ·
      # Evidence, each followed by at most one role. Manuals have no reference
      # point, so no phase, and are FAC throughout, so no mood is written.
      codes = "(ASR|DIR|ADM|IRG)·(PFV|PRF|PRG|HAB)(·NEG)?(·(DEO|ABL|TEL)·(NEC|WNC|POS)(·NEG)?)?(·ONE)?(·(OBS|PUP|RPR|INF|REC))?";
      role = "step|precondition|obligation-holder [a-z-]+";
      clause = "\\[${codes}]( (${role}))?";
      clausesOf =
        a:
        map (m: {
          codes = lib.elemAt m 0;
          role = lib.elemAt m 2;
        }) (lib.filter builtins.isList (builtins.split "\\[([^]]*)]( (${role}))?" a));
      has = pattern: x: builtins.match ".*(${pattern}).*" x != null;
      # The laws of specification 3.3 that a single manual clause can break,
      # each with the message the build prints.
      clauseLaws = [
        {
          law = "a DIR clause is a step, and a step is DIR (M1, M2, law 13)";
          holds = x: (lib.hasPrefix "DIR" x.codes) == (x.role == "step");
        }
        {
          law = "IRG only on a precondition (M1)";
          holds = x: !(lib.hasPrefix "IRG" x.codes) || x.role == "precondition";
        }
        {
          law = "a precondition is ASR or IRG with PRF or HAB (M3, law 13)";
          holds = x: x.role != "precondition" || builtins.match "(ASR|IRG)·(PRF|HAB).*" x.codes != null;
        }
        {
          law = "every DEO clause carries an obligation-holder, and only DEO clauses do (C7, M5, law 13)";
          holds = x: (has "DEO" x.codes) == (x.role != null && lib.hasPrefix "obligation-holder" x.role);
        }
        {
          law = "an obligation-holder is on an ASR clause (law 13)";
          holds = x: !(has "DEO" x.codes) || lib.hasPrefix "ASR" x.codes;
        }
        {
          law = "ONE excludes HAB (law 9)";
          holds = x: !(has "ONE" x.codes && has "HAB" x.codes);
        }
        {
          law = "evidence only on ASR (law 10)";
          holds = x: !(has "OBS|PUP|RPR|INF|REC" x.codes) || lib.hasPrefix "ASR" x.codes;
        }
        {
          law = "NEG·G·NEC·NEG and NEG·G·POS·NEG are written G·POS and G·NEC (rewrite 1)";
          holds = x: !(has "NEG·(DEO|ABL|TEL)·(NEC|POS)·NEG" x.codes);
        }
      ];
      readings =
        map (x: x.annotation) (manualSentences ++ frontSentences ++ manualRecoveries)
        ++ manualMixed
        ++ lib.concatMap (x: [
          x.step
          x.result
        ]) manualSteps;
      illFormed = lib.filter (a: builtins.match "${clause}( ${clause})*" a == null) readings;
      broken = lib.concatMap (
        a:
        lib.concatMap (x: map (l: "${a}: ${l.law}") (lib.filter (l: !(l.holds x)) clauseLaws)) (clausesOf a)
      ) readings;
      clauseCount = a: lib.length (clausesOf a);
      # The Manual questions (specification 4.3): every step is followed by
      # an ASR result with no role, and every recovery starts from a
      # precondition that carries a negation and leads to a step.
      unanswering =
        map (x: "the step has no stated result: ${x.stepSentence}") (
          lib.filter (
            x:
            !(has "] step" x.step)
            || lib.any (y: !(lib.hasPrefix "ASR" y.codes) || y.role != null) (clausesOf x.result)
          ) manualSteps
        )
        ++ map (x: "the recovery does not start from a failed precondition: ${x.sentence}") (
          lib.filter (x: !(has "NEG] precondition.*] step" x.annotation)) manualRecoveries
        );

      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 + "\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 laws of
      # 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 reading in display order: ${lib.escapeShellArg a}" >&2
            '') illFormed}
            ${lib.optionalString (illFormed != [ ]) "exit 1"}
            ${lib.concatMapStrings (m: ''
              echo "prose: ${lib.escapeShellArg m}" >&2
            '') (broken ++ unanswering)}
            ${lib.optionalString (broken ++ unanswering != [ ]) "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: a: n + clauseCount 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;
        };
      })
    ];
}