<?php
/**
* Syncs resources of HTML documents with manual overrides.
*
* This script provides a temporary solution for syncing resources (CSS, JS, images)
* between HTML documents, allowing for manual overrides.
*
* Usage:
* - Pass paths to the source and destination HTML files as arguments.
* - Use a configuration file (e.g., 'override.php') to define manual overrides.
*/
// Configuration file path (optional)
$overrideFile = 'override.php';
// Function to read overrides from the configuration file
function loadOverrides($file) {
$overrides = [];
if (file_exists($file)) {
include $file; // Include the override file
if (isset($overrides)) {
return $overrides;
}
}
return $overrides;
}
// Load overrides
$overrides = loadOverrides($overrideFile);
// Function to sync resources
function syncResources($sourceHtml, $destHtml, $overrides) {
// Extract resources from source HTML
$sourceResources = extractResources($sourceHtml);
// Extract resources from destination HTML
$destResources = extractResources($destHtml);
// Combine resources
$combinedResources = array_merge($sourceResources, $destResources);
// Apply overrides
if ($overrides) {
foreach ($overrides as $resource => $overrideValue) {
if (in_array($resource, $combinedResources)) {
$combinedResources[$resource] = $overrideValue;
}
}
}
// Write the updated resources to the destination HTML
writeResourcesToHtml($destHtml, $combinedResources);
}
/**
* Extracts resources (CSS, JS, images) from an HTML file.
*
* @param string $html The HTML content.
* @return array An array of resource URLs.
*/
function extractResources($html) {
$resources = [];
// Regex to find <link> tags for CSS
$cssRegex = '/<link\s+rel="stylesheet"\s+href="([^"]+)"/i';
preg_match_all($cssRegex, $html, $matches, PREG_SET_ORDER);
if ($matches) {
foreach ($matches as $match) {
$resources[] = $match[1];
}
}
// Regex to find <script> tags for JavaScript
$jsRegex = '/<script\s+src="([^"]+)"/i';
preg_match_all($jsRegex, $html, $matches, PREG_SET_ORDER);
if ($matches) {
foreach ($matches as $match) {
$resources[] = $match[1];
}
}
// Regex to find <img> tags for images
$imgRegex = '/<img\s+src="([^"]+)"/i';
preg_match_all($imgRegex, $html, $matches, PREG_SET_ORDER);
if ($matches) {
foreach ($matches as $match) {
$resources[] = $match[1];
}
}
return $resources;
}
/**
* Writes the resources to the destination HTML file.
*
* @param string $html The HTML content.
* @param array $resources An array of resource URLs.
*/
function writeResourcesToHtml($html, $resources) {
$modifiedHtml = $html;
foreach ($resources as $resource) {
// Add a placeholder for the resource (e.g., a comment)
$modifiedHtml .= "\n<!-- Resource: " . $resource . " -->\n";
}
//Write the modified HTML back to the destination file
file_put_contents($html, $modifiedHtml);
}
// --- Main execution ---
if (count($argv) !== 3) {
echo "Usage: php sync.php <source_html_file> <destination_html_file>\n";
exit(1);
}
$sourceHtml = $argv[1];
$destHtml = $argv[2];
syncResources($sourceHtml, $destHtml, $overrides);
echo "Resources synced.\n";
?>
Add your comment