Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zotero pdf extraction footnote and eof page number. #526

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions chrome/content/zotfile/pdfAnnotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Zotero.ZotFile.pdfAnnotations = new function() {
att = this.pdfAttachmentsForExtraction[i].attachment,
path = this.pdfAttachmentsForExtraction[i].path,
progress = this.pdfAttachmentsForExtraction[i].itemProgress,
outputFile = path.replace('.pdf', '.txt');
outputFile = path.replace('.pdf', '.txt');
// extract annotations with poppler
yield Zotero.Utilities.Internal.exec(this.popplerExtractorPath, [path, outputFile]);
// get annotations from file and create note
Expand All @@ -114,7 +114,7 @@ Zotero.ZotFile.pdfAnnotations = new function() {
}
}
if (setProgress)
this.progressWin.startCloseTimer(Zotero.ZotFile.getPref('info_window_duration'));
this.progressWin.startCloseTimer(Zotero.ZotFile.getPref('info_window_duration'));
});

this.popplerExtractorGetAnnotationsFromFile = Zotero.Promise.coroutine(function* (path) {
Expand Down Expand Up @@ -175,7 +175,7 @@ Zotero.ZotFile.pdfAnnotations = new function() {
});

this.getColorCategory = function (r,g,b) {
// convert RGB to HSL
// convert RGB to HSL
r /= 255; g /= 255; b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
Expand All @@ -195,7 +195,7 @@ Zotero.ZotFile.pdfAnnotations = new function() {
h +=360;
}
}
// define color category based on HSL
// define color category based on HSL
if (l < 0.12) return "Black";
if (l > 0.98) return "White";
if (s < 0.2) return "Gray";
Expand All @@ -208,8 +208,9 @@ Zotero.ZotFile.pdfAnnotations = new function() {
if (h < 335) return "Magenta";
return "Red";
};

this.getNoteContent = function(annotations, item, att, method) {
//array for getting footnotes every unique page
var footnote = [];
var lib = att.library.libraryType == 'user' ? 0 : att.libraryID,
groupID = lib != 0 ? Zotero.Groups.getGroupIDFromLibraryID(att.libraryID) : undefined,
format_uri = 'zotero://open-pdf/library/items/%(key)?page=%(page)',
Expand All @@ -222,7 +223,12 @@ Zotero.ZotFile.pdfAnnotations = new function() {
format_underline = this.getPref("pdfExtraction.formatAnnotationUnderline"),
settings_colors = JSON.parse(this.getPref("pdfExtraction.colorCategories")),
setting_color_notes = this.getPref("pdfExtraction.colorNotes"),
setting_aggregate_color_highlights = this.getPref("pdfExtraction.colorAnnotations"),
//some new settings added manually
format_endofpage = this.getPref("pdfExtraction.formatEndOfPage"),
format_footnote_link = this.getPref("pdfExtraction.formatFootNoteAddIfChanged"),
setting_footnote = this.getPref("pdfExtraction.createFootnote"),
// end of manually added settings
setting_aggregate_color_highlights = this.getPref("pdfExtraction.colorAnnotations"),
cite = this.getPref("pdfExtraction.NoteFullCite") ? this.Wildcards.replaceWildcard(item, "%a %y:").replace(/_(?!.*_)/," and ").replace(/_/g,", ") : "p. ",
repl = JSON.parse(this.getPref("pdfExtraction.replacements")),
reg = repl.map(function(obj) {
Expand Down Expand Up @@ -261,12 +267,17 @@ Zotero.ZotFile.pdfAnnotations = new function() {
page = anno.page,
uri = lib == 0 ? format_uri : format_uri_group;
uri = this.Utils.str_format(uri, {'groupID': groupID, 'key': att.key, 'page': anno.page});
//Check if this is last annotation of page or last annotation
var lastofpage = false;
try{
if (page!== annotations[i+1].page) {lastofpage = true;}
} catch(err) {lastofpage = true;}
// get page
if(this.getPref("pdfExtraction.NoteTruePage")) {
try {
var itemPages = item.getField('pages');
if(itemPages) {
var page_parsed = typeof itemPages == "string" ? parseInt(itemPages.split('-')[0], 10) : itemPages;
var page_parsed = typeof itemPages == "string" ? parseInt(itemPages.split('-')[0], 10) : itemPages;
page = isNaN(page_parsed) ? page : page_parsed + page - 1;
}
}
Expand All @@ -291,15 +302,15 @@ Zotero.ZotFile.pdfAnnotations = new function() {
color_hex += "FFFFFF";
};
// add markup to note (process colour/underline markups in PDF)
if(anno.markup && anno.markup != "") {
if(anno.markup && anno.markup != "") {
var format_markup = anno.subtype == "Highlight" ? format_highlight : format_underline;
for (var k = 0; k < repl.length; k++)
anno.markup = anno.markup.replace(reg[k], repl[k].replacement);
if (!setting_color_notes && setting_aggregate_color_highlights) {
anno.markup = "<span style='background-color:rgba(" + anno.color.join(',') + ",.25)'><strong>(" + color_category + ")</strong> - " + anno.markup + "</span>";
}
var markup_formated = this.Utils.str_format(format_markup,
{'content': anno.markup, 'cite': link, 'page': page, 'uri': uri, 'label': anno.title,
var markup_formated = this.Utils.str_format(format_markup,
{'content': anno.markup, 'cite': link, 'page': page, 'uri': uri, 'label': anno.title,
'color': color, 'color_category': color_category_hex, 'color_hex': color_hex, 'color_category_name': color_category,
'group': groupID, 'key': att.key});
if(!setting_color_notes)
Expand All @@ -314,10 +325,10 @@ Zotero.ZotFile.pdfAnnotations = new function() {
}
// add to note text (process notes added to PDF)
if(anno.content && anno.content != "" &&
(!anno.markup || this.Utils.strDistance(anno.content,anno.markup)>0.15 )) {
(!anno.markup || this.Utils.strDistance(anno.content,anno.markup)>0.15 )) {
var content = anno.content.replace(/(\r\n|\n|\r)/gm,"<br>");
// '<p><i>%(content) (<a href="%(uri)">note on p.%(page)</a>)</i></p><br>'
var content_formated = this.Utils.str_format(format_note,
var content_formated = this.Utils.str_format(format_note,
{'content': content, 'cite': link, 'page': page, 'uri': uri, 'label': anno.title,
'color': color, 'color_category': color_category_hex, 'color_hex': color_hex, 'color_category_name': color_category,
'group': groupID, 'key': att.key});
Expand All @@ -331,7 +342,24 @@ Zotero.ZotFile.pdfAnnotations = new function() {
note[color_category] += content_formated;
}
}
}
//add End of page note
if (lastofpage){
var endofpage_formatted = this.Utils.str_format(format_endofpage,
{'content': anno.markup, 'cite': link, 'page': page, 'uri': uri, 'label': anno.title,
'color': color, 'color_category': color_category_hex, 'color_hex': color_hex, 'color_category_name': color_category,
'group': groupID, 'key': att.key});
note += endofpage_formatted
}
// populate footnote
if (setting_footnote){
var footnote_link_formated = this.Utils.str_format(format_footnote_link,
{'content': anno.markup, 'cite': link, 'page': page, 'uri': uri, 'label': anno.title,
'color': color, 'color_category': color_category_hex, 'color_hex': color_hex, 'color_category_name': color_category,
'group': groupID, 'key': att.key});
if (footnote.indexOf(footnote_link_formated)=== -1){footnote.push(footnote_link_formated);}
}
}// add footnote ad last
if (setting_footnote){note += "<hr><p>"+ footnote.join("<br>")+"/<p>";}
return note;
}.bind(Zotero.ZotFile);

Expand All @@ -352,7 +380,7 @@ Zotero.ZotFile.pdfAnnotations = new function() {
args.callbackObj = this;
args.callback = this.extractionComplete;
Zotero.ZotFile.PdfExtractor.extractAnnotations(args);
};
};

/** Keypress listener that cancels the extraction if the user presses escape. */
this.cancellationListener = function(keyEvent) {
Expand Down
3 changes: 3 additions & 0 deletions defaults/preferences/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,7 @@ pref("extensions.zotfile.pdfExtraction.localeDateInNote", true);
pref("extensions.zotfile.pdfExtraction.colorNotes", false);
pref("extensions.zotfile.pdfExtraction.colorAnnotations", false);
pref("extensions.zotfile.pdfExtraction.colorCategories", '{"Black": "#000000", "White": "#FFFFFF", "Gray": "#808080", "Red": "#FF0000", "Orange": "#FFA500", "Yellow": "#FFFF00", "Green": "#00FF00", "Cyan": "#00FFFF", "Blue": "#0000FF", "Magenta": "#FF00FF"}');
pref("extensions.zotfile.pdfExtraction.formatEndOfPage","<p><b>(page: %(page))</b></p>");
pref("extensions.zotfile.pdfExtraction.formatFootNoteAddIfChanged", "(page: %(page)): %(uri)");
pref("extensions.zotfile.pdfExtraction.createFootnote", false);
//pref("extensions.zotfile.pdfExtraction.format", '"<p>%(markup)s" %(cite)s</p><br><p>%(note)s</p><br>');