diff --git a/compiler/compiler.go b/compiler/compiler.go index d9a0bad..4fd643d 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -61,6 +61,7 @@ func Compile(elements []parser.Element, variables map[string]string, outputFile func writeElements(elements []parser.Element, file *os.File, variables map[string]string) error { for _, element := range elements { + if element.Name == "head" { _, err := file.WriteString(fmt.Sprintf("\n")) if err != nil { diff --git a/examples/header.hy b/examples/header.hy index 216705f..c87707e 100644 --- a/examples/header.hy +++ b/examples/header.hy @@ -1,9 +1,5 @@ - - I am in the body element. - -

I am not in the body element.

- - I am outside the body element, but inside the outside element. - + +

test hello

+
\ No newline at end of file diff --git a/examples/main.hy b/examples/main.hy index d15d244..527319b 100644 --- a/examples/main.hy +++ b/examples/main.hy @@ -1,9 +1,11 @@ + Hello Hylia!

Hello!


Hello again!

+

Hello Hylia!

This is an example content.

{{testvar}} diff --git a/examples/output.htm b/examples/output.htm index e4ba8b9..a6659af 100644 --- a/examples/output.htm +++ b/examples/output.htm @@ -1,7 +1,12 @@ + +Hello Hylia! + -I am in the body element. + +

Hello Hylia!

+

This is an example content.

+

Hello!


Hello again!

-I am outside the body element, but inside the outside element. diff --git a/hylia b/hylia new file mode 100755 index 0000000..642d694 Binary files /dev/null and b/hylia differ diff --git a/importer/import.go b/importer/import.go new file mode 100644 index 0000000..bc047bf --- /dev/null +++ b/importer/import.go @@ -0,0 +1,35 @@ +package importer + +import ( + "errors" + "fmt" + "io/ioutil" + "path/filepath" + "strings" +) + +type Class struct { + Name string + Content string + FilePath string +} + +func ImportClasses(filePath string) (map[string]Class, error) { + content, err := ioutil.ReadFile(filePath) + if err != nil { + return nil, fmt.Errorf("error reading file '%s': %w", filePath, err) + } + + classes := make(map[string]Class) + lines := strings.Split(string(content), "\n") + var currentClass *Class + var nestedContent strings.Builder + + for _, line := range lines { + line = strings.TrimSpace(line) + if strings.HasPrefix(line, "") { + if currentElement != nil { + currentElement.Content = nestedContent.String() + parsedNested, nestedVars, _ := parseElements(nestedContent.String(), filename) + currentElement.NestedElements = parsedNested + mergeVariables(variables, nestedVars) + if classes == nil { + classes = make(map[string]Element) + } + classes[currentElement.Name] = *currentElement + + nestedContent.Reset() + currentElement = nil + } + + } else { + if currentElement != nil { nestedContent.WriteString(line + "\n") }