-
Notifications
You must be signed in to change notification settings - Fork 2
/
pandoc.go
92 lines (80 loc) · 2.46 KB
/
pandoc.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
package main
// pandoc.go adds pandoc support for converting listings to PDF format.
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
)
// pdfWriter transforms the listing to a PDF. First, the markdown is
// written to a temporary file (which is removed when the function
// returns); this temporary file is then passed to pandoc for conversion.
func pdfWriter(markdown, filename string, args []string) (err error) {
tmp, err := ioutil.TempFile("", "literate_pandoc")
if err != nil {
return
}
defer tmp.Close()
defer os.Remove(tmp.Name())
_, err = tmp.Write([]byte(markdown))
if err != nil {
return
}
tmptpl, err := ioutil.TempFile("", "literate_pandoc")
if err != nil {
return
}
tempName := tmptpl.Name() + ".latex"
tmptpl.Close()
os.Remove(tmptpl.Name())
defer os.Remove(tempName)
err = ioutil.WriteFile(tempName, []byte(ltxTemplate), 0644)
if err != nil {
return
}
outFile := GetOutFile(filename + ".pdf")
args = append(args, "-o")
args = append(args, outFile)
args = append(args, "--listing")
args = append(args, "--template")
args = append(args, tmptpl.Name())
args = append(args, tmp.Name())
pandoc := exec.Command("pandoc", args...)
pandocOut, err := pandoc.CombinedOutput()
if err != nil {
fmt.Printf("[!] pandoc: '%s'\n", string(pandocOut))
}
return
}
// PdfWriter transforms the listing to a PDF. First, the markdown is
// written to a temporary file (which is removed when the function
// returns); this temporary file is then passed to pandoc for
// conversion. It is a wrapper around pdfWriter for non-unified
// output.
func PdfWriter(markdown, filename string) (err error) {
return pdfWriter(markdown, filename, nil)
}
// PdfWriter transforms the listing to a PDF. First, the markdown is
// written to a temporary file (which is removed when the function
// returns); this temporary file is then passed to pandoc for conversion.
func UnifiedPdfWriter(markdown, filename string) (err error) {
return pdfWriter(markdown, filename, []string{"-V", "documentclass=book", "--toc"})
}
// PandocTexWriter uses pandoc to convert the markdown output to a
// TeX file.
func PandocTexWriter(markdown, filename string) (err error) {
tmp, err := ioutil.TempFile("", "literate_pandoc")
if err != nil {
return
}
defer tmp.Close()
defer os.Remove(tmp.Name())
_, err = tmp.Write([]byte(markdown))
if err != nil {
return
}
outFile := GetOutFile(filename + ".ltx")
pandoc := exec.Command("pandoc", "-s", "-o", outFile, tmp.Name())
err = pandoc.Run()
return
}