(define
  slug->component
  (fn
    (slug prefix infix suffix)
    (if
      infix
      (make-symbol (str prefix slug infix slug suffix))
      (make-symbol (str prefix slug suffix)))))

(define
  make-page-fn
  (fn
    (default-name prefix infix suffix)
    (fn
      (slug)
      (if
        (nil? slug)
        (list (make-symbol default-name))
        (list (slug->component slug prefix infix suffix))))))

(define
  home
  (fn
    (content)
    (if (nil? content) (quote (~docs-content/home-content)) content)))

(define language (fn (content) (if (nil? content) nil content)))

(define
  geography
  (fn
    (content)
    (if (nil? content) (quote (~geography/index-content)) content)))

(define applications (fn (content) (if (nil? content) nil content)))

(define etc (fn (content) (if (nil? content) nil content)))

(define hypermedia (fn (content) (if (nil? content) nil content)))

(define
  reactive
  (fn
    (content)
    (if
      (nil? content)
      (quote (~reactive-islands/index/reactive-islands-index-content))
      content)))

(define
  examples
  (make-page-fn
    "~reactive-islands/demo/reactive-islands-demo-content"
    "~reactive-islands/demo/example-"
    nil
    ""))

(define
  cek
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~geography/cek/cek-content))
      (case
        slug
        "demo"
        (quote (~geography/cek/cek-demo-content))
        "freeze"
        (quote (~geography/cek/cek-freeze-content))
        "content"
        (quote (~geography/cek/cek-content-address-content))
        :else (quote (~geography/cek/cek-content))))))

(define
  provide
  (fn
    (content)
    (if (nil? content) (quote (~geography/provide-content)) content)))

(define
  scopes
  (fn
    (content)
    (if (nil? content) (quote (~geography/scopes-content)) content)))

(define
  spreads
  (fn
    (content)
    (if (nil? content) (quote (~geography/spreads-content)) content)))

(define
  marshes
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~reactive-islands/marshes/reactive-islands-marshes-content))
      (case
        slug
        "hypermedia-feeds"
        (quote (~reactive-islands/marshes/example-hypermedia-feeds))
        "server-signals"
        (quote (~reactive-islands/marshes/example-server-signals))
        "on-settle"
        (quote (~reactive-islands/marshes/example-on-settle))
        "signal-triggers"
        (quote (~reactive-islands/marshes/example-signal-triggers))
        "view-transform"
        (quote (~reactive-islands/marshes/example-view-transform))
        :else (quote (~reactive-islands/marshes/reactive-islands-marshes-content))))))

(define
  isomorphism
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~plans/isomorphic/plan-isomorphic-content))
      (case
        slug
        "bundle-analyzer"
        (let-match
          {:total-macros total-macros :pages pages :io-count io-count :pure-count pure-count :total-components total-components}
          (helper "bundle-analyzer-data")
          (quasiquote
            (~analyzer/bundle-analyzer-content
              :pages (unquote pages)
              :total-components (unquote total-components)
              :total-macros (unquote total-macros)
              :pure-count (unquote pure-count)
              :io-count (unquote io-count))))
        "routing-analyzer"
        (let-match
          {:pages pages :total-pages total-pages :server-count server-count :registry-sample registry-sample :client-count client-count}
          (helper "routing-analyzer-data")
          (quasiquote
            (~routing-analyzer/content
              :pages (unquote pages)
              :total-pages (unquote total-pages)
              :client-count (unquote client-count)
              :server-count (unquote server-count)
              :registry-sample (unquote registry-sample))))
        "data-test"
        (let-match
          {:server-time server-time :transport transport :phase phase :items items}
          (helper "data-test-data")
          (quasiquote
            (~data-test/content
              :server-time (unquote server-time)
              :items (unquote items)
              :phase (unquote phase)
              :transport (unquote transport))))
        "async-io"
        (quote (~async-io-demo/content))
        "affinity"
        (let-match
          {:components components :page-plans page-plans}
          (helper "affinity-demo-data")
          (quasiquote
            (~affinity-demo/content
              :components (unquote components)
              :page-plans (unquote page-plans))))
        "optimistic"
        (let-match
          {:server-time server-time :items items}
          (helper "optimistic-demo-data")
          (quasiquote
            (~optimistic-demo/content
              :items (unquote items)
              :server-time (unquote server-time))))
        "offline"
        (let-match
          {:server-time server-time :notes notes}
          (helper "offline-demo-data")
          (quasiquote
            (~offline-demo/content
              :notes (unquote notes)
              :server-time (unquote server-time))))
        :else (quote (~plans/isomorphic/plan-isomorphic-content))))))

