)~s', $template, $match)) { fwrite(STDERR, "The template does not contain the approved Progress Status Record CSS.\n"); exit(1); } // The CSS must work for every record number, not just record 1. $layoutCss = str_replace( '#progress-status-record-1-of-13', 'h1[id^="progress-status-record-"]', $match[1] ); /** @return string */ function approvalCard(string $id, string $title, string $paragraph): string { $lines = preg_split('~\s*~i', trim($paragraph)) ?: []; $items = []; foreach ($lines as $line) { $line = trim($line); if ($line === '') { continue; } if (preg_match('~^(Signature|Date):\s*_*\s*$~i', strip_tags($line), $label)) { $items[] = '

' . htmlspecialchars($label[1], ENT_QUOTES, 'UTF-8') . ':

'; } else { $items[] = '

' . $line . '

'; } } return '

' . $title . '

' . implode("\n", $items) . '
'; } $files = glob($directory . '/*.html') ?: []; $formatted = 0; $skipped = 0; foreach ($files as $path) { $html = file_get_contents($path); if ($html === false) { fwrite(STDERR, "Could not read: $path\n"); continue; } if (str_contains($html, '/* BRN Progress Status Record print layout')) { $skipped++; continue; } $html = preg_replace('~~', "\n", $html, 1, $headCount); if ($headCount !== 1) { fwrite(STDERR, "Skipped malformed HTML: " . basename($path) . "\n"); continue; } $header = '
BRN corporate identity
Progress Status Record
'; $html = preg_replace( '~(

.*?

\s*)~s', $header . "\n" . '$1
', $html, 1, $titleCount ); if ($titleCount !== 1) { fwrite(STDERR, "Skipped unsupported record structure: " . basename($path) . "\n"); continue; } if (preg_match('~

8\. Approval

(.*?)(?=)~s', $html, $approvalMatch)) { preg_match_all('~

(.*?)

\s*

(.*?)

~s', $approvalMatch[1], $blocks, PREG_SET_ORDER); if (count($blocks) === 3) { $cards = []; foreach ($blocks as $block) { $cards[] = approvalCard($block[1], $block[2], $block[3]); } $approval = '

8. Approval

' . implode("\n", $cards) . '
'; $html = str_replace($approvalMatch[0], $approval, $html); } } $date = 'Progress Status Record'; if (preg_match('~\s*~s', $html, $dateMatch)) { $date = trim(strip_tags($dateMatch[1])); } $footer = ''; $html = str_replace('', $footer . "\n", $html); if (file_put_contents($path, $html) === false) { fwrite(STDERR, "Could not write: $path\n"); continue; } $formatted++; } printf("Formatted: %d; already formatted: %d\n", $formatted, $skipped);
Report date(.*?)