commit 819e8cde08388b2d207190da673539cc4bfb7709 Author: jamerno Date: Thu Mar 5 14:00:26 2026 +0100 gawo2025-ws-json-html diff --git a/buildHtml($jsonData); + } +} +?> \ No newline at end of file diff --git a/Examplary.html b/Examplary.html new file mode 100755 index 0000000..e141c36 --- /dev/null +++ b/Examplary.html @@ -0,0 +1,109 @@ + + + + Examplary + + + + +
+ +

Facharbeitswebsite

+ + + +
+ +
+
+
+
+
+
+

Special Card Section

+

This card has a gradient background and an image holder.

+ Learn More +
+
+
+
+
+ +
+ +

This is a card.

+

Here could be a card text.

+ +
+

Schlechte Facharbeitswebsite

+
+
+ + + diff --git a/Examplary.json b/Examplary.json new file mode 100755 index 0000000..973fc91 --- /dev/null +++ b/Examplary.json @@ -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" } + ] + } + ] + } + ] + } +} diff --git a/Examplary.zip b/Examplary.zip new file mode 100755 index 0000000..b9a38fc Binary files /dev/null and b/Examplary.zip differ diff --git a/Simple.json b/Simple.json new file mode 100755 index 0000000..0f0885e --- /dev/null +++ b/Simple.json @@ -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" + } + ] + } +} diff --git a/check.php b/check.php new file mode 100755 index 0000000..a4ad6cd --- /dev/null +++ b/check.php @@ -0,0 +1,16 @@ + \ No newline at end of file diff --git a/debug.php b/debug.php new file mode 100755 index 0000000..4f9f708 --- /dev/null +++ b/debug.php @@ -0,0 +1,3 @@ + "working"]); +?> diff --git a/index.php b/index.php new file mode 100755 index 0000000..405d96a --- /dev/null +++ b/index.php @@ -0,0 +1,75 @@ +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"; + if (isset($headData['title'])) { + $html .= "{$headData['title']}\n"; + } + $html .= "\n"; // Include a CSS file + $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\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."; + +?> diff --git a/innstructions.txt b/innstructions.txt new file mode 100755 index 0000000..5946d7c --- /dev/null +++ b/innstructions.txt @@ -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 + diff --git a/output.html b/output.html new file mode 100755 index 0000000..77f8da0 --- /dev/null +++ b/output.html @@ -0,0 +1,40 @@ + + + +Examplary + + + +

Examplary Website.

+

Facharbeitswebsite

+ +
+
+
+
+
+
+

This isn`t a card.

+

Here couldn`t be a card text.

+

Schlechte Facharbeit

+
+
+ + + \ No newline at end of file diff --git a/output2.html b/output2.html new file mode 100755 index 0000000..1999572 --- /dev/null +++ b/output2.html @@ -0,0 +1,15 @@ + + + +Box Example + + + +
This is a simple outlined box with some text inside.
+Press me + + + \ No newline at end of file diff --git a/parser.php b/parser.php new file mode 100755 index 0000000..48d08d5 --- /dev/null +++ b/parser.php @@ -0,0 +1,89 @@ +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\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."; +?> diff --git a/parser2.php b/parser2.php new file mode 100755 index 0000000..1ff9dfb --- /dev/null +++ b/parser2.php @@ -0,0 +1,89 @@ +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\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."; +?> diff --git a/test.php b/test.php new file mode 100755 index 0000000..00001ca --- /dev/null +++ b/test.php @@ -0,0 +1,10 @@ + diff --git a/testing.php b/testing.php new file mode 100755 index 0000000..1259f2d --- /dev/null +++ b/testing.php @@ -0,0 +1,120 @@ +$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 = "\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\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."; + +?>