/
export.go
73 lines (62 loc) · 1.74 KB
/
export.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
package main
import (
"bytes"
"errors"
"os"
"os/exec"
"strings"
"github.com/xyproto/files"
"github.com/xyproto/vt100"
)
// exportScdoc tries to export the current document as a manual page, using scdoc
func (e *Editor) exportScdoc(manFilename string) error {
scdoc := exec.Command("scdoc")
// Place the current contents in a buffer, and feed it to stdin to the command
var buf bytes.Buffer
buf.WriteString(e.String())
scdoc.Stdin = &buf
// Create a new file and use it as stdout
manpageFile, err := os.Create(manFilename)
if err != nil {
return err
}
var errBuf bytes.Buffer
scdoc.Stdout = manpageFile
scdoc.Stderr = &errBuf
// Save the command in a temporary file
saveCommand(scdoc)
// Run scdoc
if err := scdoc.Run(); err != nil {
errorMessage := strings.TrimSpace(errBuf.String())
if len(errorMessage) > 0 {
return errors.New(errorMessage)
}
return err
}
return nil
}
// exportAdoc tries to export the current document as a manual page, using asciidoctor
func (e *Editor) exportAdoc(c *vt100.Canvas, tty *vt100.TTY, manFilename string) error {
// TODO: Use a proper function for generating temporary files
tmpfn := "___o___.adoc"
if files.Exists(tmpfn) {
return errors.New(tmpfn + " already exists, please remove it")
}
// TODO: Write a SaveAs function for the Editor
oldFilename := e.filename
e.filename = tmpfn
err := e.Save(c, tty)
if err != nil {
e.filename = oldFilename
return err
}
e.filename = oldFilename
// Run asciidoctor
adocCommand := exec.Command("asciidoctor", "-b", "manpage", "-o", manFilename, tmpfn)
saveCommand(adocCommand)
if err = adocCommand.Run(); err != nil {
_ = os.Remove(tmpfn) // Try removing the temporary filename if pandoc fails
return err
}
return os.Remove(tmpfn)
}