test inlining
This commit is contained in:
parent
d53d0b1f54
commit
f67c6a6186
45
lib/inliner.php
Normal file
45
lib/inliner.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
function inlineLocalStylesFromHref($inputString) {
|
||||
$basePath = '/path/to/your/local/files'; // Hardcoded base path
|
||||
|
||||
// Define the regular expression pattern to match <link> tags with href attribute for CSS files
|
||||
$pattern = '/<link.*?href=["\']\/assets\/(.*?)["\'].*?rel=["\']stylesheet["\'].*?>/i';
|
||||
|
||||
// Use preg_replace_callback to replace matched link tags with inline styles
|
||||
$outputString = preg_replace_callback($pattern, function($match) use ($basePath) {
|
||||
// Extract the href attribute value
|
||||
$href = $match[1];
|
||||
|
||||
// Get the content of the local CSS file
|
||||
$cssContent = file_get_contents($basePath . '/assets/' . $href);
|
||||
|
||||
// Create an inline style tag with the CSS content
|
||||
return "<style>\n{$cssContent}\n</style>";
|
||||
}, $inputString);
|
||||
|
||||
return $outputString;
|
||||
}
|
||||
|
||||
function inlineScriptFromSrc($inputString) {
|
||||
$basePath = '/path/to/your/local/files'; // Hardcoded base path
|
||||
|
||||
// Define the regular expression pattern to match <script> tags with src attribute
|
||||
$pattern = '/<script.*?src=["\']\/assets\/(.*?)["\'].*?>\s*<\/script>/i';
|
||||
|
||||
// Use preg_replace_callback to replace matched script tags with inline script
|
||||
$outputString = preg_replace_callback($pattern, function($match) use ($basePath) {
|
||||
// Extract the src attribute value
|
||||
$src = $match[1];
|
||||
|
||||
// Get the content of the JavaScript file
|
||||
$jsContent = file_get_contents($basePath . '/assets/' . $src);
|
||||
|
||||
// Escape the JavaScript content for inline use
|
||||
$escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8');
|
||||
|
||||
// Create an inline script with the escaped content
|
||||
return "<script>\n{$escapedJsContent}\n</script>";
|
||||
}, $inputString);
|
||||
|
||||
return $outputString;
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
require_once "lib/inliner.php";
|
||||
function renderDynamicPage($page_file): array
|
||||
{
|
||||
return require $page_file;
|
||||
@ -127,5 +128,7 @@ function getPage($page_name = null): array|false|string
|
||||
$out = str_replace("__TEMPLATE__NAV__", $nav, $out);
|
||||
$out = str_replace("__TEMPLATE__PAGE__", $page, $out);
|
||||
$out = str_replace("__TEMPLATE__DYNASTYLE__", $dynamic_style, $out);
|
||||
$out = inlineLocalStylesFromHref($out);
|
||||
$out = inlineScriptFromSrc($out);
|
||||
return str_replace("__TEMPLATE_PAGE_TITLE__", $page_title, $out);
|
||||
}
|
Loading…
Reference in New Issue
Block a user