HtmlRenderer turns a rule or expression into HTML markup with semantic class names.
Import it from the optional rendering entry point:
import { DefaultHtmlTheme, HtmlRenderer } from '@samatawy/rules/render';
import { RuleParser, Workspace } from '@samatawy/rules';
import { DefaultHtmlTheme, HtmlRenderer } from '@samatawy/rules/render';
const space = new Workspace();
const parser = new RuleParser({ workspace: space });
const rule = parser.parse('IF total > 100 THEN discount = 0.1');
const renderer = new HtmlRenderer('inline');
renderer.setStyles(DefaultHtmlTheme.styles());
const html = renderer.render(rule);
The renderer accepts either:
IF, THEN, ELSE, SET, and command executionHtmlRenderer supports two output modes.
classclass is the default.
const renderer = new HtmlRenderer();
const html = renderer.render(rule);
In this mode, the renderer emits class names such as keyword, variable, operator, function, literal, and block, but it does not inject inline styles.
This is the right mode when:
Example stylesheet:
.keyword { color: rebeccapurple; font-weight: 600; }
.variable { color: darkgreen; }
.literal { color: darkorange; }
.operator { color: dodgerblue; }
.block {
border: 1px solid #c7c7c7;
border-radius: 1.5rem;
padding: 0.5rem 0.75rem;
margin: 0.5rem 0;
}
inlineconst renderer = new HtmlRenderer('inline');
renderer.setStyles(DefaultHtmlTheme.styles());
In this mode, the renderer still emits class names, but it also writes style="..." attributes for any element types you configure with setStyle() or setStyles().
This is useful when:
DefaultHtmlTheme.styles() returns a ready-made inline theme for the built-in element types.
The HTML renderer styles logical element categories instead of specific AST classes. The available element types are:
operatorparenthesisdotcommakeywordliteralvariablearrayfunctioncommandarithmeticcomparisonlogicalternaryswitchlambdablockExample customization:
const renderer = new HtmlRenderer('inline');
renderer.setStyles(DefaultHtmlTheme.styles());
renderer.setStyle('variable', {
color: 'crimson',
'font-weight': '700',
'border-radius': '4px',
});
renderer.setStyle('operator', { color: 'royalblue' });
Supported CSS properties are intentionally limited to text and box styling that can be safely applied to inline output. They include:
color, background, background-colorborder, border-width, border-style, border-color, border-radiusborder-top-left-radius, border-top-right-radius, border-bottom-left-radius, border-bottom-right-radiusmargin, margin-top, margin-right, margin-bottom, margin-leftpadding, padding-top, padding-right, padding-bottom, padding-leftfont-size, font-weight, font-style, font-familytext-align, text-decoration, text-transform, text-shadowline-height, letter-spacing, word-spacing, white-spacesetStyle() and setStyles() only affect generated markup when the renderer is created with 'inline'.'class' mode, styling is entirely controlled by your own CSS.. and rendered segment-by-segment, which lets you style dotted paths cleanly.