-
Notifications
You must be signed in to change notification settings - Fork 0
/
plaintext.go
64 lines (53 loc) · 1.14 KB
/
plaintext.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
package rescript
import (
"fmt"
"io"
"strings"
)
// NewPlaintextComposer creates a new composer which creates plain text output
// for a regicnition result.
func NewPlaintextComposer() ComposeFunc {
return composePlain
}
func composePlain(w io.Writer, m Metadata, r map[string]*Node) error {
var err error
sw := stringWriter{w}
// Output the title if we have one
title := m.Title
if title != "" {
_, err = sw.WriteString(strings.ToUpper(title) + "\n")
if err != nil {
return err
}
}
// Output the text body from all pages
for i, pageID := range m.PageIDs {
tail, ok := r[pageID]
if ok {
err = plaintextPage(sw, i, tail)
if err != nil {
return err
}
}
// TODO what should we do with pages w/o results?
}
return nil
}
func plaintextPage(sw io.StringWriter, idx int, n *Node) error {
var err error
_, err = sw.WriteString(fmt.Sprintf("\n[Page %d]\n\n", idx+1))
if err != nil {
return err
}
for node := n; node != nil; node = node.Next() {
_, err = sw.WriteString(node.Token().String())
if err != nil {
return err
}
}
_, err = sw.WriteString("\n")
if err != nil {
return err
}
return nil
}