diff --git a/README.md b/README.md index 76e9454..288995d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/loader.test.ts b/lib/loader.test.ts index 8b21d04..680cde0 100644 --- a/lib/loader.test.ts +++ b/lib/loader.test.ts @@ -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 @@ -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 + }); }); diff --git a/lib/loader.ts b/lib/loader.ts index e0e54a6..66c3096 100644 --- a/lib/loader.ts +++ b/lib/loader.ts @@ -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 | null }; +type DivSeg = { kind: 'div'; text: string; divStyle: Record; spanStyle: Record }; +type Segment = SpanSeg | DivSeg; + function parseInlineMarkup( text: string, baseStyle: Record, @@ -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; @@ -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 | 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 = {}; - 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; } @@ -201,17 +219,52 @@ 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 = { 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 = { ...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', @@ -219,19 +272,27 @@ function parseInlineMarkup( 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[] = [], @@ -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', diff --git a/profiles/aquaponey/template.yaml b/profiles/aquaponey/template.yaml index 993f0ce..380fd8a 100644 --- a/profiles/aquaponey/template.yaml +++ b/profiles/aquaponey/template.yaml @@ -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: @@ -111,7 +111,9 @@ element: opacity: 0.9 marginTop: 6 children: "pour" - - type: span + - type: lines props: param: name + containerStyle: + alignSelf: stretch children: "{{ name }}" diff --git a/profiles/book/template.yaml b/profiles/book/template.yaml index 0e4a72e..0c43b07 100644 --- a/profiles/book/template.yaml +++ b/profiles/book/template.yaml @@ -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 diff --git a/profiles/build_with_ai/template.yaml b/profiles/build_with_ai/template.yaml index d526264..17d8440 100644 --- a/profiles/build_with_ai/template.yaml +++ b/profiles/build_with_ai/template.yaml @@ -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 diff --git a/profiles/citation/template.yaml b/profiles/citation/template.yaml index 038aaac..0eb53bd 100644 --- a/profiles/citation/template.yaml +++ b/profiles/citation/template.yaml @@ -45,7 +45,6 @@ params: default: "" type: textarea style: - fg: 4 font: name: Montserrat size: 26 @@ -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 diff --git a/profiles/manifesto/template.yaml b/profiles/manifesto/template.yaml index 6b19f02..b224af4 100644 --- a/profiles/manifesto/template.yaml +++ b/profiles/manifesto/template.yaml @@ -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 @@ -225,7 +221,6 @@ element: style: textAlign: center lineHeight: 0.95 - color: "{{ text_color }}" textTransform: uppercase containerStyle: alignItems: center diff --git a/profiles/post_bottom/template.yaml b/profiles/post_bottom/template.yaml index f0eb26f..0d907f2 100644 --- a/profiles/post_bottom/template.yaml +++ b/profiles/post_bottom/template.yaml @@ -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 diff --git a/profiles/post_top/template.yaml b/profiles/post_top/template.yaml index e0ba20f..85bae42 100644 --- a/profiles/post_top/template.yaml +++ b/profiles/post_top/template.yaml @@ -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