gawo2025-ws-json-html
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
class JsonToHtmlParser {
|
||||
public function parseJsonFile($jsonFilePath) {
|
||||
echo "Current working dir: " . getcwd() . "\n";
|
||||
echo "Checking file at: $jsonFilePath\n";
|
||||
|
||||
$resolvedPath = realpath($jsonFilePath);
|
||||
echo "Resolved file path: $resolvedPath\n";
|
||||
|
||||
if (!file_exists($resolvedPath)) {
|
||||
die("JSON file not found at $resolvedPath\n");
|
||||
}
|
||||
|
||||
echo "File found!\n";
|
||||
$jsonData = json_decode(file_get_contents($resolvedPath), true);
|
||||
if ($jsonData === null) {
|
||||
die("Failed to decode JSON: " . json_last_error_msg());
|
||||
}
|
||||
|
||||
return $this->buildHtml($jsonData);
|
||||
}
|
||||
}
|
||||
?>
|
||||
Executable
+109
@@ -0,0 +1,109 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Examplary</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f4f4f4;
|
||||
text-align: center;
|
||||
}
|
||||
.container {
|
||||
width: 80%;
|
||||
margin: 20px auto;
|
||||
padding: 20px;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
||||
}
|
||||
.nav-list {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
text-align: left;
|
||||
width: fit-content;
|
||||
margin: 0 auto 20px 0; /* Aligns the list to the left */
|
||||
}
|
||||
.nav-list li {
|
||||
padding: 5px 0;
|
||||
font-size: 18px;
|
||||
}
|
||||
.card {
|
||||
width: 60%;
|
||||
margin: 20px auto;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
||||
}
|
||||
.card-body {
|
||||
padding: 20px;
|
||||
}
|
||||
.card-title {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.bg-card-gradient {
|
||||
background: linear-gradient(45deg, #6a11cb, #2575fc);
|
||||
color: white;
|
||||
padding: 30px;
|
||||
}
|
||||
.bg-holder {
|
||||
height: 150px;
|
||||
background: url('https://via.placeholder.com/150') no-repeat center center;
|
||||
background-size: cover;
|
||||
border-radius: 10px;
|
||||
}
|
||||
.button {
|
||||
display: inline-block;
|
||||
padding: 10px 20px;
|
||||
background: #2575fc;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
border-radius: 5px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<h1Examplary Website.</h1>
|
||||
<p>Facharbeitswebsite</p>
|
||||
|
||||
<ul class="nav-list">
|
||||
<li>Basic</li>
|
||||
<li>Elements</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-5 text-center bg-card-gradient">
|
||||
<div class="position-relative p-4 pt-md-5 pb-md-7 light">
|
||||
<div class="bg-holder bg-auth-card-shape"></div>
|
||||
<h2 class="card-title">Special Card Section</h2>
|
||||
<p>This card has a gradient background and an image holder.</p>
|
||||
<a href="#" class="button">Learn More</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
|
||||
<h2>This is a card.</h2>
|
||||
<p>Here could be a card text.</p>
|
||||
|
||||
<footer>
|
||||
<p> Schlechte Facharbeitswebsite</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Executable
+67
@@ -0,0 +1,67 @@
|
||||
{
|
||||
"head": {
|
||||
"title": "Examplary",
|
||||
"styles": "body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: red; text-align: center; }\n.container { width: 80%; margin: 20px auto; padding: 20px; background: green; border-radius: 10px; box-shadow: 0 128px 512px rgba(100,100,200,50); }\n.nav-list { list-style-type: none; padding: 0; text-align: left; width: fit-content; margin: 0 auto 20px 0; }\n.nav-list li { padding: 5px 0; font-size: 18px; }\n.card { width: 60%; margin: 20px auto; background: white; border-radius: 10px; overflow: hidden; box-shadow: 0 128px 1024px rgba(200,50,100,40); }\n.card-body { padding: 20px; }\n.card-title { font-size: 24px; font-weight: bold; }\n.bg-card-gradient { background: linear-gradient(45deg, green, purple); color: white; padding: 30px; }\n.bg-holder { height: 150px; background: url('https://via.placeholder.com/150') no-repeat center center; background-size: cover; border-radius: 10px; }\n.button { display: inline-block; padding: 10px 20px; background: green; color: white; text-decoration: none; border-radius: 5px; margin-top: 10px; }"
|
||||
},
|
||||
"body": {
|
||||
"content": [
|
||||
{
|
||||
"element": "div",
|
||||
"attributes": { "class": "container" },
|
||||
"content": [
|
||||
{ "element": "h1", "value": "Examplary Website." },
|
||||
{ "element": "p", "value": "Facharbeitswebsite" },
|
||||
{
|
||||
"element": "ul",
|
||||
"attributes": { "class": "nav-list" },
|
||||
"content": [
|
||||
{ "element": "li", "value": "Basic" },
|
||||
{ "element": "li", "value": "Elements" }
|
||||
]
|
||||
},
|
||||
{ "element": "hr" },
|
||||
{
|
||||
"element": "div",
|
||||
"attributes": { "class": "col-md-5 text-center bg-card-gradient" },
|
||||
"content": [
|
||||
{
|
||||
"element": "div",
|
||||
"attributes": { "class": "card-body" },
|
||||
"content": [
|
||||
{
|
||||
"element": "div",
|
||||
"attributes": { "class": "card-body" },
|
||||
"content": [
|
||||
{
|
||||
"element": "div",
|
||||
"attributes": { "class": "nav-list" },
|
||||
"content": [
|
||||
{ "element": "h2", "attributes": { "class": "card-title" }, "value": "Special Card Section" },
|
||||
{ "element": "p", "value": "This card has a gradient background and an image holder." },
|
||||
{
|
||||
"element": "a",
|
||||
"attributes": { "href": "output2.html", "class": "button" },
|
||||
"value": "Learn Less"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{ "element": "hr" },
|
||||
{ "element": "h2", "value": "This isn`t a card." },
|
||||
{ "element": "p", "value": "Here couldn`t be a card text." },
|
||||
{
|
||||
"element": "footer",
|
||||
"content": [
|
||||
{ "element": "p", "value": "Schlechte Facharbeit" }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"head": {
|
||||
"title": "Box Example",
|
||||
"styles": ".box { border: 2px solid black; padding-top: 20px; margin: 20px; }\n .button {padding: 100px; background-color: red}"
|
||||
},
|
||||
"body": {
|
||||
"content": [
|
||||
{
|
||||
"element": "div",
|
||||
"attributes": {
|
||||
"class": "box"
|
||||
},
|
||||
"value": "This is a simple outlined box with some text inside."
|
||||
|
||||
},
|
||||
{
|
||||
"element": "a",
|
||||
"attributes": {"href": "output.html", "class": "button"},
|
||||
"value": "Press me"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
$jsonFilePath = "/home/kuehnk01/Schreibtisch/Examplary/Examplary.json" ;
|
||||
if (file_exists($jsonFilePath)) {
|
||||
echo "File exist";
|
||||
} else {
|
||||
echo "no json";
|
||||
}
|
||||
echo realpath("Examplary.json");
|
||||
echo getcwd();
|
||||
echo file_exists("Examplary.json") ? "File exists" : "file not found";
|
||||
echo "current user: " . get_current_user();
|
||||
if (is_readable($jsonFilePath)) {
|
||||
echo "File can be accessed" ;
|
||||
}
|
||||
echo "Current working dir" . getcwd() . PHP_EOL;
|
||||
?>
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class JsonToHtmlParser {
|
||||
public function parseJsonFile($jsonFilePath) {
|
||||
if (!file_exists($jsonFilePath)) {
|
||||
die("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";
|
||||
if (isset($headData['title'])) {
|
||||
$html .= "<title>{$headData['title']}</title>\n";
|
||||
}
|
||||
$html .= "<link rel='stylesheet' type='text/css' href='styles.css'>\n"; // Include a CSS file
|
||||
$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("data.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.";
|
||||
|
||||
?>
|
||||
Executable
+31
@@ -0,0 +1,31 @@
|
||||
How to Install and Run the Examplary Website with PHP
|
||||
|
||||
1. Install PHP (if not already installed)
|
||||
- Windows:
|
||||
1. Download PHP from https://windows.php.net/download/
|
||||
2. Extract the files to C:\php
|
||||
3. Add C:\php to the system PATH environment variable
|
||||
- Linux (Debian/Ubuntu):
|
||||
1. Open a terminal
|
||||
2. Run: sudo apt update && sudo apt install php
|
||||
- macOS:
|
||||
1. Open a terminal
|
||||
2. Run: brew install php (if Homebrew is installed)
|
||||
3. Alternatively, install PHP manually from https://www.php.net/downloads
|
||||
|
||||
2. Save the project files in the same folder:
|
||||
- parser.php
|
||||
- examplary.json
|
||||
|
||||
3. Open a terminal or command prompt and navigate to the folder:
|
||||
- Example: cd path/to/folder
|
||||
|
||||
4. Run the PHP script to generate the HTML file:
|
||||
php parser.php
|
||||
|
||||
5. Start a local PHP server:
|
||||
php -S localhost:8000
|
||||
|
||||
6. Open a web browser and go to:
|
||||
http://localhost:8000/output.html
|
||||
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Examplary</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: red; text-align: center; }
|
||||
.container { width: 80%; margin: 20px auto; padding: 20px; background: green; border-radius: 10px; box-shadow: 0 128px 512px rgba(100,100,200,50); }
|
||||
.nav-list { list-style-type: none; padding: 0; text-align: left; width: fit-content; margin: 0 auto 20px 0; }
|
||||
.nav-list li { padding: 5px 0; font-size: 18px; }
|
||||
.card { width: 60%; margin: 20px auto; background: white; border-radius: 10px; overflow: hidden; box-shadow: 0 128px 1024px rgba(200,50,100,40); }
|
||||
.card-body { padding: 20px; }
|
||||
.card-title { font-size: 24px; font-weight: bold; }
|
||||
.bg-card-gradient { background: linear-gradient(45deg, green, purple); color: white; padding: 30px; }
|
||||
.bg-holder { height: 150px; background: url('https://via.placeholder.com/150') no-repeat center center; background-size: cover; border-radius: 10px; }
|
||||
.button { display: inline-block; padding: 10px 20px; background: green; color: white; text-decoration: none; border-radius: 5px; margin-top: 10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container"><h1 >Examplary Website.</h1>
|
||||
<p >Facharbeitswebsite</p>
|
||||
<ul class="nav-list"><li >Basic</li>
|
||||
<li >Elements</li>
|
||||
</ul>
|
||||
<hr ></hr>
|
||||
<div class="col-md-5 text-center bg-card-gradient"><div class="card-body"><div class="card-body"><div class="nav-list"><h2 class="card-title">Special Card Section</h2>
|
||||
<p >This card has a gradient background and an image holder.</p>
|
||||
<a href="output2.html" class="button">Learn Less</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<hr ></hr>
|
||||
<h2 >This isn`t a card.</h2>
|
||||
<p >Here couldn`t be a card text.</p>
|
||||
<footer ><p >Schlechte Facharbeit</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Box Example</title>
|
||||
<style>
|
||||
.box { border: 2px solid black; padding-top: 20px; margin: 20px; }
|
||||
.button {padding: 100px; background-color: red}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="box">This is a simple outlined box with some text inside.</div>
|
||||
<a href="output.html" class="button">Press me</a>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
class JsonToHtmlParser {
|
||||
public function parseJsonFile($jsonFilePath) {
|
||||
echo "Current working dir: " . getcwd() . "\n";
|
||||
echo "Checking file at: $jsonFilePath\n";
|
||||
|
||||
$resolvedPath = realpath($jsonFilePath);
|
||||
echo "Resolved file path: $resolvedPath\n";
|
||||
|
||||
if (!file_exists($resolvedPath)) {
|
||||
die("JSON file not found at $resolvedPath\n");
|
||||
}
|
||||
|
||||
echo "File found!\n";
|
||||
$jsonData = json_decode(file_get_contents($resolvedPath), true);
|
||||
if ($jsonData === null) {
|
||||
die("Failed to decode JSON: " . json_last_error_msg());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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.";
|
||||
?>
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
class JsonToHtmlParser {
|
||||
public function parseJsonFile($jsonFilePath) {
|
||||
echo "Current working dir: " . getcwd() . "\n";
|
||||
echo "Checking file at: $jsonFilePath\n";
|
||||
|
||||
$resolvedPath = realpath($jsonFilePath);
|
||||
echo "Resolved file path: $resolvedPath\n";
|
||||
|
||||
if (!file_exists($resolvedPath)) {
|
||||
die("JSON file not found at $resolvedPath\n");
|
||||
}
|
||||
|
||||
echo "File found!\n";
|
||||
$jsonData = json_decode(file_get_contents($resolvedPath), true);
|
||||
if ($jsonData === null) {
|
||||
die("Failed to decode JSON: " . json_last_error_msg());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Instantiate and use the class outside the class definition
|
||||
$parser = new JsonToHtmlParser();
|
||||
$htmlOutput = $parser->parseJsonFile("/home/ortelbachf/Schreibtisch/Examplary/Simple.json");
|
||||
|
||||
// Automatically save the HTML to output.html in the same directory
|
||||
$outputFilePath = getcwd() . "/output2.html";
|
||||
file_put_contents($outputFilePath, $htmlOutput);
|
||||
|
||||
echo "HTML file generated successfully! Open 'output.html' to view the website.";
|
||||
?>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
$jsonFilePath = "/home/kuehnk01/Schreibtisch/Examplary/Examplary.json";
|
||||
if (file_exists($jsonFilePath)) {
|
||||
echo "File exists.\n";
|
||||
$jsonData = file_get_contents($jsonFilePath);
|
||||
echo "JSON Content: " . $jsonData;
|
||||
} else {
|
||||
echo "File not found.\n";
|
||||
}
|
||||
?>
|
||||
Executable
+120
@@ -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.";
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user