-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfgen.go
261 lines (212 loc) · 5.64 KB
/
pdfgen.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2022 The golang.design Initiative.
// All rights reserved. Created by Changkun Ou <changkun.de>
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
"gopkg.in/yaml.v3"
"mvdan.cc/xurls/v2"
)
var md goldmark.Markdown
func init() {
md = goldmark.New(
goldmark.WithExtensions(
meta.Meta,
),
)
}
func usage() {
fmt.Fprintf(os.Stderr, `pdfgen converts a golang.design research markdown file to a pdf.
usage: pdfgen bench-time.md
`)
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
usage()
return
}
path := args[0]
// Only deal with .md files
if !strings.HasSuffix(path, ".md") {
log.Fatalf("pdfgen: input file must be a markdown file.")
return
}
b, err := os.ReadFile(path)
if err != nil {
log.Fatalf("pdfgen: failed to load the given markdown file.")
}
var buf bytes.Buffer
context := parser.NewContext()
if err := md.Convert(b, &buf, parser.WithContext(context)); err != nil {
log.Fatal(err)
}
metaData := meta.Get(context)
convertDate(metaData)
authors := parseAuthor(b)
metaData["author"] = authors
abstrat := parseAbstract(b)
// https://stackoverflow.com/questions/1919982/regex-smallest-possible-match-or-nongreedy-match
re := regexp.MustCompile("\\[\\^(.*?)\\]")
abstrat = re.ReplaceAllString(abstrat, "\\cite{$1}") // use citation key
metaData["abstract"] = abstrat
metaData["header-includes"] = `\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LE,RO]{\rightmark}
\fancyhead[RE,LO]{The golang.design Research}
\fancyfoot{}
\fancyfoot[C]{\thepage}`
body := parseBody(b)
body = re.ReplaceAllString(body, "\\cite{$1}") // use citation key
head, err := yaml.Marshal(metaData)
if err != nil {
log.Fatalf("pdfgen: failed to construct metadata")
}
content := fmt.Sprintf(`---
%v
---
%v
`, string(head), body)
// Prepare all content.
ref := "ref.tex"
references := parseReferences(b)
if err := os.WriteFile(ref, []byte(references), os.ModePerm); err != nil {
log.Fatalf("pdfgen: cannot create reference file: %v", err)
}
defer os.Remove(ref)
article := "article.md"
if err := os.WriteFile(article, []byte(content), os.ModePerm); err != nil {
log.Fatalf("pdfgen: cannot create temporary file: %v", err)
}
defer os.Remove(article)
// Generate pdf
dst := "../" + strings.TrimSuffix(path, ".md") + ".pdf"
cmd := exec.Command("pandoc", article, ref,
"-V", "linkcolor:blue",
"--pdf-engine=xelatex",
"-o", dst)
log.Println(cmd.String())
if b, err := cmd.CombinedOutput(); err != nil {
log.Fatal(string(b))
}
return
}
func convertDate(metaData map[string]any) {
dateRaw, ok := metaData["date"]
if !ok {
log.Fatalf("pdfgen: metadata missing date information.")
}
date, ok := dateRaw.(string)
if !ok {
log.Fatalf("pdfgen: metadata contains invalid date format.")
}
t, err := time.Parse("2006-01-02T15:04:05Z07:00", date)
if err != nil {
log.Fatalf("pdfgen: cannot parse date: %v", err)
}
metaData["date"] = t.Format("January 02, 2006")
}
type author struct {
Name string
Email string
}
func (a author) String() string {
return fmt.Sprintf("%v^[Email: %v]", a.Name, a.Email)
}
func parseAuthor(b []byte) []string {
s := bufio.NewScanner(bytes.NewReader(b))
authors := []string{}
for s.Scan() {
l := s.Text()
if strings.HasPrefix(l, "Author(s): ") {
authorsStr := strings.TrimPrefix(l, "Author(s): ")
authorList := strings.Split(authorsStr, ", ")
for _, a := range authorList {
before, after, ok := strings.Cut(a, "](")
if !ok {
continue
}
name := strings.TrimPrefix(before, "[")
email := strings.TrimPrefix(strings.TrimSuffix(after, ")"), "mailto:")
email = strings.ReplaceAll(email, "[at]", "@")
authors = append(authors, author{name, email}.String())
}
}
}
if len(authors) == 0 {
log.Fatalf(`pdfgen: cannot find authors, make sure the markdown uses the correct convention:
Author(s): [FirstName LastName](mailto:email), [FirstName LastName](mailto:email)`)
}
return authors
}
func parseAbstract(b []byte) string {
content := string(b)
var ok bool
_, content, ok = strings.Cut(content, "<!--abstract-->\n")
if !ok {
goto err
}
content, _, ok = strings.Cut(content, "\n<!--more-->")
if !ok {
goto err
}
return content
err:
log.Fatal(`pdfgen: cannot find abstract, make sure the markdown uses the correct convention:
<!--abstract-->
abstract content goes here...
<!--more-->
`)
return ""
}
func parseBody(b []byte) string {
content := string(b)
var ok bool
_, content, ok = strings.Cut(content, "\n<!--more-->")
if !ok {
goto err
}
content, _, ok = strings.Cut(content, "## References")
if !ok {
goto err
}
return content
err:
log.Fatal(`pdfgen: cannot find body, make sure the markdown uses the correct convention:
<!--more-->
content body...
## References
`)
return ""
}
func parseReferences(b []byte) string {
content := string(b)
var ok bool
_, content, ok = strings.Cut(content, "## References\n")
if !ok {
log.Fatal(`pdfgen: cannot find references, make sure the markdown uses the correct convention:
## References
[^ou2022bench]: Changkun Ou. 2020. Conduct Reliable Benchmarking in Go. TalkGo Meetup. Virtual Event. March 26. https://golang.design/s/gobench
`)
}
content = strings.ReplaceAll(content, "[^", "\\bibitem{")
content = strings.ReplaceAll(content, "]:", "}")
rxStrict := xurls.Strict()
urls := rxStrict.FindAllString(content, -1)
for _, url := range urls {
content = strings.ReplaceAll(content, url, "\\url{"+url+"}")
}
return "\\begin{thebibliography}{99}" + content + "\\end{thebibliography}"
}