Skip to content

Commit

Permalink
feat: rename func, TODO additions
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Nov 5, 2024
1 parent 30702b4 commit 2f029f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 49 deletions.
13 changes: 4 additions & 9 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
- [ X ] Determine package name to be generated
- [ ] Determine language to be used in lexer
```go
lexer := lexers.Match("foo.go")
if lexer == nil {
lexer = lexers.Fallback
}
chroma.Coalesce(lexer)
```
- [ ] With args, args for chroma
- [ ] Drill down style
- [ ] Flag for with version
- [ ] Flag for with timestamp
- [ ] Do we need lazy?
- [ ] Do we need the post generation loop? If so, we should add back the cmd flag
- [ ] Clean up
- [ ] Tests
- [ ] Docs
42 changes: 2 additions & 40 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (g *generator) generate() (err error) {
if err = g.writeImports(); err != nil {
return
}
if err = g.writFoo(); err != nil {
if err = g.writeComponent(); err != nil {
return
}
if err = g.writeBlankAssignmentForRuntimeImport(); err != nil {
Expand Down Expand Up @@ -148,7 +148,7 @@ func (g *generator) writeImports() error {
return nil
}

func (g *generator) writFoo() (err error) {
func (g *generator) writeComponent() (err error) {
if _, err = g.w.Write("func " + g.componentName + "() templ.Component {\n"); err != nil {
return
}
Expand Down Expand Up @@ -259,44 +259,6 @@ func (g *generator) writFoo() (err error) {
return nil
}

func (g *generator) writeComponent() (err error) {
if _, err = g.w.Write("templ " + g.componentName + "() {\n"); err != nil {
return
}

f, err := os.ReadFile(g.og)
contents, err := io.ReadAll(bytes.NewReader(f))
lexer := lexers.Analyse(string(contents))
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
formatter := html.New(html.PreventSurroundingPre(true), html.InlineCode(true))
style := styles.Get("monkokailight")
if style == nil {
style = styles.Fallback
}
iterator, err := lexer.Tokenise(nil, string(contents))
var b bytes.Buffer
err = formatter.Format(&b, style, iterator)
if err != nil {
return err
}
// if _, err = g.w.Write("<pre> {`\n" + b.String() + "\n`}</pre>"); err != nil {

if _, err = g.w.Write(b.String()); err != nil {
return err
}
if _, err = g.w.Write("\n"); err != nil {
return err
}

if _, err := g.w.Write("}\n"); err != nil {
return err
}
return err
}

// writeBlankAssignmentForRuntimeImport writes out a blank identifier assignment.
// This ensures that even if the github.com/a-h/templ/runtime package is not used in the generated code,
// the Go compiler will not complain about the unused import.
Expand Down

0 comments on commit 2f029f9

Please sign in to comment.