Files
wms-app/scripts/format_progress_status_html.php
T
2026-08-17 13:12:07 +07:00

118 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
/*
* Applies the approved print layout from the first formatted Progress Status
* Record to every generated Progress Status Record HTML file. Markdown source
* files are deliberately not read or changed.
*
* Usage:
* php scripts/format_progress_status_html.php
*/
$root = dirname(__DIR__);
$directory = $root . '/sdlc/1-PM Process (10 Work Product)/3.Progess Status Record';
$templatePath = $directory . '/200-WMS-26-001-00 Progress Status Record 25690123 V1.0 ApS.html';
if (!is_file($templatePath)) {
fwrite(STDERR, "Template Progress Status Record HTML was not found.\n");
exit(1);
}
$template = file_get_contents($templatePath);
if ($template === false || !preg_match('~(/\* BRN Progress Status Record print layout.*?</style>)~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('~<br\s*/?>\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[] = '<p class="sign-row"><strong>' . htmlspecialchars($label[1], ENT_QUOTES, 'UTF-8') . ':</strong><span class="write-line"></span></p>';
} else {
$items[] = '<p>' . $line . '</p>';
}
}
return '<div class="approval-card"><h3 id="' . $id . '">' . $title . '</h3>' . implode("\n", $items) . '</div>';
}
$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('~</head>~', "<style>\n$layoutCss\n</style>\n</head>", $html, 1, $headCount);
if ($headCount !== 1) {
fwrite(STDERR, "Skipped malformed HTML: " . basename($path) . "\n");
continue;
}
$header = '<header class="psr-letterhead"><img class="document-header-image" src="../../../app/assets/images/brn-document-header-left.png" alt="BRN corporate identity"><div class="psr-title-band">Progress Status Record</div></header>';
$html = preg_replace(
'~(<h1 id="progress-status-record-[^"]+">.*?</h1>\s*)<table>~s',
$header . "\n" . '$1<table class="document-control">',
$html,
1,
$titleCount
);
if ($titleCount !== 1) {
fwrite(STDERR, "Skipped unsupported record structure: " . basename($path) . "\n");
continue;
}
if (preg_match('~<h2 id="8-approval">8\. Approval</h2>(.*?)(?=</body>)~s', $html, $approvalMatch)) {
preg_match_all('~<h3 id="([^"]+)">(.*?)</h3>\s*<p>(.*?)</p>~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 = '<section class="approval-page"><h2 id="8-approval">8. Approval</h2><div class="approval-grid">'
. implode("\n", $cards)
. '</div></section>';
$html = str_replace($approvalMatch[0], $approval, $html);
}
}
$date = 'Progress Status Record';
if (preg_match('~<td>Report date</td>\s*<td>(.*?)</td>~s', $html, $dateMatch)) {
$date = trim(strip_tags($dateMatch[1]));
}
$footer = '<footer class="psr-footer">200-WMS-26-001-00 · BRN WMS · Progress Status Record · '
. htmlspecialchars($date, ENT_QUOTES, 'UTF-8') . ' · V1.0 Final</footer>';
$html = str_replace('</body>', $footer . "\n</body>", $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);