Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ element:
### Inline markup (inside `lines` and `span` children)

```
[FG=2]colored text[/] → text in palette color 2
[BG=1]highlighted[/] → background in palette color 1
[FS=48 FW=900]big bold[/] → font-size 48, font-weight 900
[FN=Caveat]handwriting[/] → font-family override
[ROT=45]rotated[/] → rotate transform
[ALIGN=center]centered line[/] → line-level alignment (lines only)
[SPAN FG=2]colored text[/] → inline span, text in palette color 2
[SPAN BG=1]highlighted[/] → inline span, background behind text only
[SPAN FS=48 FW=900]big bold[/] → inline span, font-size 48, font-weight 900
[SPAN FN=Caveat]handwriting[/] → inline span, font-family override
[SPAN ROT=45]rotated[/] → inline span, rotate transform
[DIV BG=1]full-width band[/] → block div, background spans full line width
[DIV FG=2 BG=1 ALIGN=right]text[/] → block div, full-width, text right-aligned
[ALIGN=center]centered line → line-level alignment prefix (lines only, no [/])
```

### Grouping sub-params
Expand Down
77 changes: 75 additions & 2 deletions lib/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ describe('expandSpecialElements — coerceStyle', () => {
// ── expandSpecialElements — inline markup ────────────────────────────────────

describe('expandSpecialElements — inline markup', () => {
it('[FG=N] applies palette color to the matching span', () => {
it('[SPAN FG=N] applies palette color to the matching span', () => {
const palette = ['#FF0000', '#00FF00'];
const node = {
type: 'lines',
props: { style: { fontSize: 28 }, children: '[FG=1]red text[/]' },
props: { style: { fontSize: 28 }, children: '[SPAN FG=1]red text[/]' },
};
const result = expandSpecialElements(node, palette);
// result → column div → line div → inline element div → span
Expand All @@ -222,4 +222,77 @@ describe('expandSpecialElements — inline markup', () => {
const lineDiv = result.props.children[0];
expect(lineDiv.props.style.justifyContent).toBe('center');
});

it('[SPAN FG=N] behaves like [FG=N] (explicit span alias)', () => {
const palette = ['#FF0000', '#00FF00'];
const node = {
type: 'lines',
props: { style: { fontSize: 28 }, children: '[SPAN FG=1]red text[/]' },
};
const result = expandSpecialElements(node, palette);
const lineDiv = result.props.children[0];
const inlineEl = lineDiv.props.children[0];
const span = inlineEl.props.children[0];
expect(span.props.style.color).toBe('#FF0000');
expect(span.props.children).toBe('red text');
});

it('[DIV BG=N] creates a full-width div inside the line wrapper, with flex: 1 and backgroundColor', () => {
const palette = ['#0D1B2A', '#FFFFFF'];
const node = {
type: 'lines',
props: { style: { fontSize: 28 }, children: '[DIV BG=1]Full width band[/]' },
};
const result = expandSpecialElements(node, palette);
const lineDiv = result.props.children[0]; // line wrapper (flex row)
const divEl = lineDiv.props.children[0]; // the [DIV] block
expect(divEl.type).toBe('div');
expect(divEl.props.style.flex).toBe(1);
expect(divEl.props.style.backgroundColor).toBe('#0D1B2A');
});

it('[DIV FG=N] applies color to the inner span, not the outer div', () => {
const palette = ['#0D1B2A', '#FFFFFF'];
const node = {
type: 'lines',
props: { style: { fontSize: 28 }, children: '[DIV FG=2]White text[/]' },
};
const result = expandSpecialElements(node, palette);
const lineDiv = result.props.children[0]; // line wrapper
const divEl = lineDiv.props.children[0]; // the [DIV] block
expect(divEl.type).toBe('div');
expect(divEl.props.style.color).toBeUndefined();
const innerSpan = divEl.props.children[0];
expect(innerSpan.props.style.color).toBe('#FFFFFF');
});

it('[DIV ALIGN=right] sets justifyContent: flex-end on the block div', () => {
const node = {
type: 'lines',
props: { style: { fontSize: 28 }, children: '[DIV ALIGN=right]Author[/]' },
};
const result = expandSpecialElements(node);
const lineDiv = result.props.children[0]; // line wrapper
const divEl = lineDiv.props.children[0]; // the [DIV] block
expect(divEl.type).toBe('div');
expect(divEl.props.style.justifyContent).toBe('flex-end');
});

it('[DIV] mixed with plain text wraps both in a flex row, DIV gets flex: 1', () => {
const palette = ['#0D1B2A', '#FFFFFF'];
const node = {
type: 'lines',
props: { style: { fontSize: 28 }, children: 'Label: [DIV BG=1]value[/]' },
};
const result = expandSpecialElements(node, palette);
const lineDiv = result.props.children[0]; // line wrapper (flex row)
const outerEl = lineDiv.props.children[0]; // outer flex container for mixed content
expect(outerEl.type).toBe('div');
expect(outerEl.props.style.display).toBe('flex');
const children = outerEl.props.children;
expect(children[0].type).toBe('span');
expect(children[0].props.children).toBe('Label: ');
expect(children[1].type).toBe('div');
expect(children[1].props.style.flex).toBe(1); // fills remaining horizontal space
});
});
106 changes: 89 additions & 17 deletions lib/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ function resolveStyle(

type Align = 'left' | 'center' | 'right';

const alignToJustify = (a: Align) =>
a === 'center' ? 'center' : a === 'right' ? 'flex-end' : 'flex-start';

type SpanSeg = { kind: 'span'; text: string; override: Record<string, any> | null };
type DivSeg = { kind: 'div'; text: string; divStyle: Record<string, any>; spanStyle: Record<string, any> };
type Segment = SpanSeg | DivSeg;

function parseInlineMarkup(
text: string,
baseStyle: Record<string, any>,
Expand All @@ -169,6 +176,10 @@ function parseInlineMarkup(
return null;
};

// Convert leading/trailing spaces to non-breaking spaces to prevent CSS whitespace collapsing
const preserveSpaces = (s: string) =>
s.replace(/^( +)/, m => ' '.repeat(m.length)).replace(/( +)$/, m => ' '.repeat(m.length));

// Extract leading [ALIGN=x] tag (no closing [/])
let align: Align = 'left';
let workText = text;
Expand All @@ -182,17 +193,24 @@ function parseInlineMarkup(
return { element: { type: 'span', props: { style: baseStyle, children: workText } }, align };
}

// Parse inline tags: [FG=N BG=N FN=name FS=N FW=N ROT=N]text[/]
// Parse inline tags: [DIV? SPAN? FG=N BG=N FN=name FS=N FW=N ROT=N ALIGN=x]text[/]
const tagRegex = /\[([^\]\/][^\]]*)\]([\s\S]*?)\[\/\]/g;
const segments: Array<{ text: string; override: Record<string, any> | null }> = [];
const segments: Segment[] = [];
let lastIndex = 0;

for (const match of workText.matchAll(tagRegex)) {
if (match.index! > lastIndex) {
segments.push({ text: workText.slice(lastIndex, match.index), override: null });
segments.push({ kind: 'span', text: workText.slice(lastIndex, match.index), override: null });
}
const tagContent = match[1];
const isDivBlock = /^DIV(?:\s|$)/i.test(tagContent);
const isSpanExplicit = /^SPAN(?:\s|$)/i.test(tagContent);
const attrStr = (isDivBlock || isSpanExplicit) ? tagContent.replace(/^(?:DIV|SPAN)\s*/i, '') : tagContent;

const override: Record<string, any> = {};
for (const prop of match[1].matchAll(/(\w+)=([^\s\]]+)/g)) {
let blockAlign: Align = 'left';

for (const prop of attrStr.matchAll(/(\w+)=([^\s\]]+)/g)) {
const key = prop[1].toUpperCase();
const val = prop[2];
if (key === 'FG') { const c = resolveColor(val); if (c) override.color = c; }
Expand All @@ -201,37 +219,80 @@ function parseInlineMarkup(
else if (key === 'FS') { const n = parseInt(val, 10); if (!isNaN(n)) override.fontSize = n; }
else if (key === 'FW') { const n = parseInt(val, 10); if (!isNaN(n)) override.fontWeight = n; }
else if (key === 'ROT') { const n = parseInt(val, 10); if (!isNaN(n)) override.transform = `rotate(${n}deg)`; }
else if (isDivBlock && key === 'ALIGN') { blockAlign = val.toLowerCase() as Align; }
}

const tagText = preserveSpaces(match[2]);

if (isDivBlock) {
const divStyle: Record<string, any> = { display: 'flex', flex: 1, justifyContent: alignToJustify(blockAlign) };
if (override.backgroundColor !== undefined) divStyle.backgroundColor = override.backgroundColor;
if (override.transform !== undefined) divStyle.transform = override.transform;

const spanStyle: Record<string, any> = { ...baseStyle };
if (override.color !== undefined) spanStyle.color = override.color;
if (override.fontFamily !== undefined) spanStyle.fontFamily = override.fontFamily;
if (override.fontSize !== undefined) spanStyle.fontSize = override.fontSize;
if (override.fontWeight !== undefined) spanStyle.fontWeight = override.fontWeight;

segments.push({ kind: 'div', text: tagText, divStyle, spanStyle });
} else {
segments.push({ kind: 'span', text: tagText, override });
}
segments.push({ text: match[2], override });
lastIndex = match.index! + match[0].length;
}
if (lastIndex < workText.length) segments.push({ text: workText.slice(lastIndex), override: null });
if (lastIndex < workText.length) segments.push({ kind: 'span', text: workText.slice(lastIndex), override: null });

const hasMarkup = segments.some(s => s.override && Object.keys(s.override).length > 0);
const hasMarkup = segments.some(s =>
s.kind === 'div' || (s.kind === 'span' && s.override && Object.keys(s.override).length > 0)
);
if (!hasMarkup && segments.length <= 1) {
return { element: { type: 'span', props: { style: baseStyle, children: workText } }, align };
}

// Single DIV block — return directly, no outer wrapper needed
if (segments.length === 1 && segments[0].kind === 'div') {
const seg = segments[0] as DivSeg;
return {
element: {
type: 'div',
props: {
style: seg.divStyle,
children: [{ type: 'span', props: { style: seg.spanStyle, children: seg.text } }],
},
},
align,
};
}

return {
element: {
type: 'div',
props: {
style: { display: 'flex', alignItems: 'baseline' },
children: segments
.filter(s => s.text.length > 0)
.map(s => ({
type: 'span',
props: { style: s.override ? { ...baseStyle, ...s.override } : baseStyle, children: s.text },
})),
.map(s => {
if (s.kind === 'div') {
return {
type: 'div',
props: {
style: s.divStyle,
children: [{ type: 'span', props: { style: s.spanStyle, children: s.text } }],
},
};
}
return {
type: 'span',
props: { style: s.override ? { ...baseStyle, ...s.override } : baseStyle, children: s.text },
};
}),
},
},
align,
};
}

const alignToJustify = (a: Align) =>
a === 'center' ? 'center' : a === 'right' ? 'flex-end' : 'flex-start';

export function expandSpecialElements(
node: any,
palette: string[] = [],
Expand Down Expand Up @@ -314,11 +375,22 @@ export function expandSpecialElements(
const doSplit = splitProp !== false && splitProp !== 'false';

if (!doSplit) {
const { element } = parseInlineMarkup(text, { ...style, whiteSpace: 'pre-wrap' }, palette);
return { ...element, props: { ...element.props, style: { ...element.props.style, alignSelf: 'flex-start' } } };
const { element, align } = parseInlineMarkup(text, { ...style, whiteSpace: 'pre-wrap' }, palette);
if (align === 'left') {
return { ...element, props: { ...element.props, style: { ...element.props.style, alignSelf: 'flex-start' } } };
}
return {
type: 'div',
props: { style: { display: 'flex', justifyContent: alignToJustify(align) }, children: [element] },
};
}

const rawLines = text.split('\n');
const allLines = text.split('\n');
const firstNonEmpty = allLines.findIndex(l => l.trim() !== '');
const lastNonEmpty = [...allLines].reverse().findIndex(l => l.trim() !== '');
const rawLines = firstNonEmpty === -1
? []
: allLines.slice(firstNonEmpty, allLines.length - lastNonEmpty);
const lineHeight = Number(style.fontSize ?? 28);
return {
type: 'div',
Expand Down
6 changes: 4 additions & 2 deletions profiles/aquaponey/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ params:
name:
description: Prénom ou nom de la personne
required: true
default: "[Prénom]"
default: "[ALIGN=center]SOCIETE"
style:
fg: 3
font:
Expand Down Expand Up @@ -111,7 +111,9 @@ element:
opacity: 0.9
marginTop: 6
children: "pour"
- type: span
- type: lines
props:
param: name
containerStyle:
alignSelf: stretch
children: "{{ name }}"
2 changes: 1 addition & 1 deletion profiles/book/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ params:
description: Title lines — use [FS=N FW=N] for per-line size (e.g. first line smaller)
required: true
default: |-
[FS=44 FW=700]Learn About[/]
[SPAN FS=44 FW=700]Learn About[/]
The
DevOps
Profession
Expand Down
2 changes: 1 addition & 1 deletion profiles/build_with_ai/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ params:

pipeline:
description: "Pipeline steps — use [FG=N]text[/] for colors (3=green, 4=orange, 6=gray)"
default: "[FG=6]SPEC[/] HUMAN · [FG=6]CODE[/] AI · [FG=6]HUMAN XP[/] [FG=4]MEDIUM[/] · [FG=6]REVIEW[/] [FG=3]HUMAN[/]"
default: "[SPAN FG=6]SPEC[/] HUMAN · [SPAN FG=6]CODE[/] AI · [SPAN FG=6]HUMAN XP[/] [SPAN FG=4]MEDIUM[/] · [SPAN FG=6]REVIEW[/] [SPAN FG=3]HUMAN[/]"
type: textarea
style:
fg: 5
Expand Down
2 changes: 0 additions & 2 deletions profiles/citation/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ params:
default: ""
type: textarea
style:
fg: 4
font:
name: Montserrat
size: 26
Expand All @@ -63,7 +62,6 @@ params:
default: "Le succès, c'est tomber sept fois et se relever huit."
type: textarea
style:
fg: 2
font:
name: AsapSharp
size: 72
Expand Down
5 changes: 0 additions & 5 deletions profiles/manifesto/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ params:
name: BarlowCondensed
size: 128
weight: 900
text_color:
description: Headline color
default: 2
type: color
tagline:
description: Tagline / subtitle at the bottom
default: ETAT DES LIEUX
Expand Down Expand Up @@ -225,7 +221,6 @@ element:
style:
textAlign: center
lineHeight: 0.95
color: "{{ text_color }}"
textTransform: uppercase
containerStyle:
alignItems: center
Expand Down
2 changes: 1 addition & 1 deletion profiles/post_bottom/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ params:
weight: 900
description:
description: "Body text (multi-line, use [FG=N] [BG=N] [FN=name] [FS=N] [FW=N] for inline style)"
default: "GENERATE [FG=3 BG=1]CLEAN[/], IMPACTFUL SLIDES\nFOR YOUR SOCIAL MEDIA POSTS [FG=3 BG=1]IN SECONDS[/]"
default: "GENERATE [SPAN FG=3 BG=1]CLEAN[/], IMPACTFUL SLIDES\nFOR YOUR SOCIAL MEDIA POSTS [SPAN FG=3 BG=1]IN SECONDS[/]"
type: textarea
style:
fg: 2
Expand Down
2 changes: 1 addition & 1 deletion profiles/post_top/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ params:
weight: 900
description:
description: "Body text (multi-line, use [FG=N] [BG=N] [FN=name] [FS=N] [FW=N] for inline style)"
default: "GENERATE [FG=3 BG=1]CLEAN[/], IMPACTFUL SLIDES\nFOR YOUR SOCIAL MEDIA POSTS [FG=3 BG=1]IN SECONDS[/]"
default: "GENERATE [SPAN FG=3 BG=1]CLEAN[/], IMPACTFUL SLIDES\nFOR YOUR SOCIAL MEDIA POSTS [SPAN FG=3 BG=1]IN SECONDS[/]"
type: textarea
style:
fg: 2
Expand Down
Loading