-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (108 loc) · 3.88 KB
/
index.js
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
var pdfjs = require('pdfjs-dist-for-node/build/pdf.combined.js')
var altmetrics = require('beagle-altmetrics')
var _ = require('lodash')
var doiRegex = require('doi-regex')
var response
var altmetricsFromDoi = function (doi, cb) {
response = true
return altmetrics.getDataFromDoi(doi, function (err, data) {
if (err !== null) {
console.error(err)
// TODO What does process do?
// process.exit(-1)
}
return cb(null, data)
})
}
var getFingerprint = function (documentObject, cb) {
if (!documentObject) throw new Error('No pdf provided')
pdfjs.getDocument(documentObject).then(function (pdf) {
return cb(null, pdf.fingerprint)
}).catch(function (err) {
return cb(err)
})
}
var getMetadata = function (documentObject, cb) {
if (!documentObject) throw new Error('No pdf provided')
pdfjs.getDocument(documentObject).then(function (pdf) {
return pdf.getMetadata()
}).then(function (data) {
return cb(null, data)
}).catch(function (err) {
// Unable to get Document or metadata
return cb(err)
})
}
var readPDFText = function (documentObject, options, cb) {
if (!documentObject) throw new Error('No pdf provided')
pdfjs.getDocument(documentObject).then(function (pdf) {
var numPages = pdf.numPages
// Define vars where they won't be redefined in each loop in if statements
var doi
var match
var j = 0
for (var i = 1; i <= numPages; i++) {
pdf.getPage(i).then(function (page) {
return page.getTextContent()
}).then(function (textContent) {
_.each(textContent.items, function (item) {
// TODO match[2] tracks .t001, .g001, etc. Capture these, they may be relevant
// to research.
match = doiRegex.groups(item.str)
if (match && !doi) {
// Only call once, for now. TODO Multiple DOIs
doi = match[1]
if (options.modules.altmetrics) {
altmetricsFromDoi(doi, cb)
}
}
})
j++
if (j === numPages && !doi) {
cb('Failed to find a DOI.')
}
}).catch(function (err) {
return cb(err)
})
}
}).catch(function (err) {
// Unable to get document
return cb(err)
})
}
var getHightlightCoords = function () {
var pageIndex = window.PDFViewerApplication.pdfViewer.currentPageNumber - 1
var page = window.PDFViewerApplication.pdfViewer.pages[pageIndex]
var pageRect = page.canvas.getClientRects()[0]
var selectionRects = window.getSelection().getRangeAt(0).getClientRects()
var viewport = page.viewport
var selected = _.map(selectionRects, function (r) {
return viewport.convertToPdfPoint(r.left - pageRect.left, r.top - pageRect.top).concat(
viewport.convertToPdfPoint(r.right - pageRect.left, r.bottom - pageRect.top))
})
return {page: pageIndex, coords: selected}
}
var showHighlight = function (selected, cb) {
var pageIndex = selected.page
if (pageIndex === window.PDFViewerApplication.pdfViewer._currentPageNumber - 1) {
var page = window.PDFViewerApplication.pdfViewer.pages[pageIndex]
var pageElement = page.canvas.parentElement
var viewport = page.viewport
selected.coords.forEach(function (rect) {
var bounds = viewport.convertToViewportRectangle(rect)
var el = document.createElement('div')
el.setAttribute('style', 'position: absolute; background-color: rgba(238, 170, 0, .2);' +
'left:' + Math.min(bounds[0], bounds[2]) + 'px; top:' + Math.min(bounds[1], bounds[3]) + 'px;' +
'width:' + Math.abs(bounds[0] - bounds[2]) + 'px; height:' + Math.abs(bounds[1] - bounds[3]) + 'px;')
pageElement.appendChild(el)
return cb && cb()
})
}
return
}
exports.pdfjs = pdfjs
exports.getFingerprint = getFingerprint
exports.getMetadata = getMetadata
exports.readPDFText = readPDFText
exports.getHightlightCoords = getHightlightCoords
exports.showHighlight = showHighlight