-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (131 loc) · 3.94 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"encoding/json"
"fmt"
"github.com/dustin/go-humanize"
"github.com/i582/cfmt/cmd/cfmt"
citeproc "github.com/sett17/citeproc-js-go"
"github.com/sett17/citeproc-js-go/csljson"
"github.com/sett17/mdpaper/v2/cli"
"github.com/sett17/mdpaper/v2/globals"
"github.com/sett17/mdpaper/v2/goldmark-cite"
goldmark_figref "github.com/sett17/mdpaper/v2/goldmark-figref"
goldmark_math "github.com/sett17/mdpaper/v2/goldmark-math"
"github.com/sett17/mdpaper/v2/pdf"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/text"
"os"
"time"
)
func main() {
cfmt.RegisterStyle("purple", func(s string) string {
return cfmt.Sprintf("{{%s}}::#BA8EF7", s)
})
cli.ParseForHelp(os.Args[1:])
cli.ParseForVersion(os.Args[1:])
mdFiles := cli.Parse(os.Args[1:])
if len(mdFiles) == 0 {
cli.Error(fmt.Errorf("no input file"), false)
cli.HelpProgArg.Func("")
}
if !globals.DidConfig {
cli.CfgFunc("config.yaml")
}
cli.Output("Input files: %q\n", mdFiles)
for _, file := range mdFiles {
inp, err := os.ReadFile(file)
if err != nil {
cli.Error(err, false)
continue
}
globals.File = append(globals.File, inp...)
globals.File = append(globals.File, 0x0a, 0x0a)
}
start := time.Now()
p := goldmark.New(
goldmark.WithExtensions(
&goldmark_cite.CitationExtension{},
&goldmark_math.Extender{},
&goldmark_figref.FigRefExtension{},
meta.Meta, // just to ignore frontmatter
extension.NewTable(),
),
goldmark.WithParserOptions()).Parser()
ast := p.Parse(text.NewReader(globals.File))
//ast.Dump(globals.File, 0)
//return
parsed := time.Now()
cli.Output("Parsed in %v\n", parsed.Sub(start))
if globals.Cfg.Citation.Enabled {
globals.Citeproc = citeproc.NewSession()
if globals.Cfg.Citation.CSLFile != "" {
err := globals.Citeproc.SetCslFile(globals.Cfg.Citation.CSLFile)
if err != nil {
cli.Error(err, true)
}
}
if globals.Cfg.Citation.LocaleFile != "" {
err := globals.Citeproc.SetLocaleFile(globals.Cfg.Citation.LocaleFile)
if err != nil {
cli.Error(err, true)
}
}
err := globals.Citeproc.Init()
if err != nil {
cli.Error(err, false)
cli.Info("Citations will be turned off\n")
globals.Cfg.Citation.Enabled = false
}
bibFile, err := os.ReadFile(globals.Cfg.Citation.File)
if err == nil {
citsList := make([]csljson.Item, 0)
err := json.Unmarshal(bibFile, &citsList)
if err != nil {
cli.Error(err, true)
}
for _, cit := range citsList {
//globals.Citations[cit.CitationKey] = cit
globals.Citations[cit.ID] = cit
}
err = globals.Citeproc.AddCitation(citsList...)
if err != nil {
cli.Error(err, false)
cli.Info("Citations will be turned off\n")
globals.Cfg.Citation.Enabled = false
}
} else {
cli.Error(fmt.Errorf("could not read bibliography file"), false)
cli.Info("Citations will be turned off\n")
globals.Cfg.Citation.Enabled = false
}
citT := time.Now()
cli.Output("CSLJSON loaded in %v\n\n", citT.Sub(parsed))
}
pp := pdf.FromAst(ast)
outName := globals.NameEncode(globals.Cfg.Paper.Title) + ".pdf"
outp, err := os.Create(outName)
if err != nil {
cli.Error(err, true)
}
beforeWrite := time.Now()
pp.WriteFile(outp)
doneWrite := time.Now()
fi, err := outp.Stat()
cli.Output("%s of PDF put into %s, in %v\n", humanize.Bytes(uint64(fi.Size())), outName, doneWrite.Sub(beforeWrite))
cli.Output("Total time: %v\n", doneWrite.Sub(start))
if globals.Cfg.Paper.Debug {
dbgOut, err := os.Create("debug.txt")
if err != nil {
cli.Error(err, true)
}
globals.Cfg.Paper.Debug = true
pp.WriteDebug(dbgOut)
fi, err = dbgOut.Stat()
cli.Output("%s of debug info put into debug.txt, in %v\n", humanize.Bytes(uint64(fi.Size())), doneWrite.Sub(beforeWrite))
}
cli.Warning("Please look over the PDF yourself and make sure that it is correct.\nmdpaper is not responsible for the correctness of the output.")
println()
cli.CheckVersion()
}