feat: add a power html rewriter to customized my old pages

This commit is contained in:
Hugo Poissonnet 2022-11-06 11:42:22 +01:00
parent dd7f80ee5a
commit 598d28551d
3 changed files with 59 additions and 12 deletions

View File

@ -0,0 +1,8 @@
<div id="hosted-on" class="widget widget_text">
<h5>Auto-hébergé sur mon NAS</h5>
<div class="textwidget">
<div style="text-align: center">
Ce blog est hébergé à la maison avec de l'Internet fournit par <a href="https://www.franciliens.net">Franciliens.net</a>
</div>
</div>
</div>

View File

@ -0,0 +1,4 @@
<div style="background-color: orange;border-radius: 6px;font-weight: bold;margin-bottom: 10px;" class="container">
Ceci est la version legacy de mon blog si vous voulez voir les nouveaux articles et et savoir pourquoi c'est par
<a href="/new">ICI</a>
</div>

View File

@ -30,7 +30,7 @@
* @link https://archivarix.com
*/
@ini_set( 'display_errors', 1 );
@ini_set( 'display_errors', 0 );
/**
* Switch mode to integrate with 3rd party CMS.
@ -111,6 +111,24 @@ const ARCHIVARIX_INCLUDE_CUSTOM = array(
'REGEX' => 0,
'POSITION' => -1,
],
// remove twitter widget in the right column on all the pages
[
'OPERATION' => 'REMOVE',
'XPATH' => '//*[@id="twitter-2"]',
],
[
'OPERATION' => 'REPLACE',
'XPATH' => '//*[@id="text-6"]',
'FILE' => 'hosted_by_franciliens.html',
],
[
'OPERATION' => 'APPEND',
'XPATH' => '//nav[contains(@class, "navbar")]',
'FILE' => 'warning_new_blog.php',
],
);
/**
@ -368,7 +386,7 @@ function prepareContent( $file, $sourcePath )
$content = file_get_contents( $file );
foreach ( $LOADER['ARCHIVARIX_INCLUDE_CUSTOM'] as $includeCustom ) {
if ( $includeCustom['FILE'] ) {
if ( $includeCustom['FILE'] && $includeCustom['KEYPHRASE'] ) {
global $includeRule;
$includeRule = $includeCustom;
ob_start();
@ -391,18 +409,35 @@ function prepareContent( $file, $sourcePath )
}
$content = preg_replace( '~' . $includeCustom['KEYPHRASE'] . '~is', $includedContent, $content, $includeCustom['LIMIT'] );
}
}
} else if ( $includeCustom['OPERATION'] && $includeCustom['XPATH'] ) {
$doc = new DOMDocument();
$doc->loadHTML($content);
$xpath = new DOMXpath($doc);
$hostedByElements = $xpath->query("//*[@id='text-6']");
$hostedByElements->item(0)?->remove();
$twitterWidgetElements = $xpath->query("//*[@id='twitter-2']");
$twitterWidgetElements->item(0)?->remove();
//$content = '';
$elementsToChange = $xpath->query($includeCustom['XPATH']);
foreach ( $elementsToChange as $elementToChange ) {
switch ( $includeCustom['OPERATION'] ) {
case 'APPEND':
case 'REPLACE' :
$fragment = $doc->createDocumentFragment();
ob_start();
require($sourcePath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . $includeCustom['FILE']);
$fragment->appendXML(ob_get_clean());
if ($includeCustom['OPERATION'] === 'APPEND') {
$elementToChange->append($fragment->firstChild);
} else {
$elementToChange->replaceWith($fragment->firstChild);
}
break;
case 'REMOVE' :
$elementToChange->remove();
break;
}
}
$content = $doc->saveHTML();
}
}
if ( function_exists( 'mb_strlen' ) ) {
header( 'Content-Length: ' . mb_strlen( $content, '8bit' ), true );