package compiler import ( "fmt" "os" "hylia/parser" ) // THE HYLIA COMPILER 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" for _, element := range elements { 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("\n\n") if err != nil { return fmt.Errorf("Failed to write to the output file: %w", err) } if head != "" { _, err = file.WriteString("\n" + head + "\n") if err != nil { return fmt.Errorf(errmsg, err) } } if body != "" { _, err = file.WriteString("\n" + 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("\n") if err != nil { return fmt.Errorf("Failed to write to the output file: %w", err) } return nil }