buildHtml($jsonData);
}
private function buildHtml($data) {
$html = "\n\n";
if (isset($data['head'])) {
$html .= $this->parseHead($data['head']);
}
if (isset($data['body'])) {
$html .= "
\n" . $this->parseContent($data['body']['content']) . "\n\n";
}
$html .= "";
return $html;
}
private function parseHead($headData) {
$html = "\n";
// Title
if (isset($headData['title'])) {
$html .= "{$headData['title']}\n";
}
// Inline CSS Styles
if (isset($headData['styles'])) {
$html .= "\n";
}
$html .= "\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);
}
}
// Instantiate and use the class outside the class definition
$parser = new JsonToHtmlParser();
$htmlOutput = $parser->parseJsonFile("/home/ortelbachf/Schreibtisch/Examplary/Examplary.json");
// Automatically save the HTML to output.html in the same directory
$outputFilePath = getcwd() . "/output.html";
file_put_contents($outputFilePath, $htmlOutput);
echo "HTML file generated successfully! Open 'output.html' to view the website.";
?>