gram/packages/renderer/README.md
abiwab c7d18ed6a7
All checks were successful
CI / Build, Test & Publish (push) Successful in 3m14s
feat: plan the mise en place per section, upfront or per working day (#76)
Adds the mise en place breakdown per section (`miseEnPlace`) and three complete timelines (`schedules.perSection`, `upfront`, `perSession`), read by the renderer, CLI (`--mise-en-place`), VS Code (`gram.miseEnPlace`), language server, playground and analyzer diff.

None of the new fields (`generator`, `miseEnPlace`, `schedules`) or options were in 1.3.0. The existing timing fields keep their values and are marked deprecated (removal in 2.0.0, tracked in `deprecations.yaml`).

This is the base for the upcoming scheduler work; the shape of `schedules` is expected to be reworked before 1.4.0 ships, so this is not a release PR.

Checks: lint, typecheck, 1397 tests, conformance 99/99 and deprecations:check pass.
Reviewed-on: #76
2026-10-04 11:07:13 +02:00

110 lines
3.6 KiB
Markdown

# @gram-lang/renderer
[![npm version](https://badge.fury.io/js/@gram-lang%2Frenderer.svg)](https://www.npmjs.com/package/@gram-lang/renderer)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
*Part of the [Gram monorepo](https://git.gram-lang.org/gram-lang/gram).*
The universal renderer for the Gram recipe language. It takes compiled or analyzed recipe ASTs and renders them into structured Markdown or semantic HTML with support for custom visual elements and class names.
---
## 📚 General Documentation
For full syntax specifications, grammar details, cheatsheets, and best practices, please refer to the **[Gram Documentation](https://docs.gram-lang.org/)**.
---
## 🛠️ Installation
```bash
npm install @gram-lang/renderer
# or
bun add @gram-lang/renderer
```
---
## ⚡ Usage
```typescript
import {
toMarkdown,
toHTML,
toPrintHTML,
toGanttHTML,
attachGanttInteractivity
} from '@gram-lang/renderer';
const recipe = {
title: "Simple Crepes",
metrics: { activeTime: 10 },
// The timelines a compiled recipe carries, one per mode (`perSection`,
// `upfront`, `perSession`; `compile()` produces them for you): only the one
// you render is read. `metrics.totalTime` is deprecated: it is only read when
// `schedules` is missing.
schedules: {
perSection: { totalTime: 30, idleTime: 20, blocks: [], sessions: [] }
},
shopping_list: [
{ id: "flour", qty: 200, unit: "g" }
],
sections: [
{
title: "Preparation",
steps: [
{ type: "step", content: ["Whisk everything together."] }
]
}
]
};
// 1. Simple Markdown rendering
const markdown = toMarkdown(recipe);
// 2. Customized HTML rendering with custom icons (Inversion of Control)
const html = toHTML(recipe, {
icons: {
clock: '<span class="icon-clock">🕒</span>',
fire: '<span class="icon-fire">🔥</span>'
},
classes: {
recipeTitle: "custom-title",
recipeMeta: "flex gap-2"
}
});
// 3. Print-optimized HTML rendering (self-contained, single-page layout)
const printHtml = toPrintHTML(recipe);
// 4. Gantt Chart timeline rendering (HTML fragment + client-side interactivity)
const ganttHtml = toGanttHTML(recipe, { lang: 'en' });
const container = document.getElementById('gantt-container');
container.innerHTML = ganttHtml;
const handle = attachGanttInteractivity(container, {
timeMode: 'forward', // 'forward' (T+), 'reverse' (T-), or 'target'
targetTime: '19:30', // Target serve time when timeMode is 'target'
isCompactMode: false // Toggle compact row height
});
```
---
## 🏗️ Structure
* `src/index.ts`: The main entry point exporting public APIs.
* `src/types.ts`: TypeScript interfaces for `RendererOptions`, `RendererIcons`, and `RendererClasses`.
* `src/utils.ts`: Common helpers (decimal-to-fraction formatting, HTML escaping, duration formatting).
* `src/gantt/`: Gantt chart layout computation, HTML fragment rendering, and event delegation interactivity (`layout.ts`, `render.ts`, `interactivity.ts`, `types.ts`).
* `src/formatters/element.ts`: Universal formatter for individual AST nodes (ingredients, cookware, timers, alternative groups).
* `src/formatters/markdown.ts`: Orchestrates Markdown generation for full recipes.
* `src/formatters/html.ts`: Orchestrates HTML generation for full recipes using classes and icons injection.
* `src/formatters/print.ts`: Orchestrates self-contained, print-optimized HTML generation (`toPrintHTML`).
* `gantt.css`: Stylesheet for the Gantt chart timeline component.
---
## 📄 License
This project is licensed under the GPL-3.0 License.