(define
  doc
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~docs-content/docs-introduction-content))
      (case
        slug
        "introduction"
        (quote (~docs-content/docs-introduction-content))
        "getting-started"
        (quote (~docs-content/docs-getting-started-content))
        "components"
        (quote (~docs-content/docs-components-content))
        "evaluator"
        (quote (~docs-content/docs-evaluator-content))
        "primitives"
        (let
          ((data (helper "primitives-data")))
          (quasiquote
            (~docs-content/docs-primitives-content
              :prims (~docs/primitives-tables :primitives (unquote data)))))
        "special-forms"
        (let
          ((data (helper "special-forms-data")))
          (quasiquote
            (~docs-content/docs-special-forms-content
              :forms (~docs/special-forms-tables :forms (unquote data)))))
        "server-rendering"
        (quote (~docs-content/docs-server-rendering-content))
        :else (quote (~docs-content/docs-introduction-content))))))

(define
  spec
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~specs/architecture-content))
      (case
        slug
        "core"
        (let
          ((files (make-spec-files core-spec-items)))
          (quasiquote
            (~specs/overview-content
              :spec-title "Core Language"
              :spec-files (unquote files))))
        "adapters"
        (let
          ((files (make-spec-files adapter-spec-items)))
          (quasiquote
            (~specs/overview-content
              :spec-title "Adapters"
              :spec-files (unquote files))))
        "browser"
        (let
          ((files (make-spec-files browser-spec-items)))
          (quasiquote
            (~specs/overview-content
              :spec-title "Browser Runtime"
              :spec-files (unquote files))))
        "reactive"
        (let
          ((files (make-spec-files reactive-spec-items)))
          (quasiquote
            (~specs/overview-content
              :spec-title "Reactive System"
              :spec-files (unquote files))))
        "host"
        (let
          ((files (make-spec-files host-spec-items)))
          (quasiquote
            (~specs/overview-content
              :spec-title "Host Interface"
              :spec-files (unquote files))))
        "extensions"
        (let
          ((files (make-spec-files extension-spec-items)))
          (quasiquote
            (~specs/overview-content
              :spec-title "Extensions"
              :spec-files (unquote files))))
        :else (let
          ((found-spec (find-spec slug)))
          (if
            found-spec
            (let-match
              {:desc desc :prose prose :title title :filename filename}
              found-spec
              (let
                ((src (helper "read-spec-file" filename)))
                (quasiquote
                  (~specs/detail-content
                    :spec-title (unquote title)
                    :spec-desc (unquote desc)
                    :spec-filename (unquote filename)
                    :spec-source (unquote src)
                    :spec-prose (unquote prose)))))
            (quasiquote (~specs/not-found :slug (unquote slug)))))))))

(define
  explore
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~specs/architecture-content))
      (let
        ((found-spec (find-spec slug)))
        (if
          found-spec
          (let
            ((data (spec-explore (get found-spec "filename") (get found-spec "title") (get found-spec "desc"))))
            (if
              data
              (quasiquote
                (~specs-explorer/spec-explorer-content :data (unquote data)))
              (quasiquote (~specs/not-found :slug (unquote slug)))))
          (quasiquote (~specs/not-found :slug (unquote slug))))))))

(define
  make-spec-files
  (fn
    (items)
    (map
      (fn
        (item)
        (dict
          :title (get item "title")
          :desc (get item "desc")
          :prose (get item "prose")
          :filename (get item "filename")
          :href (str "/sx/(language.(spec." (get item "slug") "))")
          :source (helper "read-spec-file" (get item "filename"))))
      items)))

