Add proper elements <element name="head"> will now be treated as <head>. WARNING: I WAS A STUPID NINCOMPOOP AND BROKE NESTING ELEMENTS!!!

This commit is contained in:
conzer 2024-12-05 20:57:48 -05:00
parent cb7a238c80
commit 89fff8112f
8 changed files with 111 additions and 26 deletions

View file

@ -1,19 +1,74 @@
package compiler
import (
"strings"
"fmt"
"os"
"hylia/parser"
)
// THE HYLIA COMPILER
func Compile(elements []string) (string, error) {
html := "<!DOCTYPE html>\n<html>\n<body>\n"
func Compile(elements []parser.Element, outputFile string) error {
var head string
var body string
var other string
var errmsg string
errmsg += "Failed to write to the output file: %w"
// Take the elements into HTML
for _, element := range elements {
html += element + "\n"
switch element.Name {
case "head":
head += element.Content + "\n"
case "body":
body += element.Content + "\n"
default:
other += element.Content + "\n"
}
}
file, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("Failed to create output file: %w", err)
}
defer file.Close()
// Write the HTML structure to compile into
_, err = file.WriteString("<!DOCTYPE html>\n<html>\n")
if err != nil {
return fmt.Errorf("Failed to write to the output file: %w", err)
}
html += "</body>\n</html>"
return html, nil
if head != "" {
_, err = file.WriteString("<head>\n" + head + "</head>\n")
if err != nil {
return fmt.Errorf(errmsg, err)
}
}
if body != "" {
_, err = file.WriteString("<body>\n" + body + "</body>\n")
if err != nil {
return fmt.Errorf(errmsg, err)
}
}
if other != "" {
_, err = file.WriteString(other)
if err != nil {
return fmt.Errorf(errmsg, err)
}
}
// Close the HTML structs
_, err = file.WriteString("</html>\n")
if err != nil {
return fmt.Errorf("Failed to write to the output file: %w", err)
}
return nil
}