1. <?php
  2. /**
  3. * Syncs resources of HTML documents with manual overrides.
  4. *
  5. * This script provides a temporary solution for syncing resources (CSS, JS, images)
  6. * between HTML documents, allowing for manual overrides.
  7. *
  8. * Usage:
  9. * - Pass paths to the source and destination HTML files as arguments.
  10. * - Use a configuration file (e.g., 'override.php') to define manual overrides.
  11. */
  12. // Configuration file path (optional)
  13. $overrideFile = 'override.php';
  14. // Function to read overrides from the configuration file
  15. function loadOverrides($file) {
  16. $overrides = [];
  17. if (file_exists($file)) {
  18. include $file; // Include the override file
  19. if (isset($overrides)) {
  20. return $overrides;
  21. }
  22. }
  23. return $overrides;
  24. }
  25. // Load overrides
  26. $overrides = loadOverrides($overrideFile);
  27. // Function to sync resources
  28. function syncResources($sourceHtml, $destHtml, $overrides) {
  29. // Extract resources from source HTML
  30. $sourceResources = extractResources($sourceHtml);
  31. // Extract resources from destination HTML
  32. $destResources = extractResources($destHtml);
  33. // Combine resources
  34. $combinedResources = array_merge($sourceResources, $destResources);
  35. // Apply overrides
  36. if ($overrides) {
  37. foreach ($overrides as $resource => $overrideValue) {
  38. if (in_array($resource, $combinedResources)) {
  39. $combinedResources[$resource] = $overrideValue;
  40. }
  41. }
  42. }
  43. // Write the updated resources to the destination HTML
  44. writeResourcesToHtml($destHtml, $combinedResources);
  45. }
  46. /**
  47. * Extracts resources (CSS, JS, images) from an HTML file.
  48. *
  49. * @param string $html The HTML content.
  50. * @return array An array of resource URLs.
  51. */
  52. function extractResources($html) {
  53. $resources = [];
  54. // Regex to find <link> tags for CSS
  55. $cssRegex = '/<link\s+rel="stylesheet"\s+href="([^"]+)"/i';
  56. preg_match_all($cssRegex, $html, $matches, PREG_SET_ORDER);
  57. if ($matches) {
  58. foreach ($matches as $match) {
  59. $resources[] = $match[1];
  60. }
  61. }
  62. // Regex to find <script> tags for JavaScript
  63. $jsRegex = '/<script\s+src="([^"]+)"/i';
  64. preg_match_all($jsRegex, $html, $matches, PREG_SET_ORDER);
  65. if ($matches) {
  66. foreach ($matches as $match) {
  67. $resources[] = $match[1];
  68. }
  69. }
  70. // Regex to find <img> tags for images
  71. $imgRegex = '/<img\s+src="([^"]+)"/i';
  72. preg_match_all($imgRegex, $html, $matches, PREG_SET_ORDER);
  73. if ($matches) {
  74. foreach ($matches as $match) {
  75. $resources[] = $match[1];
  76. }
  77. }
  78. return $resources;
  79. }
  80. /**
  81. * Writes the resources to the destination HTML file.
  82. *
  83. * @param string $html The HTML content.
  84. * @param array $resources An array of resource URLs.
  85. */
  86. function writeResourcesToHtml($html, $resources) {
  87. $modifiedHtml = $html;
  88. foreach ($resources as $resource) {
  89. // Add a placeholder for the resource (e.g., a comment)
  90. $modifiedHtml .= "\n<!-- Resource: " . $resource . " -->\n";
  91. }
  92. //Write the modified HTML back to the destination file
  93. file_put_contents($html, $modifiedHtml);
  94. }
  95. // --- Main execution ---
  96. if (count($argv) !== 3) {
  97. echo "Usage: php sync.php <source_html_file> <destination_html_file>\n";
  98. exit(1);
  99. }
  100. $sourceHtml = $argv[1];
  101. $destHtml = $argv[2];
  102. syncResources($sourceHtml, $destHtml, $overrides);
  103. echo "Resources synced.\n";
  104. ?>

Add your comment