Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | 1281x 1281x 1281x 1176x 1281x 1176x 107x 104x 104x 103x 103x 107x 107x 107x 107x 107x 191x 191x 191x 191x 191x 59x 59x 35x 35x 59x 59x 59x 35x 48x 48x 35x 25x 25x 49x 49x 25x 24x 48x 48x 48x 24x 35x 25x 75x 50x 50x 49x 49x 25x 25x 25x 25x 25x 25x 35x 27x 27x 27x 27x 61x 61x 61x 61x 36x 36x 35x 39x 37x 36x 36x 36x 39x 39x 35x 35x 35x 35x 35x 35x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Generators/SwotContent
* @description Pure functions for building SWOT analysis HTML sections.
* Generates accessible, CSS-only SWOT matrix visualizations that can be
* embedded in any article type. Designed for agentic workflows to extend
* articles with strategic analysis using data from MCP servers or other sources.
*
* The SWOT framework covers:
* - **Strengths**: Internal positive factors
* - **Weaknesses**: Internal negative factors
* - **Opportunities**: External positive factors
* - **Threats**: External negative factors
*
* Multi-dimensional extension adds:
* - **Political, Economic, Social, Legal, Geopolitical** dimension drill-downs
* - **Stakeholder perspectives** (Citizens, Industry, NGOs, MEPs, Governments, Media)
* - **Temporal analysis** (short-term, medium-term, long-term)
* - **Cross-references** linking items to EP documents/votes
*/
import { escapeHTML, isSafeURL } from '../utils/file-utils.js';
import {
getLocalizedString,
SWOT_STRINGS,
MULTI_DIMENSIONAL_SWOT_STRINGS,
} from '../constants/languages.js';
import type {
SwotAnalysis,
SwotItem,
SwotStrings,
SwotDimension,
SwotCrossReference,
TemporalSwotAssessment,
MultiDimensionalSwot,
MultiDimensionalSwotStrings,
StakeholderType,
} from '../types/index.js';
// ─── Sub-section builders ────────────────────────────────────────────────────
/**
* Build a single SWOT item HTML.
*
* @param item - SWOT item with text and optional severity
* @returns HTML list item string
*/
function buildSwotItem(item: SwotItem): string {
const severityClass = item.severity ? ` swot-severity-${escapeHTML(item.severity)}` : '';
const severityBadge = item.severity
? ` <span class="swot-severity-badge" role="img" aria-label="${escapeHTML(item.severity)}"></span>`
: '';
return `<li class="swot-item${severityClass}">${escapeHTML(item.text)}${severityBadge}</li>`;
}
/**
* Build a single SWOT quadrant HTML.
*
* @param quadrantKey - Quadrant identifier (strengths/weaknesses/opportunities/threats)
* @param items - Items in this quadrant
* @param label - Localized quadrant heading
* @param description - Accessible description
* @returns HTML string for one quadrant
*/
function buildSwotQuadrant(
quadrantKey: string,
items: readonly SwotItem[],
label: string,
description: string
): string {
const itemsHtml =
items.length > 0
? items.map((item) => buildSwotItem(item)).join('\n ')
: '<li class="swot-item swot-empty">—</li>';
return `<div class="swot-quadrant swot-${escapeHTML(quadrantKey)}" role="region" aria-label="${escapeHTML(label)}">
<h4 class="swot-quadrant-heading">${escapeHTML(label)}</h4>
<p class="swot-quadrant-desc">${escapeHTML(description)}</p>
<ul class="swot-list">
${itemsHtml}
</ul>
</div>`;
}
// ─── Main builder ────────────────────────────────────────────────────────────
/**
* Build a complete SWOT analysis section HTML.
*
* Generates an accessible CSS Grid-based SWOT matrix that can be embedded
* in any article type. The visualization is pure HTML/CSS with no JavaScript
* required, ensuring compatibility with strict CSP policies.
*
* Returns an empty string if the analysis is null/undefined or contains
* no items in any quadrant.
*
* @param analysis - SWOT analysis data (null/undefined returns empty string)
* @param lang - BCP 47 language code for localized headings (defaults to "en")
* @param heading - Optional custom section heading override
* @returns HTML section string or empty string
*/
export function buildSwotSection(
analysis: SwotAnalysis | null | undefined,
lang: string = 'en',
heading?: string
): string {
if (!analysis) return '';
const totalItems =
analysis.strengths.length +
analysis.weaknesses.length +
analysis.opportunities.length +
analysis.threats.length;
if (totalItems === 0) return '';
const strings: SwotStrings = getLocalizedString(SWOT_STRINGS, lang);
const sectionHeading = heading ?? analysis.title ?? strings.sectionHeading;
const strengthsHtml = buildSwotQuadrant(
'strengths',
analysis.strengths,
strings.strengthsLabel,
strings.strengthsDesc
);
const weaknessesHtml = buildSwotQuadrant(
'weaknesses',
analysis.weaknesses,
strings.weaknessesLabel,
strings.weaknessesDesc
);
const opportunitiesHtml = buildSwotQuadrant(
'opportunities',
analysis.opportunities,
strings.opportunitiesLabel,
strings.opportunitiesDesc
);
const threatsHtml = buildSwotQuadrant(
'threats',
analysis.threats,
strings.threatsLabel,
strings.threatsDesc
);
return `
<section class="swot-analysis" role="region" aria-label="${escapeHTML(sectionHeading)}">
<h2>${escapeHTML(sectionHeading)}</h2>
<div class="swot-matrix">
<div class="swot-axis-labels">
<span class="swot-axis-internal">${escapeHTML(strings.internalLabel)}</span>
<span class="swot-axis-external">${escapeHTML(strings.externalLabel)}</span>
</div>
<div class="swot-grid">
<div class="swot-row swot-row-positive">
${strengthsHtml}
${opportunitiesHtml}
</div>
<div class="swot-row swot-row-negative">
${weaknessesHtml}
${threatsHtml}
</div>
</div>
</div>
</section>`;
}
// ─── Multi-dimensional SWOT helpers ─────────────────────────────────────────
/**
* Build a compact SWOT matrix grid (without outer section wrapper).
* Used for embedding inside dimension or temporal detail sections.
*
* @param analysis - SWOT analysis data
* @param strings - Localized SWOT strings
* @returns HTML string for a compact quadrant grid
*/
function buildSwotMiniGrid(analysis: SwotAnalysis, strings: SwotStrings): string {
const sHtml = buildSwotQuadrant(
'strengths',
analysis.strengths,
strings.strengthsLabel,
strings.strengthsDesc
);
const wHtml = buildSwotQuadrant(
'weaknesses',
analysis.weaknesses,
strings.weaknessesLabel,
strings.weaknessesDesc
);
const oHtml = buildSwotQuadrant(
'opportunities',
analysis.opportunities,
strings.opportunitiesLabel,
strings.opportunitiesDesc
);
const tHtml = buildSwotQuadrant(
'threats',
analysis.threats,
strings.threatsLabel,
strings.threatsDesc
);
return `<div class="swot-grid swot-mini-grid">
<div class="swot-row swot-row-positive">${sHtml}${oHtml}</div>
<div class="swot-row swot-row-negative">${wHtml}${tHtml}</div>
</div>`;
}
/**
* Map a dimension name to its localized label.
*
* @param name - Dimension identifier
* @param mdStrings - Localized multi-dimensional SWOT strings
* @returns Localized dimension label
*/
function getDimensionLabel(
name: SwotDimension['name'],
mdStrings: MultiDimensionalSwotStrings
): string {
const map: Record<SwotDimension['name'], string> = {
political: mdStrings.dimensionPolitical,
economic: mdStrings.dimensionEconomic,
social: mdStrings.dimensionSocial,
legal: mdStrings.dimensionLegal,
geopolitical: mdStrings.dimensionGeopolitical,
};
return map[name];
}
/**
* Build the expandable dimension drill-down sections.
* Each dimension is rendered as an accessible `<details>` disclosure widget.
*
* @param dimensions - Array of dimension data
* @param swotStrings - Localized SWOT strings for quadrant labels
* @param mdStrings - Localized multi-dimensional SWOT strings
* @returns HTML string for all dimension sections
*/
function buildDimensionsDrillDown(
dimensions: readonly SwotDimension[],
swotStrings: SwotStrings,
mdStrings: MultiDimensionalSwotStrings
): string {
Iif (dimensions.length === 0) return '';
const sectionsHtml = dimensions
.map((dim) => {
const label = getDimensionLabel(dim.name, mdStrings);
const miniGrid = buildSwotMiniGrid(dim, swotStrings);
return `<details class="swot-dimension swot-dimension-${escapeHTML(dim.name)}">
<summary class="swot-dimension-summary">${escapeHTML(label)}</summary>
<div class="swot-dimension-content">${miniGrid}</div>
</details>`;
})
.join('\n ');
return `<div class="swot-dimensions" role="region" aria-label="${escapeHTML(mdStrings.dimensionsLabel)}">
<h3 class="swot-section-subheading">${escapeHTML(mdStrings.dimensionsLabel)}</h3>
${sectionsHtml}
</div>`;
}
/**
* Map a stakeholder type to its localized label.
*
* @param type - Stakeholder type identifier
* @param mdStrings - Localized multi-dimensional SWOT strings
* @returns Localized stakeholder label
*/
function getStakeholderLabel(
type: StakeholderType,
mdStrings: MultiDimensionalSwotStrings
): string {
const map: Record<StakeholderType, string> = {
citizen: mdStrings.stakeholderCitizen,
industry: mdStrings.stakeholderIndustry,
ngo: mdStrings.stakeholderNgo,
mep: mdStrings.stakeholderMep,
government: mdStrings.stakeholderGovernment,
media: mdStrings.stakeholderMedia,
};
return map[type];
}
/**
* Build the stakeholder perspectives section.
* Each stakeholder view is a collapsible `<details>` widget.
*
* @param stakeholderViews - Map of stakeholder type to SWOT analysis
* @param swotStrings - Localized SWOT strings for quadrant labels
* @param mdStrings - Localized multi-dimensional SWOT strings
* @returns HTML string for the stakeholder section, or empty string if none
*/
function buildStakeholderSection(
stakeholderViews: Readonly<Partial<Record<StakeholderType, SwotAnalysis>>> | undefined,
swotStrings: SwotStrings,
mdStrings: MultiDimensionalSwotStrings
): string {
if (!stakeholderViews) return '';
const entries = Object.entries(stakeholderViews) as [StakeholderType, SwotAnalysis][];
const validEntries = entries.filter(([, view]) => {
const total =
view.strengths.length +
view.weaknesses.length +
view.opportunities.length +
view.threats.length;
return total > 0;
});
if (validEntries.length === 0) return '';
const viewsHtml = validEntries
.map(([type, view]) => {
const label = getStakeholderLabel(type, mdStrings);
const miniGrid = buildSwotMiniGrid(view, swotStrings);
return `<details class="swot-stakeholder swot-stakeholder-${escapeHTML(type)}">
<summary class="swot-stakeholder-summary">${escapeHTML(label)}</summary>
<div class="swot-stakeholder-content">${miniGrid}</div>
</details>`;
})
.join('\n ');
return `<div class="swot-stakeholders" role="region" aria-label="${escapeHTML(mdStrings.stakeholderPerspectivesLabel)}">
<h3 class="swot-section-subheading">${escapeHTML(mdStrings.stakeholderPerspectivesLabel)}</h3>
${viewsHtml}
</div>`;
}
/**
* Build the temporal analysis section.
* Short-term and medium-term (and optionally long-term) periods are
* each rendered as a collapsible `<details>` widget.
*
* @param temporal - Temporal SWOT assessment data
* @param swotStrings - Localized SWOT strings for quadrant labels
* @param mdStrings - Localized multi-dimensional SWOT strings
* @returns HTML string for the temporal section, or empty string if not provided
*/
function buildTemporalSection(
temporal: TemporalSwotAssessment | undefined,
swotStrings: SwotStrings,
mdStrings: MultiDimensionalSwotStrings
): string {
if (!temporal) return '';
const renderPeriod = (analysis: SwotAnalysis | undefined, label: string): string => {
if (!analysis) return '';
const total =
analysis.strengths.length +
analysis.weaknesses.length +
analysis.opportunities.length +
analysis.threats.length;
if (total === 0) return '';
const miniGrid = buildSwotMiniGrid(analysis, swotStrings);
return `<details class="swot-temporal-period">
<summary class="swot-temporal-summary">${escapeHTML(label)}</summary>
<div class="swot-temporal-content">${miniGrid}</div>
</details>`;
};
const shortHtml = renderPeriod(temporal.shortTerm, mdStrings.shortTermLabel);
const mediumHtml = renderPeriod(temporal.mediumTerm, mdStrings.mediumTermLabel);
const longHtml = renderPeriod(temporal.longTerm, mdStrings.longTermLabel);
const combinedHtml = shortHtml + mediumHtml + longHtml;
Iif (!combinedHtml) return '';
return `<div class="swot-temporal" role="region" aria-label="${escapeHTML(mdStrings.temporalAnalysisLabel)}">
<h3 class="swot-section-subheading">${escapeHTML(mdStrings.temporalAnalysisLabel)}</h3>
${combinedHtml}
</div>`;
}
/**
* Build the cross-references section linking SWOT items to EP documents.
*
* @param crossReferences - Array of cross-reference data
* @param mdStrings - Localized multi-dimensional SWOT strings
* @returns HTML string for the cross-references section, or empty string if empty
*/
function buildCrossReferencesSection(
crossReferences: readonly SwotCrossReference[] | undefined,
mdStrings: MultiDimensionalSwotStrings
): string {
if (!crossReferences || crossReferences.length === 0) return '';
const refsHtml = crossReferences
.map((ref) => {
const urlPart =
ref.url && isSafeURL(ref.url)
? `<a href="${escapeHTML(ref.url)}" class="swot-ref-link" rel="noopener noreferrer">${escapeHTML(ref.documentId)}</a>`
: `<span class="swot-ref-id">${escapeHTML(ref.documentId)}</span>`;
return `<li class="swot-ref-item">
<span class="swot-ref-evidence">${escapeHTML(mdStrings.evidenceLabel)}: ${escapeHTML(ref.itemText)}</span>
— ${urlPart}: <cite class="swot-ref-title">${escapeHTML(ref.documentTitle)}</cite>
</li>`;
})
.join('\n ');
return `<div class="swot-cross-references" role="region" aria-label="${escapeHTML(mdStrings.crossReferencesLabel)}">
<h3 class="swot-section-subheading">${escapeHTML(mdStrings.crossReferencesLabel)}</h3>
<ul class="swot-ref-list">${refsHtml}</ul>
</div>`;
}
/**
* Aggregate all dimension items into a single SwotAnalysis for the primary view.
*
* @param dimensions - Array of dimension data
* @returns Aggregated SwotAnalysis or null if all dimensions are empty
*/
function aggregateDimensions(dimensions: readonly SwotDimension[]): SwotAnalysis | null {
const strengths = dimensions.flatMap((d) => d.strengths);
const weaknesses = dimensions.flatMap((d) => d.weaknesses);
const opportunities = dimensions.flatMap((d) => d.opportunities);
const threats = dimensions.flatMap((d) => d.threats);
const total = strengths.length + weaknesses.length + opportunities.length + threats.length;
if (total === 0) return null;
return { strengths, weaknesses, opportunities, threats };
}
// ─── Multi-dimensional SWOT main builder ─────────────────────────────────────
/**
* Build a complete multi-dimensional SWOT analysis section HTML.
*
* Renders a primary aggregated 4-quadrant SWOT matrix followed by optional:
* - **Dimension drill-downs** (Political, Economic, Social, Legal, Geopolitical)
* as CSS-only `<details>` disclosure widgets
* - **Stakeholder perspectives** (Citizens, Industry, NGOs, MEPs, etc.)
* as collapsible `<details>` widgets
* - **Temporal analysis** (short-term, medium-term, long-term)
* as collapsible `<details>` widgets
* - **Cross-references** linking SWOT items to specific EP documents/votes
*
* All interactivity is CSS-only (no JavaScript), ensuring compatibility
* with strict CSP policies and WCAG 2.1 AA accessibility requirements.
*
* Returns an empty string if the analysis has no dimensions or all
* dimensions are empty.
*
* @param analysis - Multi-dimensional SWOT data (null/undefined returns empty string)
* @param lang - BCP 47 language code for localized headings (defaults to "en")
* @param heading - Optional custom section heading override
* @returns HTML section string or empty string
*/
export function buildMultiDimensionalSwotSection(
analysis: MultiDimensionalSwot | null | undefined,
lang: string = 'en',
heading?: string
): string {
if (!analysis) return '';
if (analysis.dimensions.length === 0) return '';
const swotStrings: SwotStrings = getLocalizedString(SWOT_STRINGS, lang);
const mdStrings: MultiDimensionalSwotStrings = getLocalizedString(
MULTI_DIMENSIONAL_SWOT_STRINGS,
lang
);
const sectionHeading = heading ?? analysis.title ?? swotStrings.sectionHeading;
const aggregated = aggregateDimensions(analysis.dimensions);
if (!aggregated) return '';
const primaryMatrix = buildSwotMiniGrid(aggregated, swotStrings);
const dimensionsHtml = buildDimensionsDrillDown(analysis.dimensions, swotStrings, mdStrings);
const stakeholderHtml = buildStakeholderSection(
analysis.stakeholderViews,
swotStrings,
mdStrings
);
const temporalHtml = buildTemporalSection(analysis.temporal, swotStrings, mdStrings);
const refsHtml = buildCrossReferencesSection(analysis.crossReferences, mdStrings);
return `
<section class="swot-analysis swot-multidimensional" role="region" aria-label="${escapeHTML(sectionHeading)}">
<h2>${escapeHTML(sectionHeading)}</h2>
<div class="swot-matrix">
<div class="swot-axis-labels">
<span class="swot-axis-internal">${escapeHTML(swotStrings.internalLabel)}</span>
<span class="swot-axis-external">${escapeHTML(swotStrings.externalLabel)}</span>
</div>
${primaryMatrix}
</div>
${dimensionsHtml}
${stakeholderHtml}
${temporalHtml}
${refsHtml}
</section>`;
}
|