# Project Context: docs-index-generator

## Purpose

`docs-index-generator` generates a static documentation index from one or more directories containing Markdown files. It creates a searchable, folder-aware HTML index and copies the original Markdown files into the output directory.

The project originated as a single `generate-index.py`. It was deliberately split into modules without changing the original documentation index design or behavior. The current project also adds PWA installation support.

## Core requirement

**Structural refactoring must not silently change the existing UI, styling, search behavior, folder behavior, pagination, Markdown handling, timestamps, or CLI semantics.**

When modifying the project, prefer small, isolated changes and test the generated output after each meaningful change.

## CLI

```text
./generate-index.py --source PATH:VISIBILITY --output OUTPUT
```

Examples:

```bash
./generate-index.py --source ../docs-public:public --output public
./generate-index.py --source ../docs-public:public --source ../docs-private:private --output public
```

Supported source visibility values are `public` and `private`.

## Source processing

- Markdown files are collected recursively using `*.md`.
- Only files are included.
- A root-level `README.md` is excluded.
- Each entry keeps its source root, relative path, href, visibility, and Git-based modification timestamp.
- Git timestamps use `git -C <source> log -1 --format=%cI -- <relative-path>` when available.
- The timestamp is displayed in `Europe/Vienna`.
- If Git metadata is unavailable, the file mtime is used.
- Multiple source roots are separated in the generated output by visibility directories.

## Existing UI behavior

The generated index uses the existing dark green CRT/hacker-style design. Do not redesign it as part of unrelated changes.

Existing functionality includes:

- recursive folder tree
- public/private/mixed visibility badges
- file and folder metadata
- folder expand/collapse
- search across file names and relative paths
- regex-based search
- multiple search terms must all match
- search highlighting
- invalid regex handling
- pagination with `PAGE_SIZE = 25`
- pagination disabled while searching
- Markdown rendering warning modal
- localStorage dismissal key `markdown-warning-dismissed`

The JavaScript implementation in `generator/static/script.js` is intentionally kept separate from Python and should not be rewritten merely for formatting reasons.

## Project layout

```text
generate-index.py              # tiny executable entry point
README.md                      # user-facing project documentation
CONTEXT.md                     # maintenance / LLM context
generator/
├── __init__.py
├── cli.py                     # argparse, source parsing, main()
├── collector.py               # Markdown discovery
├── generator.py               # generation orchestration + Markdown copying
├── metadata.py                # Git timestamps / Vienna timezone
├── models.py                  # Source dataclass
├── pwa.py                     # PWA assets copied to output
├── render.py                  # Python rendering logic + template assembly
├── tree.py                    # documentation tree model/helpers
├── static/
│   ├── icons/
│   │   ├── icon-192.png
│   │   └── icon-512.png
│   ├── manifest.json          # PWA manifest source
│   ├── script.js              # existing index JavaScript
│   ├── style.css              # existing index CSS
│   └── sw.js                  # PWA service worker source
└── templates/
    └── index.html             # HTML template with placeholders
```

## Rendering architecture

`render.py` reads:

- `templates/index.html`
- `static/style.css`
- `static/script.js`

It replaces these placeholders:

- `__CSS__`
- `__JS__`
- `__TREE_HTML__`
- `__FILE_COUNT__`
- `__GENERATED_AT__`

The result is a self-contained generated `index.html` with the original CSS and JavaScript embedded. The PWA metadata and service-worker registration are part of the HTML template.

## PWA support

The generated output additionally contains:

```text
public/
├── index.html
├── manifest.json
├── sw.js
└── icons/
    ├── icon-192.png
    └── icon-512.png
```

The manifest deliberately uses relative `start_url` and `scope` values so the application works from a GitLab Pages project subpath, not only from a domain root.

The generated HTML contains:

- `rel="manifest"`
- `theme-color`
- mobile web app metadata
- Apple touch icon
- service worker registration using `./sw.js`

The service worker provides an application shell cache and caches successful GET requests. On a network failure it falls back to the cache. The cache name is currently `docs-index-v1`; increment it when a deliberate cache migration is required.

PWA installation still depends on the hosting environment and browser. The generated site must be served over a secure context such as HTTPS for normal service-worker/PWA behavior. `localhost` is also treated as a secure development context by browsers.

## Important implementation rules

1. Do not move or rewrite the existing CSS/JS unless explicitly requested.
2. Do not change the generated HTML structure without a functional reason.
3. Keep Markdown files copied unchanged from their source.
4. Preserve CLI output:
   - `Generated <output>/index.html`
   - `Indexed <n> Markdown files`
5. Preserve `Europe/Vienna` timestamp formatting.
6. Preserve the `README.md` exclusion at the source root.
7. When changing generation behavior, run a clean smoke test with at least nested folders and multiple Markdown files.
8. Prefer complete affected files over patches when presenting changes to the project owner.

## Testing expectations

At minimum:

```bash
python3 -m py_compile generate-index.py generator/*.py
./generate-index.py --source /path/to/docs:public --output public
```

A useful smoke test should verify:

- expected Markdown file count
- root `README.md` is excluded
- nested folders appear
- copied Markdown content is unchanged
- `index.html` is generated
- `manifest.json`, `sw.js`, and both icons are generated
- PWA references are present in `index.html`

## Known warning

The JavaScript contains a regex literal such as `/\s+/` inside a Python-generated string. Python may report a `SyntaxWarning` about an invalid escape sequence. This warning existed in the original implementation and is not, by itself, a functional failure.

## Maintenance preference

The project owner prefers practical, copy-pasteable commands and complete affected files. Avoid unrelated cleanup or stylistic rewrites when making functional changes.
