# utensil/forest architecture

> Architecture diagram of the GitHub repository utensil/forest, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/utensil/forest
- Repository: https://github.com/utensil/forest

## Overview

##### Main Components
- Source Content and Data Store:
– The “trees” directory holds hundreds of .tree files that represent the note trees (Zettelkasten notes).
– Other directories also include “tex” for LaTeX source, “assets” for HTML/CSS/JS, “theme” for styling, and “templates” for layout definitions.
- Build Pipeline and Task Automation:
– Build scripts such as “build.sh” and “dev.sh” coordinate the conversion of source content (trees, tex, assets) into a static website.
– “just”, along with shell and Python scripts, acts as a task runner to manage environment setup and file transformation.
- Compilation & Rendering Components:
– Forester (via opam) processes the trees and renders them to an intermediate XML/HTML representation.
– Bun is used to compile and bundle JavaScript/TypeScript code (found in the “bun” directory) for interactive components.
– Additional build tools include dvisvgm for converting LaTeX to SVG.
- Development and Deployment Environment:
– The development server (invoked by “dev.sh”) watches file changes (e.g., with watchexec) and serves the site locally (http://localhost:1314).
– GitHub Actions (evident from “.github/workflows” files) manage CI/CD, running “build.sh” during deployment.
- Experimental / Extension Layers:
– Experimental WASM builds (conditional in build.sh) integrate Rust-based components.
– Configuration and environment files (e.g., .env.in, bundler configurations) support the cross-tool integration.

##### Out the Relationships and Interactions
– The content creation happens in “trees”, “tex”, “assets”, “templates”, “theme”, and config files.
- Build Process:
– “dev.sh” (or “build.sh” when triggered by CI) initiates the build pipeline.
– Forester reads the note trees and uses configuration from “forest.toml” and “forest” setups (like “config.lua” and “uts-plugins.lua”) to generate intermediate content.
– Bun takes the JavaScript/TypeScript source (files in “bun”) and compiles these, possibly integrating WASM components if they build.
– Other scripts (e.g., “scripts/build-dvisvgm.sh”) ensure that LaTeX content is converted properly.
- Output and Serving:
– The processed files are assembled into the final static website, which is then served by the development server.
– GitHub Actions use “build.sh” to rebuild the site and deploy it (as seen in the workflow file for gh-pages).

##### Architectural Patterns and Design Principles
- Pipeline Architecture:
– There is a clear sequence from source content → build processing (via Forester and bundlers) → final static site generation.
- Modularity:
– Separation of concerns is evident: each directory (assets, trees, bun, scripts) serves a distinct purpose.
– Use of task runners (“just”, shell/Python scripts) enables isolated handling of tasks like asset compilation, linting (Biome), and file watching.
- Extensibility:
– Experimental features, like WASM integration and the use of “just”, are designed so that they do not block the main build process.
- Integration of Heterogeneous Technologies:
– The system combines OCaml-based tools (Forester via opam), JavaScript/TypeScript (via Bun), and traditional UNIX scripting.

##### Additional Inclusions
- External dependencies like opam, rustup, and brew-managed packages that are prerequisites.
- Fallback paths in the build process (e.g., WASM build may fail silently but does not hinder the main website build).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Content Sources"
        trees["Note Trees (.tree)"]:::content
        tex["LaTeX Sources (.tex,.bib)"]:::content
        assets["HTML/CSS/JS Assets"]:::content
        templates["Layout Templates"]:::content
        theme["Site Theme"]:::content
    end

    subgraph "Build Process"
        build["Build Scripts (build.sh,etc)"]:::build
        taskrunner["Task Runner (just)"]:::build
        helpers["Helper Scripts (build-dvisvgm.sh,etc)"]:::build
        envconfig["Env & Config (.env.in)"]:::build
    end

    subgraph "Compilation & Rendering"
        forester["Forester Engine"]:::engine
        bun["Bun Bundler (JS/TS)"]:::engine
    end

    subgraph "Deployment/Monitoring"
        static["Static Website"]:::deployment
        dev["Dev Server (dev.sh)"]:::deployment
        ghactions["GitHub Actions CI"]:::deployment
    end

    subgraph "Experimental Extensions"
        wasm["Experimental WASM Build"]:::experimental
    end

    %% Content Sources trigger build process
    trees -->|"triggers"| build
    tex -->|"triggers"| build
    assets -->|"triggers"| build
    templates -->|"triggers"| build
    theme -->|"triggers"| build

    %% Build process components feed into rendering engines
    build -->|"runs"| forester
    envconfig -->|"configures"| forester
    taskrunner -->|"bundles"| bun
    helpers -->|"converts"| bun

    %% Experimental WASM branch from main build scripts
    build -->|"executes"| wasm
    wasm -->|"integrates"| static

    %% Rendering outputs merge into static website generation
    forester -->|"renders"| static
    bun -->|"bundles"| static

    %% Deployment targets from the static website
    static -->|"serves"| dev
    static -->|"deploys"| ghactions

    %% Click Events
    click trees "https://github.com/utensil/forest/tree/main/trees"
    click tex "https://github.com/utensil/forest/tree/main/tex"
    click assets "https://github.com/utensil/forest/tree/main/assets"
    click templates "https://github.com/utensil/forest/tree/main/templates"
    click theme "https://github.com/utensil/forest/tree/main/theme"
    click build "https://github.com/utensil/forest/blob/main/build.sh"
    click taskrunner "https://github.com/utensil/forest/tree/main/justfile"
    click helpers "https://github.com/utensil/forest/blob/main/scripts/build-dvisvgm.sh"
    click envconfig "https://github.com/utensil/forest/blob/main/.env.in"
    click forester "https://github.com/utensil/forest/blob/main/forest.toml"
    click bun "https://github.com/utensil/forest/tree/main/bun"
    click dev "https://github.com/utensil/forest/blob/main/dev.sh"
    click ghactions "https://github.com/utensil/forest/tree/main/.github/workflows"

    %% Styles
    classDef content fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef build fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef engine fill:#A9DFBF,stroke:#333,stroke-width:2px;
    classDef deployment fill:#FAD7A0,stroke:#333,stroke-width:2px;
    classDef experimental fill:#F5B7B1,stroke:#333,stroke-width:2px;
```
