Skip to content

Commit

Permalink
support for group libraries + unattached PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
madturtles committed Aug 10, 2024
1 parent 83af7d6 commit b61ca1f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
19 changes: 14 additions & 5 deletions src/chrome/content/zoteroocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ Zotero.OCR = new function() {
if (item.isAttachment()) {
if (item.isFileAttachment() && item.attachmentContentType == 'application/pdf') {
pdfItem = item;
// if the PDF has no parent item, there is no reasonable place to attach the output files
// => create an empty parent item to keep things tidy
if (pdfItem.isTopLevelItem()) {
await Zotero.getActiveZoteroPane().createEmptyParent(pdfItem);
}
item = Zotero.Items.get(item.parentItemID);
}
else {
Expand Down Expand Up @@ -179,10 +184,10 @@ Zotero.OCR = new function() {
let newNote = new Zotero.Item('note');
newNote.setNote(contents);
newNote.parentID = item.id;
newNote.libraryID = item.libraryID;
yield newNote.saveTx();
}


if (Zotero.Prefs.get("zoteroocr.outputHocr")) {
let contents = yield Zotero.File.getContentsAsync(ocrbase + '.hocr');
// replace the absolute paths of images with relative ones
Expand All @@ -204,19 +209,23 @@ Zotero.OCR = new function() {
let htmlfile = Zotero.File.pathToFile(OS.Path.join(dir, pagename));
let pagecontent = preamble + "<div class='ocr_page'" + parts[i] + '<script src="https://unpkg.com/hocrjs"></script>\n</body>\n</html>';
Zotero.File.putContents(htmlfile, pagecontent);
yield Zotero.Attachments.linkFromFile({
// Zotero.Attachments.importFromFile() works in group libraries, linkFromFile() did not
await Zotero.Attachments.importFromFile({
file: OS.Path.join(dir, pagename),
contentType: "text/html",
parentItemID: item.id
parentItemID: item.id,
libraryID: item.libraryID
});
}
}

// attach PDF if it is a new one
if (Zotero.Prefs.get("zoteroocr.outputPDF") && !(Zotero.Prefs.get("zoteroocr.overwritePDF"))) {
yield Zotero.Attachments.linkFromFile({
// Zotero.Attachments.importFromFile() works in group libraries, linkFromFile() did not
await Zotero.Attachments.importFromFile({
file: ocrbase + '.pdf',
parentItemID: item.id
parentItemID: item.id,
libraryID: item.libraryID
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
RDF:about="urn:mozilla:install-manifest"
em:id="[email protected]"
em:name="Zotero OCR"
em:version="0.7.3"
em:version="0.8.0"
em:iconURL="chrome://zoteroocr/skin/ubmannheim.png"
em:icon64URL="chrome://zoteroocr/skin/ubmannheim64x64.png"
em:creator="Philipp Zumstein"
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Zotero OCR",
"version": "0.7.3",
"version": "0.8.0",
"description": "Zotero Plugin for OCR",
"author": "Philipp Zumstein",
"homepage_url": "https://github.com/UB-Mannheim/zotero-ocr",
Expand Down
19 changes: 15 additions & 4 deletions src/zotero-ocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ ZoteroOCR = {
if (item.isAttachment()) {
if (item.isFileAttachment() && item.attachmentContentType == 'application/pdf') {
pdfItem = item;
// if the PDF has no parent item, there is no reasonable place to attach the output files
// => create an empty parent item to keep things tidy
if (pdfItem.isTopLevelItem()) {
await Zotero.getActiveZoteroPane().createEmptyParent(pdfItem);
}
item = Zotero.Items.get(item.parentItemID);
}
else {
Expand All @@ -176,6 +181,7 @@ ZoteroOCR = {
}
pdfItem = pdfAttachments[0];
}

let pdf = pdfItem.getFilePath();
let base = pdf.replace(/\.pdf$/, '');
let dir = PathUtils.parent(pdf);
Expand Down Expand Up @@ -242,6 +248,7 @@ ZoteroOCR = {
let newNote = new Zotero.Item('note');
newNote.setNote(contents);
newNote.parentID = item.id;
newNote.libraryID = item.libraryID;
await newNote.saveTx();
}

Expand All @@ -266,19 +273,23 @@ ZoteroOCR = {
let htmlfile = Zotero.File.pathToFile(PathUtils.join(dir, pagename));
let pagecontent = preamble + "<div class='ocr_page'" + parts[i] + '<script src="https://unpkg.com/hocrjs"></script>\n</body>\n</html>';
Zotero.File.putContents(htmlfile, pagecontent);
await Zotero.Attachments.linkFromFile({
// Zotero.Attachments.importFromFile() works in group libraries, linkFromFile() did not
await Zotero.Attachments.importFromFile({
file: PathUtils.join(dir, pagename),
contentType: "text/html",
parentItemID: item.id
parentItemID: item.id,
libraryID: item.libraryID
});
}
}

// attach PDF if it is a new one
if (Zotero.Prefs.get("zoteroocr.outputPDF") && !(Zotero.Prefs.get("zoteroocr.overwritePDF"))) {
await Zotero.Attachments.linkFromFile({
// Zotero.Attachments.importFromFile() works in group libraries, linkFromFile() did not
await Zotero.Attachments.importFromFile({
file: ocrbase + '.pdf',
parentItemID: item.id
parentItemID: item.id,
libraryID: item.libraryID
});
}

Expand Down
6 changes: 3 additions & 3 deletions update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"[email protected]": {
"updates": [
{
"version": "0.7.3",
"update_link": "https://github.com/UB-Mannheim/zotero-ocr/releases/download/0.7.3/zotero-ocr-0.7.3.xpi",
"update_hash": "sha256:eee7dd695a551d4255d2bf4284026c95cf1048e3271d50a9591eaa885e4f6c54",
"version": "0.8.0",
"update_link": "https://github.com/UB-Mannheim/zotero-ocr/releases/download/0.8.0/zotero-ocr-0.8.0.xpi",
"update_hash": "sha256:5022b54281e6eb14421b9d6dc0d273f026e4edb6a1fdf4609c00fa55b13e5420",
"applications": {
"gecko": {
"strict_min_version": "60.0"
Expand Down
6 changes: 3 additions & 3 deletions updates.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"[email protected]": {
"updates": [
{
"version": "0.7.3",
"update_link": "https://github.com/UB-Mannheim/zotero-ocr/releases/download/0.7.3/zotero-ocr-0.7.3.xpi",
"update_hash": "sha256:eee7dd695a551d4255d2bf4284026c95cf1048e3271d50a9591eaa885e4f6c54",
"version": "0.8.0",
"update_link": "https://github.com/UB-Mannheim/zotero-ocr/releases/download/0.8.0/zotero-ocr-0.8.0.xpi",
"update_hash": "sha256:5022b54281e6eb14421b9d6dc0d273f026e4edb6a1fdf4609c00fa55b13e5420",
"applications": {
"gecko": {
"strict_min_version": "60.0"
Expand Down

0 comments on commit b61ca1f

Please sign in to comment.