diff --git a/compiler/compiler.go b/compiler/compiler.go index b0b1748..01d88c0 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -1,19 +1,74 @@ package compiler import ( - "strings" + "fmt" + "os" + "hylia/parser" ) // THE HYLIA COMPILER -func Compile(elements []string) (string, error) { - html := "\n\n
\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("\n\n") + if err != nil { + return fmt.Errorf("Failed to write to the output file: %w", err) } - html += "\n" - return html, nil + 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 + } \ No newline at end of file diff --git a/examples/header.hy b/examples/header.hy new file mode 100644 index 0000000..97f4620 --- /dev/null +++ b/examples/header.hy @@ -0,0 +1,6 @@ +This is a reusable header component.
+This is the main content.
+This is the main content.
+ + + +This is a reusable header component.
+ + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..acec70e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module hylia + +go 1.23.3 diff --git a/hylia b/hylia new file mode 100755 index 0000000..025aa79 Binary files /dev/null and b/hylia differ diff --git a/main.go b/main.go index ce23c67..39b72c5 100644 --- a/main.go +++ b/main.go @@ -8,31 +8,24 @@ import ( ) func main() { - if len(os.args) < 3 { + if len(os.Args) < 3 { fmt.Println("Usage: hylia