Automad = $Automad; $this->InPage = $InPage; } /** * Run all required post-process steps. * * @param string $output * @return string the final output */ public function process(string $output): string { $output = $this->createExtensionAssetTags($output); $output = $this->addMetaTags($output); $output = $this->obfuscateEmails($output); $output = $this->resizeImages($output); $output = Blocks::injectAssets($output); $output = $this->addCacheBustingTimestamps($output); $output = URLProcessor::resolveUrls($output, 'absoluteUrlToRoot'); $output = $this->InPage->createUI($output); return $output; } /** * Find all locally hosted assests and append a timestamp in order to avoid serving outdated files. * * @param string $str * @return string the processed output */ private function addCacheBustingTimestamps(string $str): string { $extensions = implode('|', FileUtils::allowedFileTypes()); return preg_replace_callback( '#(?<=")/[/\w\-\.]+\.(?:' . $extensions . ')(?=")#is', function ($matches) { $file = AM_BASE_DIR . $matches[0]; if (strpos($file, AM_DIR_CACHE . '/') !== false || !is_readable($file)) { return $matches[0]; } return $matches[0] . '?m=' . filemtime($file); }, $str ); } /** * Add meta tags to the head of $str. * * @param string $str * @return string The meta tag */ private function addMetaTags(string $str): string { $meta = ''; if (AM_FEED_ENABLED) { $sitename = $this->Automad->Shared->get(Fields::SITENAME); $meta .= ''; } return str_replace('
', '' . $meta, $str); } /** * Create the HTML tags for each file in the asset collection and prepend them to the closing tag. * * @param string $str * @return string The processed string */ private function createExtensionAssetTags(string $str): string { $assets = AssetCollection::get(); Debug::log($assets, 'Assets'); $html = ''; if (isset($assets['.css'])) { foreach ($assets['.css'] as $file) { $html .= ''; Debug::log($file, 'Created tag for'); } } if (isset($assets['.js'])) { foreach ($assets['.js'] as $file) { $html .= ''; Debug::log($file, 'Created tag for'); } } // Prepend all items ($html) to the closing tag. return str_replace('', $html . '', $str); } /** * Obfuscate all stand-alone eMail addresses matched in $str. * Addresses in links are ignored. * * @param string $str * @return string The processed string */ private function obfuscateEmails(string $str): string { $regexEmail = '[\w\.\+\-]+@[\w\-\.]+\.[a-zA-Z]{2,}'; // The following regex matches all email links or just an email address. // That way it is possible to separate email addresses // within tags from stand-alone ones. $regex = '/(]*href="mailto.+?<\/a>|(?P