forked from src-d/enry
/
documentation.go
43 lines (34 loc) · 1.01 KB
/
documentation.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
package generator
import (
"bytes"
"io"
"io/ioutil"
"text/template"
yaml "gopkg.in/yaml.v2"
)
// Documentation reads from fileToParse and builds source file from tmplPath. It complies with type File signature.
func Documentation(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) error {
data, err := ioutil.ReadFile(fileToParse)
if err != nil {
return err
}
var regexpList []string
if err := yaml.Unmarshal(data, ®expList); err != nil {
return err
}
buf := &bytes.Buffer{}
if err := executeDocumentationTemplate(buf, regexpList, tmplPath, tmplName, commit); err != nil {
return err
}
return formatedWrite(outPath, buf.Bytes())
}
func executeDocumentationTemplate(out io.Writer, regexpList []string, tmplPath, tmplName, commit string) error {
fmap := template.FuncMap{
"getCommit": func() string { return commit },
}
t := template.Must(template.New(tmplName).Funcs(fmap).ParseFiles(tmplPath))
if err := t.Execute(out, regexpList); err != nil {
return err
}
return nil
}