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 @@ + + +

Welcome to Hylia!

+

This is a reusable header component.

+
+
\ No newline at end of file diff --git a/examples/main.hy b/examples/main.hy new file mode 100644 index 0000000..5450dc7 --- /dev/null +++ b/examples/main.hy @@ -0,0 +1,10 @@ + + + + Hello Hylia! + + +
+

This is the main content.

+ + \ No newline at end of file diff --git a/examples/output.html b/examples/output.html new file mode 100644 index 0000000..6fece76 --- /dev/null +++ b/examples/output.html @@ -0,0 +1,18 @@ + + + + + Hello Hylia! + + + + +
+

This is the main content.

+ + + +

Welcome to Hylia!

+

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 ") return } inputFile := os.Args[1] outputFile := os.Args[2] - fmt.Println("Processing input:", inputFile) - - parsedElements, err := parser.ParseFile(inputFile) + + parsedElememts, err := parser.ParseFile(inputFile) if err != nil { - fmt.Println("ERROR! ", err) - return + fmt.Printf("ERROR! %v\n", err) + os.Exit(1) } - htmlOutput, err := compiler.Compile(parsedElements) + err = compiler.Compile(parsedElememts, outputFile) if err != nil { - fmt.Println("ERROR! ", err) - return - } - - err = os.WriteFile(outputFile, []byte(htmlOutput), 0644) - if err != nil { - fmt.Println("ERROR! ", err) - return + fmt.Printf("ERROR! %v\n", err) + os.Exit(1) } fmt.Println("SUCCESS! Output saved to ", outputFile) diff --git a/parser/parser.go b/parser/parser.go index d08f258..26a231d 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -15,7 +15,7 @@ type Element struct { FilePath string } -func ParseFile(filename string) ([]string error) { +func ParseFile(filename string) ([]Element, error) { // Hey, does the file we're trying to parse actually exist? data, err := ioutil.ReadFile(filename) if err != nil { @@ -38,11 +38,11 @@ func ParseFile(filename string) ([]string error) { content = content[start+len("") : end] // Extract the custom elements, which are wrapped in tags and handle imports - elements := []string{} + elements := []Element{} lines := strings.Split(content, "\n") for _, line := range lines { line = strings.TrimSpace(line) - if strings.HasPrefix(line, <"element") { + if strings.HasPrefix(line, " tag is missing a file ('file' attribute)") }