main.go (845B)
1 package main 2 3 import ( 4 "bytes" 5 "flag" 6 "os" 7 8 "github.com/alecthomas/chroma/formatters/html" 9 "github.com/alecthomas/chroma/lexers" 10 "github.com/alecthomas/chroma/styles" 11 ) 12 13 func main() { 14 var fileName string 15 flag.StringVar(&fileName, "f", "", "File name") 16 flag.Parse() 17 by, err := os.ReadFile(fileName) 18 if err != nil { 19 panic(err) 20 } 21 lexer := lexers.Match(fileName) 22 style := styles.Get("monokai") 23 formatter := html.New(html.Standalone(true), html.TabWidth(4), html.WithLineNumbers(true), html.LineNumbersInTable(true)) 24 iterator, _ := lexer.Tokenise(nil, string(by)) 25 buf := bytes.Buffer{} 26 _ = formatter.Format(&buf, style, iterator) 27 out := buf.Bytes() 28 out = bytes.ReplaceAll(out, []byte("{{"), []byte("{{")) 29 out = bytes.ReplaceAll(out, []byte("}}"), []byte("}}")) 30 _ = os.WriteFile("output.html", out, 0644) 31 }