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
|
package document
import (
"github.com/scrotadamus/ghligh/go-poppler"
"os"
"sync"
"strings"
"fmt"
)
const ghlighFilter = "ghligh-Y2lhbm5v:"
// This is different from poppler's annot_mapping
// it is the list of annotations mapped to the page index
type AnnotsMap map[int][]AnnotJSON
type GhlighDoc struct {
doc *poppler.Document
mu sync.Mutex
Path string `json:"file"`
HashBuffer string `json:"hash"`
AnnotsBuffer AnnotsMap `json:"highlights,omitempty"`
}
type HighlightedText struct {
Page int `json:"page"`
Text string `json:"text"`
Contents string `json:"contents,omitempty"`
}
func Open(filename string) (*GhlighDoc, error) {
var err error
g := &GhlighDoc{}
g.doc, err = poppler.Open(filename)
if err != nil {
fmt.Errorf("%s: error opening pdf %v", os.Args[0], err)
return nil, err
}
g.Path = filename
// HashDoc??
return g, nil
}
func (d *GhlighDoc) Close() {
d.AnnotsBuffer = nil
d.HashBuffer = ""
if d.doc != nil {
d.doc.Close()
}
}
func (d *GhlighDoc) Info() poppler.DocumentInfo {
return d.doc.Info()
}
func (d *GhlighDoc) tagExists(text string) bool {
for _, tag := range d.GetTags() {
if tag == text {
return true
}
}
return false
}
func (d *GhlighDoc) Tag(text string) {
if !d.tagExists(text) {
d.doc.Tag(ghlighFilter + text)
} else {
fmt.Fprintf(os.Stderr, "warning: tag %s already exist inside %s, i don't do anything\n", text, d.Path)
}
}
func (d *GhlighDoc) GetTags() []string {
var tags []string
annots := d.doc.GetTags(ghlighFilter)
for _, annot := range annots {
contents := strings.TrimPrefix(annot.Contents(), ghlighFilter)
tags = append(tags, contents)
}
return tags
}
func (d *GhlighDoc) RemoveTags(tags []string) int {
zeroPage := d.doc.GetPage(0)
var removedTags int
annots := d.doc.GetTags(ghlighFilter)
for _, annot := range annots {
contents := strings.TrimPrefix(annot.Contents(), ghlighFilter)
for _, tag := range tags {
if tag == contents {
zeroPage.RemoveAnnot(*annot)
removedTags += 1
break
}
}
}
return removedTags
}
func (d *GhlighDoc) Import(annotsMap AnnotsMap) (int, error) {
d.mu.Lock()
defer d.mu.Unlock()
annots_count := 0
var err error
d.AnnotsBuffer = annotsMap
for key := range d.AnnotsBuffer {
page := d.doc.GetPage(key)
for _, annot := range d.AnnotsBuffer[key] {
a := d.jsonToAnnot(annot)
if !isInPage(a, page) {
annots_count += 1
page.AddAnnot(*a)
}
}
page.Close()
}
d.AnnotsBuffer = nil
return annots_count, err
}
func integrityCheck(tizio *GhlighDoc, caio *GhlighDoc) {
}
func (d *GhlighDoc) GetNPages() int {
return d.doc.GetNPages()
}
func (d *GhlighDoc) GetPageText(i int) (string, error) {
nPages := d.doc.GetNPages()
if i < 0 || i > nPages {
return "", fmt.Errorf("error page %d out of range %d", i, nPages)
}
p := d.doc.GetPage(i)
defer p.Close()
text := p.Text()
return text, nil
}
func (d *GhlighDoc) Save() (bool, error) {
d.mu.Lock()
defer d.mu.Unlock()
tempFile, err := os.CreateTemp("", ".ghligh_*.pdf")
if err != nil {
return false, err
}
defer os.Remove(tempFile.Name())
ok, err := d.doc.Save(tempFile.Name())
if !ok {
return false, err
}
/* integrity check */
newDoc, err := Open(tempFile.Name())
if err != nil {
return false, err
}
if newDoc.HashDoc() != d.HashDoc() {
return false, fmt.Errorf("After saving document %s to %s its hash doesn't correspond the the old one", d.Path, tempFile.Name())
}
err = os.Rename(tempFile.Name(), d.Path)
if err != nil {
return false, err
}
return true, nil
}
func (d *GhlighDoc) Cat() []HighlightedText {
var highlights []HighlightedText
n_pages := d.doc.GetNPages()
for i := 0; i < n_pages; i++ {
page := d.doc.GetPage(i)
annots := page.GetAnnots()
for _, annot := range annots {
if annot.Type() == poppler.AnnotHighlight {
annotText := page.AnnotText(*annot)
highlights = append(highlights, HighlightedText{Page: i, Text: annotText, Contents: annot.Contents()})
}
}
page.Close()
}
return highlights
}
func (d *GhlighDoc) HasHighlights() bool {
// check if is tagged with ls
if d.tagExists("ls") {
return true
}
// check if it has highlights
n_pages := d.doc.GetNPages()
for i := 0; i < n_pages; i++ {
page := d.doc.GetPage(i)
annots := page.GetAnnots()
for _, annot := range annots {
if annot.Type() == poppler.AnnotHighlight {
return true
}
}
page.Close()
}
return false
}
func (d *GhlighDoc) GetAnnotsBuffer() AnnotsMap {
annots_json_of_page := make(AnnotsMap)
n := d.doc.GetNPages()
var annots_json []AnnotJSON
for i := 0; i < n; i++ {
annots_json = nil
page := d.doc.GetPage(i)
annots := page.GetAnnots()
for _, annot := range annots {
if annot.Type() == poppler.AnnotHighlight {
annot_json := annotToJson(*annot)
annots_json = append(annots_json, annot_json)
}
}
page.Close()
if len(annots_json) > 0 {
annots_json_of_page[i] = annots_json
}
}
return annots_json_of_page
}
|