-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesseract.js
More file actions
61 lines (48 loc) · 1.76 KB
/
tesseract.js
File metadata and controls
61 lines (48 loc) · 1.76 KB
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
Cu.import('resource://gre/modules/Downloads.jsm');
Cu.import('resource://gre/modules/Task.jsm');
(() => {
const nl = liberator.has('Unix') ? '\n' : '\r\n';
function tesseract(url) {
let editor_path = liberator.globalVariables.tesseract_editor_path || 'gvim';
let tesseract_path = liberator.globalVariables.tesseract_tesseract_path || 'tesseract';
let default_lang = liberator.globalVariables.tesseract_default_lang || 'eng';
commandline.input('language (default: ' + default_lang + ')',
(arg) => {
let lang = arg || default_lang;
let tempfile = io.createTempFile();
Task.spawn(function* () {
yield Downloads.fetch(url, tempfile.path);
return tempfile;
}).then((tempfile) => {
if (tempfile.exists()) {
let langs = io.system(tesseract_path + ' --list-langs')
.split(nl)
.filter((e, i, a) => (e.length == 3));
if (langs.some((e, i, a) => (lang == e))) {
let tempfile_txt = io.createTempFile();
io.run(tesseract_path, [tempfile.path, tempfile_txt.path, '-l', lang], true);
io.run(editor_path, [tempfile_txt.path + '.txt'], true);
} else {
liberator.echo('Invalid language. select from (' + langs.join(',') + ')');
}
}
});
}
);
}
function url(url) {
let uri = liberator.modules.util.newURI(url);
if (uri.host === 'pds.twimg.com') {
url += ':orig';
}
return url;
}
hints.addMode('r', 'Tesseract-OCR',
(elem, loc, count) => {
liberator.echomsg(elem.src);
tesseract(url(elem.src));
},
() => '//img[@src]'
);
})();
// vim: set et fdm=syntax ft=javascript sts=2 sw=2 ts=2 :