-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (89 loc) · 2.26 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"bytes"
"encoding/json"
"html"
"html/template"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/kylegrantlucas/resume-exporter/models"
"github.com/kylegrantlucas/resume-exporter/templates"
flag "github.com/ogier/pflag"
)
func main() {
log.SetFlags(0)
var infile, outfile, templateName string
flag.StringVarP(&infile, "infile", "i", "resume.json", "the file to read in")
flag.StringVarP(&outfile, "outfile", "o", "", "the file to output to")
flag.StringVarP(&templateName, "template", "t", "classic", "the template to use")
flag.Parse()
// Load the resume json
data, err := ioutil.ReadFile(infile)
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
// Unmarshal the JSON
resume := models.Resume{}
err = json.Unmarshal(data, &resume)
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
// Setup the Tex template
tmpl, err := template.New(infile).Delims("[[", "]]").Parse(templateMap[templateName])
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
// Exectute the template
buf := bytes.Buffer{}
err = tmpl.Execute(&buf, resume)
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
// Replace some strings that get escaped by acctident
texString := buf.String()
texString = strings.Replace(texString, "&", "+", -1)
texString = html.UnescapeString(texString)
// Print out to stdout or file based on flags
if outfile != "" {
out, err := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
_, err = out.Write([]byte(texString))
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
} else {
log.Print(texString)
}
if classMap[templateName] != "" {
outpath, err := filepath.Abs(filepath.Dir(outfile))
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
out, err := os.OpenFile(outpath+"/"+templateName+".cls", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.SetFlags(log.Lshortfile)
log.Fatal(err)
}
defer out.Close()
out.Write([]byte(classMap[templateName]))
}
}
var templateMap = map[string]string{
"classic": templates.Classic,
"modern": templates.Modern,
}
var classMap = map[string]string{
"modern": templates.ModernClass,
}