(define
  bootstrapper
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~specs/bootstrappers-index-content))
      (let
        ((data (helper "bootstrapper-data" slug)))
        (if
          (get data "bootstrapper-not-found")
          (quasiquote (~specs/not-found :slug (unquote slug)))
          (case
            slug
            "self-hosting"
            (let-match
              {:g1-output g1-output :py-sx-source py-sx-source :g0-bytes g0-bytes :verification-status verification-status :g0-output g0-output :defines-total defines-total :defines-matched defines-matched :g0-lines g0-lines}
              data
              (quasiquote
                (~specs/bootstrapper-self-hosting-content
                  :py-sx-source (unquote py-sx-source)
                  :g0-output (unquote g0-output)
                  :g1-output (unquote g1-output)
                  :defines-matched (unquote defines-matched)
                  :defines-total (unquote defines-total)
                  :g0-lines (unquote g0-lines)
                  :g0-bytes (unquote g0-bytes)
                  :verification-status (unquote verification-status))))
            "self-hosting-js"
            (let-match
              {:js-sx-source js-sx-source :verification-status verification-status :js-sx-lines js-sx-lines :defines-total defines-total :defines-matched defines-matched}
              data
              (quasiquote
                (~specs/bootstrapper-self-hosting-js-content
                  :js-sx-source (unquote js-sx-source)
                  :defines-matched (unquote defines-matched)
                  :defines-total (unquote defines-total)
                  :js-sx-lines (unquote js-sx-lines)
                  :verification-status (unquote verification-status))))
            "python"
            (let-match
              {:bootstrapper-source bootstrapper-source :bootstrapped-output bootstrapped-output}
              data
              (quasiquote
                (~specs/bootstrapper-py-content
                  :bootstrapper-source (unquote bootstrapper-source)
                  :bootstrapped-output (unquote bootstrapped-output))))
            "page-helpers"
            (let-match
              {:attr-result attr-result :sf-source sf-source :ref-ms ref-ms :req-attrs req-attrs :attr-detail attr-detail :attr-keys attr-keys :server-total-ms server-total-ms :attr-ms attr-ms :comp-ms comp-ms :routing-ms routing-ms :comp-source comp-source :routing-result routing-result :sf-categories sf-categories :sf-total sf-total :sf-ms sf-ms :ref-sample ref-sample}
              (helper "page-helpers-demo-data")
              (quasiquote
                (~page-helpers-demo/content
                  :sf-categories (unquote sf-categories)
                  :sf-total (unquote sf-total)
                  :sf-ms (unquote sf-ms)
                  :ref-sample (unquote ref-sample)
                  :ref-ms (unquote ref-ms)
                  :attr-result (unquote attr-result)
                  :attr-ms (unquote attr-ms)
                  :comp-source (unquote comp-source)
                  :comp-ms (unquote comp-ms)
                  :routing-result (unquote routing-result)
                  :routing-ms (unquote routing-ms)
                  :server-total-ms (unquote server-total-ms)
                  :sf-source (unquote sf-source)
                  :attr-detail (unquote attr-detail)
                  :req-attrs (unquote req-attrs)
                  :attr-keys (unquote attr-keys))))
            :else (let-match
              {:bootstrapper-source bootstrapper-source :bootstrapped-output bootstrapped-output}
              data
              (quasiquote
                (~specs/bootstrapper-js-content
                  :bootstrapper-source (unquote bootstrapper-source)
                  :bootstrapped-output (unquote bootstrapped-output))))))))))

