/
compile.go
300 lines (241 loc) · 6.42 KB
/
compile.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"crypto/sha1"
"fmt"
"github.com/PuerkitoBio/goquery"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
var extensions = []string{
"aux",
"4ct",
"4tc",
"oc",
"md5",
"dpth",
"out",
"jax",
"idv",
"lg",
"tmp",
"xref",
"log",
"auxlock",
"dvi",
"pdf",
"scmd",
"sout",
}
func clean(filename string) {
for _, extension := range extensions {
f := strings.TrimSuffix(filename, filepath.Ext(filename)) + "." + extension
os.Remove(f)
}
}
func pdflatex(filename string) ([]byte, error) {
cmdName := "pdflatex"
tikzexport := "\"\\PassOptionsToClass{tikzexport}{ximera}\\PassOptionsToClass{xake}{ximera}\\PassOptionsToClass{xake}{xourse}\\nonstopmode\\input{" + filepath.Base(filename) + "}\""
cmdArgs := []string{"-file-line-error", "-shell-escape", tikzexport}
cmd := exec.Command(cmdName, cmdArgs...)
cmd.Dir = filepath.Dir(filename)
cmdOut, err := cmd.Output()
return cmdOut, err
}
func htlatex(filename string) ([]byte, error) {
cmdName := "htlatex"
cmdArgs := []string{filepath.Base(filename), "ximera,charset=utf-8,-css", " -cunihtf -utf8", "", "--interaction=nonstopmode -shell-escape -file-line-error"}
cmd := exec.Command(cmdName, cmdArgs...)
cmd.Dir = filepath.Dir(filename)
cmdOut, err := cmd.Output()
return cmdOut, err
}
func sage(filename string) ([]byte, error) {
cmdName := "sage"
cmdArgs := []string{filepath.Base(filename)}
cmd := exec.Command(cmdName, cmdArgs...)
cmd.Dir = filepath.Dir(filename)
cmdOut, err := cmd.Output()
return cmdOut, err
}
func FindLabelAnchorsInHtml(htmlFilename string) ([]string, error) {
var ids []string
f, err := os.Open(htmlFilename)
defer f.Close()
if err != nil {
return ids, err
}
doc, err := goquery.NewDocumentFromReader(f)
if err != nil {
return ids, err
}
doc.Find("a.ximera-label").Each(func(i int, s *goquery.Selection) {
id, exists := s.Attr("id")
if exists {
ids = append(ids, id)
}
})
return ids, err
}
func readTitleAndAbstract(htmlFilename string) (title string, abstract string, err error) {
f, err := os.Open(htmlFilename)
defer f.Close()
if err != nil {
return
}
doc, err := goquery.NewDocumentFromReader(f)
if err != nil {
return
}
title = doc.Find("title").Contents().Text()
doc.Find("div.abstract").Each(func(i int, s *goquery.Selection) {
var html string
html, err = s.Html()
if err == nil {
abstract = html
}
})
doc.Find("div.abstract p").Each(func(i int, s *goquery.Selection) {
var html string
html, err = s.Html()
if err == nil {
abstract = html
}
})
return
}
func transformXourse(directory string, filename string, doc *goquery.Document) {
log.Debug("Transforming xourse file " + filename)
log.Debug("Remove the anchor links that htlatex is inserting")
doc.Find("a").Each(func(_ int, s *goquery.Selection) {
_, exists := s.Attr("id")
if exists {
s.Remove()
}
})
log.Debug("Normalize the activity links")
doc.Find("a.activity").Each(func(_ int, s *goquery.Selection) {
href, exists := s.Attr("href")
if exists {
// BADBAD: do I need this?
texActivityFilename := filepath.Clean(filepath.Join(filepath.Dir(filename), href))
activityFilename := strings.TrimSuffix(texActivityFilename, ".tex") + ".html"
// Unfortunately xourse files links are relative to repo root
href, _ = filepath.Rel(directory, texActivityFilename)
href = strings.TrimSuffix(href, ".tex")
s.SetAttr("href", href)
log.Debug("Reading title and abstract from " + activityFilename)
title, abstract, err := readTitleAndAbstract(activityFilename)
if err == nil {
s.AppendHtml("<h2>" + title + "</h2><h3>" + abstract + "</h3>")
}
}
})
return
}
func transformHtml(directory string, filename string) error {
htmlFilename := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".html"
f, err := os.Open(htmlFilename)
defer f.Close()
if err != nil {
return err
}
doc, err := goquery.NewDocumentFromReader(f)
if err != nil {
return err
}
xourseFile := false
doc.Find("meta[name=\"description\"]").Each(func(i int, s *goquery.Selection) {
content, exists := s.Attr("content")
if !exists {
log.Warn(htmlFilename + " is missing a content attribute on its meta[name=\"description\"]")
}
if content == "xourse" {
xourseFile = true
}
})
log.Debug("Remove empty paragraphs of the form <p></p>")
doc.Find("p:empty").Each(func(i int, s *goquery.Selection) {
s.Remove()
})
log.Debug("Add <meta> tags for all dependencies")
doc.Find("head").Each(func(_ int, s *goquery.Selection) {
dependencies, err := LatexDependencies(filename)
if err == nil {
// BADBAD: this does the wrong thing with xake compile
dependencies = append(dependencies, filename)
for _, dependency := range dependencies {
dependency, err := filepath.Rel(directory, dependency)
if err != nil {
continue
}
f, err := os.Open(dependency)
defer f.Close()
if err != nil {
continue
}
h := sha1.New()
if _, err := io.Copy(h, f); err == nil {
hash := fmt.Sprintf("%x", h.Sum(nil))
s.AppendHtml("<meta name=\"dependency\" content=\"" +
hash + " " +
dependency + "\">")
}
}
}
})
if xourseFile {
transformXourse(directory, filename, doc)
}
html, err := doc.Html()
if err != nil {
return err
}
fileInfo, err := os.Stat(htmlFilename)
if err != nil {
return err
}
err = ioutil.WriteFile(htmlFilename, []byte(html), fileInfo.Mode())
if err != nil {
return err
}
return nil
}
func Compile(directory string, filename string) ([]byte, error) {
log.Debug("Cleaning files associated with " + filename)
clean(filename)
log.Debug("Running pdflatex for " + filename)
output, err := pdflatex(filename)
if err != nil {
log.Error(string(output))
return output, err
}
sagetexFilename := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".sagetex.sage"
if _, err := os.Stat(sagetexFilename); !os.IsNotExist(err) {
log.Debug("Running sage for " + filename)
sage(sagetexFilename)
}
log.Debug("Running pdflatex again for " + filename)
output, err = pdflatex(filename)
if err != nil {
log.Error(err)
log.Error(string(output))
return output, err
}
log.Debug("Running htlatex on " + filename)
output, err = htlatex(filename)
if err != nil {
log.Error(err)
log.Error(string(output))
return output, err
}
log.Debug("Applying HTML transformations for " + filename)
err = transformHtml(directory, filename)
if err != nil {
return []byte{}, err
}
return []byte{}, nil
}