-
Notifications
You must be signed in to change notification settings - Fork 208
/
bookmark.go
178 lines (170 loc) · 5.09 KB
/
bookmark.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
package main
import (
"fmt"
"os"
"path/filepath"
)
func (a *App) ExtractBookmark(inFile string, outFile string, format string) error {
logger.Printf("inFile: %s, outFile: %s, format: %s\n", inFile, outFile, format)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"bookmark", "extract"}
if format != "" {
args = append(args, "--format", format)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) WriteBookmarkByFile(inFile string, outFile string, tocFile string, offset int) error {
logger.Printf("inFile: %s, outFile: %s, tocFile: %s, offset: %d\n", inFile, outFile, tocFile, offset)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
if _, err := os.Stat(tocFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"bookmark", "add"}
if tocFile != "" {
args = append(args, "--toc", tocFile)
}
if offset != 0 {
args = append(args, "--offset", fmt.Sprintf("%d", offset))
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) WriteBookmarkByGap(inFile string, outFile string, gap int, format string, startNumber int, pages string) error {
logger.Printf("inFile: %s, outFile: %s, gap: %d\n", inFile, outFile, gap)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"bookmark", "add", "--method", "gap"}
if pages != "" {
args = append(args, "--page_range", pages)
}
args = append(args, "--start-number", fmt.Sprintf("%d", startNumber))
args = append(args, "--gap", fmt.Sprintf("%d", gap))
if format != "" {
args = append(args, "--format", format)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) TransformBookmark(inFile string, outFile string, addOffset int, levelDict []string, deleteLevelBelow int, defaultLevel int, isRemoveBlankLines bool) error {
logger.Printf("inFile: %s, outFile: %s, addOffset: %d, levelDict: %v, deleteLevelBelow: %d\n", inFile, outFile, addOffset, levelDict, deleteLevelBelow)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"bookmark", "transform"}
args = append(args, "--delete-level-below", fmt.Sprintf("%d", deleteLevelBelow))
for _, level := range levelDict {
args = append(args, "--level-dict", level)
}
if addOffset != 0 {
args = append(args, "--add_offset", fmt.Sprintf("%d", addOffset))
}
args = append(args, "--default-level", fmt.Sprintf("%d", defaultLevel))
if isRemoveBlankLines {
args = append(args, "--remove-blank-lines")
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, "--toc", inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) DetectBookmarkByFont(
inFile string,
outFile string,
pages string) error {
logger.Printf("inFile: %s, outFile: %s, pages: %s\n", inFile, outFile, pages)
args := []string{"bookmark", "detect"}
if pages != "" {
args = append(args, "--page_range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) OCR(inFile string, outFile string, pages string, lang string, doubleColumn bool) error {
logger.Printf("inFile: %s, outFile: %s, pages: %s, lang: %s, doubleColumn: %v\n", inFile, outFile, pages, lang, doubleColumn)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
// path := filepath.Join(tmpDir, "ocr.py")
path, err := os.Executable()
if err != nil {
logger.Errorln(err)
return err
}
path = filepath.Join(filepath.Dir(path), "ocr.py")
args := []string{path, "ocr"}
if lang != "" {
args = append(args, "--lang", lang)
}
if doubleColumn {
args = append(args, "--use-double-column")
}
if pages != "" {
args = append(args, "--range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "python")
}
func (a *App) OCRPDFBookmark(inFile string, outFile string, pages string, lang string, doubleColumn bool) error {
logger.Printf("inFile: %s, outFile: %s, pages: %s, lang: %s, doubleColumn: %v\n", inFile, outFile, pages, lang, doubleColumn)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
// path := filepath.Join(tmpDir, "ocr.py")
path, err := os.Executable()
if err != nil {
logger.Errorln(err)
return err
}
path = filepath.Join(filepath.Dir(path), "ocr.py")
args := []string{path, "bookmark"}
if lang != "" {
args = append(args, "--lang", lang)
}
if doubleColumn {
args = append(args, "--double-column")
}
if pages != "" {
args = append(args, "--range", pages)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "python")
}