(define
  test
  (fn
    (slug)
    (if
      (nil? slug)
      (let-match
        {:server-results server-results :parser-source parser-source :framework-source framework-source :eval-source eval-source :router-source router-source :engine-source engine-source :render-source render-source :deps-source deps-source}
        (helper "run-modular-tests" "all")
        (quasiquote
          (~testing/overview-content
            :server-results (unquote server-results)
            :framework-source (unquote framework-source)
            :eval-source (unquote eval-source)
            :parser-source (unquote parser-source)
            :router-source (unquote router-source)
            :render-source (unquote render-source)
            :deps-source (unquote deps-source)
            :engine-source (unquote engine-source))))
      (case
        slug
        "runners"
        (quote (~testing/runners-content))
        :else (let-match
          {:server-results server-results :spec-source spec-source :framework-source framework-source}
          (helper "run-modular-tests" slug)
          (case
            slug
            "eval"
            (quasiquote
              (~testing/spec-content
                :spec-name "eval"
                :spec-title "Evaluator Tests"
                :spec-desc "81 tests covering the core evaluator — literals, symbols, special forms, closures."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            "parser"
            (quasiquote
              (~testing/spec-content
                :spec-name "parser"
                :spec-title "Parser Tests"
                :spec-desc "39 tests covering tokenization and parsing."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            "router"
            (quasiquote
              (~testing/spec-content
                :spec-name "router"
                :spec-title "Router Tests"
                :spec-desc "18 tests covering client-side route matching."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            "render"
            (quasiquote
              (~testing/spec-content
                :spec-name "render"
                :spec-title "Renderer Tests"
                :spec-desc "23 tests covering HTML rendering."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            "deps"
            (quasiquote
              (~testing/spec-content
                :spec-name "deps"
                :spec-title "Dependency Analysis Tests"
                :spec-desc "33 tests covering component dependency analysis."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            "engine"
            (quasiquote
              (~testing/spec-content
                :spec-name "engine"
                :spec-title "Engine Tests"
                :spec-desc "37 tests covering engine pure functions."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            "orchestration"
            (quasiquote
              (~testing/spec-content
                :spec-name "orchestration"
                :spec-title "Orchestration Tests"
                :spec-desc "17 tests covering orchestration."
                :spec-source (unquote spec-source)
                :framework-source (unquote framework-source)
                :server-results (unquote server-results)))
            :else (quasiquote
              (~testing/overview-content
                :server-results (unquote server-results)))))))))

(define
  reference
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~examples/reference-index-content))
      (let
        ((data (helper "reference-data" slug)))
        (case
          slug
          "attributes"
          (let-match
            {:req-attrs req-attrs :beh-attrs beh-attrs :uniq-attrs uniq-attrs}
            data
            (quasiquote
              (~reference/attrs-content
                :req-table (~docs/attr-table-from-data
                  :title "Request Attributes"
                  :attrs (unquote req-attrs))
                :beh-table (~docs/attr-table-from-data
                  :title "Behavior Attributes"
                  :attrs (unquote beh-attrs))
                :uniq-table (~docs/attr-table-from-data
                  :title "Unique to sx"
                  :attrs (unquote uniq-attrs)))))
          "headers"
          (let-match
            {:req-headers req-headers :resp-headers resp-headers}
            data
            (quasiquote
              (~reference/headers-content
                :req-table (~docs/headers-table-from-data
                  :title "Request Headers"
                  :headers (unquote req-headers))
                :resp-table (~docs/headers-table-from-data
                  :title "Response Headers"
                  :headers (unquote resp-headers)))))
          "events"
          (quasiquote
            (~reference/events-content
              :table (~docs/two-col-table-from-data
                :intro "sx fires custom DOM events at various points in the request lifecycle."
                :col1 "Event"
                :col2 "Description"
                :items (unquote (get data "events-list")))))
          "js-api"
          (quasiquote
            (~reference/js-api-content
              :table (~docs/two-col-table-from-data
                :intro "The client-side sx.js library exposes a public API for programmatic use."
                :col1 "Method"
                :col2 "Description"
                :items (unquote (get data "js-api-list")))))
          :else (let-match
            {:req-attrs req-attrs :beh-attrs beh-attrs :uniq-attrs uniq-attrs}
            data
            (quasiquote
              (~reference/attrs-content
                :req-table (~docs/attr-table-from-data
                  :title "Request Attributes"
                  :attrs (unquote req-attrs))
                :beh-table (~docs/attr-table-from-data
                  :title "Behavior Attributes"
                  :attrs (unquote beh-attrs))
                :uniq-table (~docs/attr-table-from-data
                  :title "Unique to sx"
                  :attrs (unquote uniq-attrs))))))))))

(define
  reference-detail
  (fn
    (kind slug)
    (if
      (nil? slug)
      nil
      (case
        kind
        "attributes"
        (let
          ((data (helper "attr-detail-data" slug)))
          (if
            (get data "attr-not-found")
            (quasiquote (~reference/attr-not-found :slug (unquote slug)))
            (let-match
              {:attr-handler attr-handler :attr-title attr-title :attr-example attr-example :attr-description attr-description :attr-demo attr-demo :attr-wire-id attr-wire-id}
              data
              (quasiquote
                (~reference/attr-detail-content
                  :title (unquote attr-title)
                  :description (unquote attr-description)
                  :demo (unquote attr-demo)
                  :example-code (unquote attr-example)
                  :handler-code (unquote attr-handler)
                  :wire-placeholder-id (unquote attr-wire-id))))))
        "headers"
        (let
          ((data (helper "header-detail-data" slug)))
          (if
            (get data "header-not-found")
            (quasiquote (~reference/attr-not-found :slug (unquote slug)))
            (let-match
              {:header-description header-description :header-demo header-demo :header-title header-title :header-example header-example :header-direction header-direction}
              data
              (quasiquote
                (~reference/header-detail-content
                  :title (unquote header-title)
                  :direction (unquote header-direction)
                  :description (unquote header-description)
                  :example-code (unquote header-example)
                  :demo (unquote header-demo))))))
        "events"
        (let
          ((data (helper "event-detail-data" slug)))
          (if
            (get data "event-not-found")
            (quasiquote (~reference/attr-not-found :slug (unquote slug)))
            (let-match
              {:event-example event-example :event-demo event-demo :event-description event-description :event-title event-title}
              data
              (quasiquote
                (~reference/event-detail-content
                  :title (unquote event-title)
                  :description (unquote event-description)
                  :example-code (unquote event-example)
                  :demo (unquote event-demo))))))
        :else nil))))

(define
  example
  (fn
    (slug)
    (if
      (nil? slug)
      nil
      (list (slug->component slug "~examples-content/example-" nil "")))))

(define sx-urls (fn (slug) (quote (~sx-urls/urls-content))))

(define cssx (make-page-fn "~cssx/overview-content" "~cssx/" nil "-content"))

(define
  protocol
  (make-page-fn "~protocols/wire-format-content" "~protocols/" nil "-content"))

(define
  sx-pub
  (fn (slug) (if (nil? slug) (quote (~sx-pub/overview-content)) nil)))

(define
  sx-tools
  (fn
    (&key title &rest args)
    (quasiquote
      (~sx-tools/overview-content
        :title (unquote (or title "SX Tools"))
        (splice-unquote args)))))

(define tools (fn (content) (if (nil? content) nil content)))

(define
  services
  (fn
    (&key title &rest args)
    (quasiquote
      (~services-tools/overview-content
        :title (unquote (or title "Services"))
        (splice-unquote args)))))

(define
  reactive-runtime
  (make-page-fn
    "~reactive-runtime/overview-content"
    "~reactive-runtime/"
    nil
    "-content"))

(define
  native-browser
  (make-page-fn
    "~applications/native-browser/content"
    "~applications/native-browser/"
    nil
    "-content"))

(define
  essay
  (make-page-fn "~essays/index/essays-index-content" "~essays/" "/essay-" ""))

(define
  philosophy
  (fn
    (slug)
    (if
      (nil? slug)
      (quote (~essays/philosophy-index/content))
      (case
        slug
        "sx-manifesto"
        (quote (~essay-sx-manifesto))
        "godel-escher-bach"
        (quote (~essays/godel-escher-bach/essay-godel-escher-bach))
        "wittgenstein"
        (quote (~essays/sx-and-wittgenstein/essay-sx-and-wittgenstein))
        "dennett"
        (quote (~essays/sx-and-dennett/essay-sx-and-dennett))
        "existentialism"
        (quote (~essays/s-existentialism/essay-s-existentialism))
        "platonic-sx"
        (quote (~essays/platonic-sx/essay-platonic-sx))
        :else (quote (~essays/philosophy-index/content))))))

(define
  plan
  (make-page-fn
    "~plans/index/plans-index-content"
    "~plans/"
    "/plan-"
    "-content"))

(define capabilities (fn (&key title &rest args) (quasiquote (~geography/capabilities-content))))

(define modules (fn (&key title &rest args) (quasiquote (~geography/modules-content))))

(define eval-rules (fn (&key title &rest args) (quasiquote (~geography/eval-rules-content))))


(define sxtp (make-page-fn "~applications/sxtp/content" "~applications/sxtp/" nil "-content"))
