paste.thms.uk/weblog-pre-process.php 1 year ago

shell_exec('git config core.quotepath off');
    echo 'git diff --name-only '.$argv[1].' ' . $argv[2];
    $diff = shell_exec('git diff --name-only '.$argv[1].' ' . $argv[2]);
    if($diff == '') {
        echo "\n*** No changes detected. Are you running the latest version of the action? See https://github.com/neatnik/weblog.lol for details.";
        exit;
    }

    $diff = explode("\n", $diff);

    // get MD5 hashes of all static files
    $cssFiles = getHashes('css', ['css']);
    $jsFiles = getHashes('js', ['js']);
    $imageFiles = getHashes('images', ['jpg','jpeg','gif','png','svg']);

    $toReplace = array_merge(
        $cssFiles,
        $jsFiles,
        $imageFiles,
    );

    // make sure we scan all templates for references to static files, regardless of whether these have changed or not
    foreach($diff as $file) {
        if(array_key_exists($file,$cssFiles) || array_key_exists($file, $jsFiles)) {
            $diff[] = 'configuration/template.html';
            $templates = scandir('weblog/templates'); 
            foreach($templates as $template) {
                if(in_array(pathinfo('weblog/templates/' . $template, PATHINFO_EXTENSION), ['md','markdown','html'])) {
                    $diff[] = 'weblog/templates/' . $template;
                }
            }
        }
    }
    $diff = array_unique($diff);

    // Replace references to these static files in any .md and .html files
    foreach($diff as $file) {
            
        if($file == '') {
            continue;
        }

        $fileEnding = pathinfo($file, PATHINFO_EXTENSION);
        if(!in_array($fileEnding, ['md','markdown','html'])) {
            echo PHP_EOL . 'Skipping file: ' . $file;
            continue;
        }

        echo "\n*** Examining file: $file...";
        
        // Firstly: get the relative directory from the file we are looking at, to the root directory
        $structure = explode('/', $file);
        foreach($structure as $i => $folder) {
            if($i !== count($structure) - 1) {
                $structure[$i] = '..';
            }
        }
        unset($structure[count($structure) - 1]);
        $relativeDir = join('/', $structure) . '/';

        // now replace any relative references to the static files, found in HTML files, as well as in markdown links
        $fileContents = file_get_contents($file);
        foreach($toReplace as $cssFile => $hash) {
            $pattern = '#(src=|href=)([\'"])' . preg_quote($relativeDir) . preg_quote($cssFile) . '([\'"])#';
            $replacement = '$1$2https://media.thms.uk/' . $cssFile . '?v=' . $hash . '$3';

            echo "\n*** replacing " . $pattern . ' with ' . $replacement;

            $fileContents = preg_replace($pattern, $replacement, $fileContents);
        }

        // finally, replace relative links to embedded links in markdown files
        if(pathinfo($file, PATHINFO_EXTENSION) === 'md') {
            foreach($imageFiles as $imageFile => $hash) {
                $pattern = '|(!\[[^]]+])\(' . preg_quote($relativeDir) . preg_quote($imageFile) . '\)|';
                $replacement = '$1(https://media.thms.uk/' . $imageFile . '?v=' . $hash . ')';

                echo "\n*** replacing " . $pattern . ' with ' . $replacement;

                $fileContents = preg_replace($pattern, $replacement, $fileContents);
            }
        }

        file_put_contents($file, $fileContents);    
    }

    // delete unchanged static files, so that we don't upload them to S3 every time
    foreach(['css','js','images'] as $directory) {
        $o_dir = new RecursiveDirectoryIterator($directory);
        $o_iter = new RecursiveIteratorIterator($o_dir);
        foreach($o_iter as $file) {
            echo PHP_EOL . $file;
            if(in_array($file->getExtension(), ['jpg','jpeg','gif','png','svg', 'css','js']) && !in_array($file->getPathName(), $diff)) {
                echo PHP_EOL . '*** Deleting unchanged ' . $file->getPathName();
                unlink($file->getPathName());
            }
        }
    }

    function getHashes(string $directory, array $types): array
    {
        $return = [];
        $o_dir = new RecursiveDirectoryIterator($directory);
        $o_iter = new RecursiveIteratorIterator($o_dir);
        foreach($o_iter as $file) {
            if(in_array($file->getExtension(), $types)) {
                $return[$file->getPathName()] = hash_file('md5', $file->getPathName());
            }
        }
        return $return;
    }