function updateHtmlHrefs()

in build/normalize-docs-files.php [26:110]


function updateHtmlHrefs($directory) {
    $htmlFiles = glob($directory . '/*.html');
    foreach ($htmlFiles as $file) {
        $doc = new DOMDocument();
        @$doc->loadHTMLFile($file); // Suppress warnings for invalid HTML

        // Remove <base> tags
        while (($baseTags = $doc->getElementsByTagName('base')) && $baseTags->length) {
            $baseTag = $baseTags->item(0);
            $baseTag->parentNode->removeChild($baseTag);
        }

        $links = $doc->getElementsByTagName('a');

        foreach ($links as $link) {
            if ($link->hasAttribute('href')) {
                $href = $link->getAttribute('href');
                // Replace and capitalize as needed
                $href = preg_replace_callback(
                    '/(namespaces|classes|packages)\/([a-zA-Z])/',
                    function ($matches) {
                        // Capitalize the first letter after the prefix and replace / with -
                        if ($matches[1] === 'classes') {
                            $prefix = 'class'; // Directly set to 'class' for 'classes'
                        } else {
                            // For 'namespaces' and 'packages', simply remove the last character
                            $prefix = substr($matches[1], 0, -1);
                        }

                        return $prefix . '-' . strtoupper($matches[2]);
                    },
                    $href
                );
                // Replace dashes with dots after the specific prefixes
                $href = preg_replace_callback(
                    '/(namespace-|class-|package-)([\w-]+)\.html/',
                    function ($matches) {
                        // Replace all dashes in the suffix part with dots
                        $suffix = str_replace("-", ".", $matches[2]);
                        return $matches[1] . $suffix . '.html';
                    },
                    $href
                );
                $link->setAttribute('href', $href);
            }
        }

        // Remove <aside> tags with class "phpdocumentor-element-found-in"
        $asides = $doc->getElementsByTagName('aside');
        $asidesToRemove = [];

        foreach ($asides as $aside) {
            if ($aside->getAttribute('class') === 'phpdocumentor-element-found-in') {
                $asidesToRemove[] = $aside;
            }
        }

        // Remove the selected <aside> tags
        foreach ($asidesToRemove as $asideToRemove) {
            $asideToRemove->parentNode->removeChild($asideToRemove);
        }

        $divs = $doc->getElementsByTagName('div');

        foreach ($divs as $div) {
            // Check if the div has the class 'phpdocumentor-modal-content'
            if ($div->getAttribute('class') === 'phpdocumentor-modal-content') {
                $preTags = $div->getElementsByTagName('pre');

                // Check each pre tag in the current div
                foreach ($preTags as $pre) {
                    if ($pre->hasAttribute('data-src') && strpos($pre->getAttribute('data-src'), 'files/') === 0) {
                        // If condition is met, remove the div from its parent
                        $div->parentNode->removeChild($div);
                        break; // Stop checking other pre tags if one meets the criteria
                    }
                }
            }
        }

        // Save the changes
        $doc->saveHTMLFile($file);
        echo "Updated hrefs in $file\n";
    }
}