/
generateHTML.go
73 lines (61 loc) · 1.54 KB
/
generateHTML.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"io/ioutil"
"log"
"os"
)
func readFile(path string) (string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("Error reading file %s: %s", path, err)
}
return string(content), nil
}
func createHTML(content string, file *os.File) error {
language := detectLanguage(content)
lexer := lexers.Get(language)
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
style := styles.Get("dracula")
if style == nil {
style = styles.Fallback
}
formatter := html.New(html.Standalone(true), html.WithLineNumbers(true), html.LinkableLineNumbers(true, ""), html.LineNumbersInTable(true))
iterator, err := lexer.Tokenise(nil, content)
if err != nil {
return fmt.Errorf("Error tokenizing content HTML: %s", err)
}
err = formatter.Format(file, style, iterator)
if err != nil {
return fmt.Errorf("Error writing HTML: %s", err)
}
return nil
}
func convert(files paths) error {
content, err := readFile(files.textFile)
if err != nil {
return fmt.Errorf("Error reading file %s: %s", files.textFile, err)
}
file, err := os.Create(files.htmlFile)
if err != nil {
return fmt.Errorf("Error creating file: %s", err)
}
defer func() {
err := file.Close()
if err != nil {
log.Printf("Error closing file: %s", err)
}
}()
err = createHTML(content, file)
if err != nil {
return err
}
return nil
}