gawo2025-ws-json-html

This commit is contained in:
2026-03-05 14:00:26 +01:00
commit 819e8cde08
15 changed files with 710 additions and 0 deletions
Executable
+120
View File
@@ -0,0 +1,120 @@
<?php
$jsonFilePath = "/home/kuehnk01/Schreibtisch/Examplary/Examplary.json";
if (!is_readable($jsonFilePath)) {
die("JSON file is not readable at $jsonFilePath");
}
echo "File is readbable outside of class" ;
if (file_exists($jsonFilePath)) {
echo "File exist";
} else {
echo "no json";
}
echo "Current working dir" . getcwd() . PHP_EOL;
class JsonToHtmlParser {
public function parseJsonFile($jsonFilePath) {
echo "\n--- DEBUG ---\n";
echo "Input path: >$jsonFilePath<\n";
echo "CWD inside class: " . getcwd() . "\n";
echo "Debugging" ; # debug process
echo "Current working dir is " . getcwd() . "\n";
if (!file_exists($jsonFilePath)) {
die("json not found at $jsonFilePath ") ;
}
echo "file found" ;
if (!is_readable($jsonFilePath)) {
die("json is not readable at $jsonFilePath");
}
echo "reading $jsonFilePath" ;
$jsonData = json_decode($jsonData, true) ;
if ($jsonData == null) {
die("Couldnt decode json" . json_last_error_msg()) ;
}
echo "json decoded succesfully " ;
echo "Testing absolute path inside class: " . $jsonFilePath . "\n";
if (!file_exists($jsonFilePath)) {
die("JSON file not found at $jsonFilePath\n");
}
if (!file_exists($jsonFilePath)) {
echo("JSON file not found.");
}
$jsonData = json_decode(file_get_contents($jsonFilePath), true);
if (!$jsonData) {
die("Invalid JSON format.");
}
return $this->buildHtml($jsonData);
}
private function buildHtml($data) {
$html = "<!DOCTYPE html>\n<html>\n";
if (isset($data['head'])) {
$html .= $this->parseHead($data['head']);
}
if (isset($data['body'])) {
$html .= "<body>\n" . $this->parseContent($data['body']['content']) . "\n</body>\n";
}
$html .= "</html>";
return $html;
}
private function parseHead($headData) {
$html = "<head>\n";
// Title
if (isset($headData['title'])) {
$html .= "<title>{$headData['title']}</title>\n";
}
// Inline CSS Styles
if (isset($headData['styles'])) {
$html .= "<style>\n{$headData['styles']}\n</style>\n";
}
$html .= "</head>\n";
return $html;
}
private function parseContent($contentArray) {
$html = "";
foreach ($contentArray as $element) {
$html .= $this->createElement($element);
}
return $html;
}
private function createElement($element) {
if (!isset($element['element'])) return "";
$tag = $element['element'];
$attributes = isset($element['attributes']) ? $this->parseAttributes($element['attributes']) : "";
$value = isset($element['value']) ? $element['value'] : "";
$innerHtml = isset($element['content']) ? $this->parseContent($element['content']) : "";
return "<$tag $attributes>$value$innerHtml</$tag>\n";
}
private function parseAttributes($attributes) {
$htmlAttributes = "";
foreach ($attributes as $key => $value) {
$htmlAttributes .= "$key=\"$value\" ";
}
return trim($htmlAttributes);
}
}
// Generate the HTML from JSON
$parser = new JsonToHtmlParser();
$htmlOutput = $parser->parseJsonFile(__DIR__ . "/examplary.json");
// Save the output to an HTML file
file_put_contents("output.html", $htmlOutput);
echo "HTML file generated successfully! Open 'output.html' to view the website.";
?>