-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
250 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
<?php | ||
$page = $_GET["page"]; | ||
$currentpage = "wiki-".$page.".php"; | ||
include("header.php"); | ||
?> | ||
|
||
<style> | ||
.mw-parser-output { | ||
margin-top: 3em; | ||
} | ||
|
||
.mw-parser-output a.external { | ||
background-position: center right; | ||
background-repeat: no-repeat; | ||
background-size: 0.857em; | ||
} | ||
|
||
.mw-parser-output a { | ||
word-wrap: break-word; | ||
} | ||
|
||
pre, .mw-code { | ||
padding: 1em; | ||
white-space: pre-wrap; | ||
overflow-x: hidden; | ||
word-wrap: break-word; | ||
} | ||
|
||
.wikitable { | ||
background-color: #f8f9fa; | ||
color: #202122; | ||
margin: 1em 0; | ||
margin-left: 0; | ||
border: 1px solid #a2a9b1; | ||
border-collapse: collapse; | ||
} | ||
|
||
.wikitable > tr > th, | ||
.wikitable > * > tr > th { | ||
background-color: #eaecf0; | ||
text-align: center; | ||
} | ||
|
||
.wikitable > tr > th, | ||
.wikitable > tr > td, | ||
.wikitable > * > tr > th, | ||
.wikitable > * > tr > td { | ||
border: 1px solid #a2a9b1; | ||
padding: 0.2em 0.4em; | ||
} | ||
|
||
figure[typeof~="mw:File"].mw-halign-left, | ||
figure[typeof~="mw:File/Frameless"].mw-halign-left { | ||
margin: 0 0.5em 0.5em 0; | ||
clear: left; | ||
float: left; | ||
} | ||
|
||
figure[typeof~="mw:File"].mw-halign-center, | ||
figure[typeof~="mw:File/Frameless"].mw-halign-center { | ||
margin: 0 auto; | ||
display: table; | ||
border-collapse: collapse; | ||
clear: none; | ||
float: none; | ||
} | ||
|
||
figure[typeof~="mw:File"] > figcaption, | ||
figure[typeof~="mw:File/Frameless"] > figcaption { | ||
display: none; | ||
} | ||
|
||
.mw-selflink { | ||
color: #000; | ||
} | ||
|
||
ul.gallery.gallery.gallery { | ||
margin: 2px; | ||
padding: 2px; | ||
display: block; | ||
} | ||
|
||
li.gallerybox { | ||
vertical-align: top; | ||
display: inline-block; | ||
} | ||
|
||
.docnav, .manualtoc { | ||
overflow: hidden; | ||
background-color: rgba(52, 58, 64, 0.5); | ||
} | ||
|
||
.mw-parser-output img { | ||
max-width: 660px; | ||
height: auto; | ||
} | ||
|
||
@media (max-width: 786px) { | ||
.mw-parser-output img { | ||
max-width: 100%; | ||
} | ||
} | ||
|
||
|
||
</style> | ||
|
||
<div id="main" class="container-fluid whitelinks md"> | ||
|
||
<?php | ||
|
||
function createDomAndXpath($html) { | ||
$dom = new DOMDocument(); | ||
libxml_use_internal_errors(true); | ||
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); | ||
libxml_clear_errors(); | ||
return array($dom, new DOMXPath($dom)); | ||
} | ||
|
||
function removeNodesByXPath($xpath, $xpathQuery) { | ||
$nodes = $xpath->query($xpathQuery); | ||
foreach ($nodes as $node) { | ||
$node->parentNode->removeChild($node); | ||
} | ||
} | ||
|
||
function removeDivsByClass($xpath, $classes) { | ||
$xpathQuery = "//div[contains(@class, '" . implode("') or contains(@class, '", $classes) . "')]"; | ||
removeNodesByXPath($xpath, $xpathQuery); | ||
} | ||
|
||
function removeDivsById($xpath, $ids) { | ||
$xpathQuery = "//div[@id='" . implode("' or @id='", $ids) . "']"; | ||
removeNodesByXPath($xpath, $xpathQuery); | ||
} | ||
|
||
function convertRelativeLinksToWikiFormat($xpath, $lang = 'en') { | ||
foreach ($xpath->query("//a[@href]") as $node) { | ||
$href = $node->getAttribute('href'); | ||
if (!preg_match('/^(http|https):\/\//', $href)) { | ||
$href = trim($href, "/"); | ||
if (preg_match('/\/([a-z]{2})$/i', $href, $matches)) { | ||
$langCode = $matches[1]; | ||
$newHref = "/wiki-" . trim($href, '/' . $langCode) . ".php?lang=" . $langCode; | ||
} else { | ||
$newHref = "/wiki-" . trim($href, '/') . ".php"; | ||
if ($lang != "en") { | ||
$newHref .= "?lang=" . $lang; | ||
} | ||
} | ||
$node->setAttribute('href', $newHref); | ||
} | ||
} | ||
} | ||
|
||
function removeFileLinks($xpath) { | ||
foreach ($xpath->query("//a[contains(@href, 'File:')]") as $node) { | ||
$parent = $node->parentNode; | ||
while ($node->firstChild) { | ||
$parent->insertBefore($node->firstChild, $node); | ||
} | ||
$parent->removeChild($node); | ||
} | ||
} | ||
|
||
function removeInlineStylesByClasses($xpath, $classNames) { | ||
foreach ($classNames as $className) { | ||
foreach ($xpath->query("//div[contains(@class, '$className')]") as $node) { | ||
$node->removeAttribute('style'); | ||
} | ||
} | ||
} | ||
|
||
function addPrefixToImageSrc($xpath, $baseImageUrl) { | ||
foreach ($xpath->query("//img[@src]") as $node) { | ||
$src = $node->getAttribute('src'); | ||
if (!preg_match('/^(http|https):\/\//', $src)) { | ||
$newSrc = rtrim($baseImageUrl, '/') . '/' . ltrim($src, '/'); | ||
$node->setAttribute('src', $newSrc); | ||
} | ||
} | ||
} | ||
|
||
function wrapTablesInDiv($xpath, $divClasses = array(), $dom) { | ||
foreach ($xpath->query("//table") as $table) { | ||
$wrapperDiv = $dom->createElement('div'); | ||
if (!empty($divClasses)) { | ||
$wrapperDiv->setAttribute('class', implode(' ', $divClasses)); | ||
} | ||
$table->parentNode->replaceChild($wrapperDiv, $table); | ||
$wrapperDiv->appendChild($table); | ||
} | ||
} | ||
|
||
function fetchWikiContent($page, $lang = 'en') { | ||
if (isset($lang)) { | ||
$page .= "/" . $lang; | ||
} | ||
|
||
$url = "https://wiki.freecad.org/api.php?action=parse&page=" . $page . "&format=json&disableeditsection=true&disabletoc=true"; | ||
$ch = curl_init(); | ||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
$response = curl_exec($ch); | ||
curl_close($ch); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
function processWikiContent($page, $lang = 'en') { | ||
$data = fetchWikiContent($page, $lang); | ||
$htmlContent = $data['parse']['text']['*']; | ||
|
||
list($dom, $xpath) = createDomAndXpath($htmlContent); | ||
|
||
addPrefixToImageSrc($xpath, "https://wiki.freecad.org"); | ||
removeDivsByClass($xpath, array('NavFrame', 'mw-pt-languages')); | ||
removeDivsById($xpath, array('itsstillfree', 'itsfree')); | ||
removeInlineStylesByClasses($xpath, array('docnav')); | ||
removeFileLinks($xpath); | ||
convertRelativeLinksToWikiFormat($xpath, $lang); | ||
wrapTablesInDiv($xpath, array('table-responsive'),$dom); | ||
|
||
return $dom->saveHTML(); | ||
} | ||
|
||
echo processWikiContent($page, $lang); | ||
|
||
?> | ||
|
||
<p> | ||
<?php echo _("This page is retrieved from"); ?> | ||
<a href="<?php echo "https://wiki.freecad.org/".$page; ?>"><?php echo "https://wiki.freecad.org/".$page;?></a> | ||
</p> | ||
</div> | ||
<?php | ||
include 'footer.php'; | ||
?> |