···11+# Architecture
22+33+`leaf` is a terminal Markdown previewer built around a small set of focused modules:
44+55+- `src/main.rs`
66+ - entrypoint
77+ - loads CLI options
88+ - reads the initial document or opens the file picker
99+ - initializes terminal + syntax/theme assets
1010+1111+- `src/app.rs`
1212+ - central runtime state
1313+ - document content, TOC, search state, watch state
1414+ - theme picker + file picker state
1515+ - cache invalidation and document replacement helpers
1616+1717+- `src/markdown.rs`
1818+ - Markdown parsing and render preparation
1919+ - heading, TOC, list, table, blockquote, code block rendering
2020+ - width-aware wrapping helpers
2121+2222+- `src/render.rs`
2323+ - draws the TUI with `ratatui`
2424+ - main content, TOC, status bar
2525+ - modal rendering for help, theme picker, and file picker
2626+2727+- `src/runtime.rs`
2828+ - event loop
2929+ - keyboard/mouse handling
3030+ - watch polling
3131+ - resize-driven render width synchronization
3232+3333+- `src/theme.rs`
3434+ - UI and Markdown theme presets
3535+ - active theme preset selection
3636+ - syntect theme mapping
3737+3838+- `src/cli.rs`
3939+ - command-line parsing
4040+ - usage/version text
4141+4242+- `src/terminal.rs`
4343+ - raw mode / alternate screen lifecycle
4444+ - terminal restore guarantees
4545+4646+- `src/tests.rs`
4747+ - regression tests for rendering and state behavior
4848+4949+## Execution flow
5050+5151+1. `main.rs` parses CLI options.
5252+2. A document is loaded from:
5353+ - a file argument, or
5454+ - `stdin`, or
5555+ - the file picker if no input is provided interactively.
5656+3. `markdown.rs` parses the source into rendered lines + TOC.
5757+4. `App` stores the state and caches.
5858+5. `runtime.rs` runs the event loop.
5959+6. `render.rs` draws each frame from `App`.
6060+6161+## Important state transitions
6262+6363+- document reload / open:
6464+ - source changes
6565+ - rendered lines and TOC are rebuilt
6666+ - caches are refreshed
6767+6868+- resize:
6969+ - effective render width is recomputed
7070+ - Markdown is reparsed width-aware
7171+7272+- theme preview:
7373+ - previewed content is reparsed and cached per preset
7474+ - `Esc` restores the original theme
7575+7676+- search:
7777+ - query state lives in `App`
7878+ - active match drives highlight + scroll position
7979+8080+## Current hotspots
8181+8282+- `src/app.rs` still centralizes many responsibilities
8383+- `src/markdown.rs` is the densest module and the main future split candidate
8484+- `src/render.rs` is growing as more modal UI is added