Skip to content

I reviewed my old changes and merged them into the current branch #79

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

Open
wants to merge 23 commits into
base: beta
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

# Personal
/.personal
/init/00-script-definitions.js
/init/00-script-definitions.js
logs
3 changes: 2 additions & 1 deletion Makefile.UNIX
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ all: Makefile.in
RELEASE:=$(shell grep version manifest.json | sed '2q;d' | sed -e 's/^ *"version": "//' -e 's/",//')

zotmoov.xpi: FORCE
rm -rf $@
python3 script_definition_builder.py
zip -FSr $@ bootstrap.js locale manifest.json prefs.js chrome preferences src lib init -x \*.DS_Store

zotmoov-%-fx.xpi: zotmoov.xpi
rm -rf $@
mv $< $@

Makefile.in: manifest.json
Expand Down
2 changes: 1 addition & 1 deletion README_DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ In order for you to be able to contribute to this plugin, you need:

# Building the plugin

First, before building, and even before running Zotero in dev mode for the plugin (directly from source) you NEED to run `script_definition_builder.py`.
First, before building, and even before running Zotero in dev mode for the plugin (directly from source) you NEED to run `script_definition_builder.py`, located in the `builder` folder.
It automatically builds a file called `00-script-definitions.js` in the `init` folder, which is necessary for Zotero to load our plugin's code.

Mr. Hoorn wrote this builder in Python so that we don't have to hardcode every source file.
Expand Down
27 changes: 16 additions & 11 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let zotmoov = null;
let zotmoovMenus = null;
let zotmoovBindings = null;
let chromeHandle = null;
let firstLoad = true;

function log(msg)
{
Expand All @@ -28,25 +29,29 @@ async function install()

async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
{
// Only ones we need to load directly here
Services.scriptloader.loadSubScript(rootURI + 'init/00-script-definitions.js');
Services.scriptloader.loadSubScript(rootURI + 'init/01-script-loader.js');
if (firstLoad)
{
// Only ones we need to load directly here
Services.scriptloader.loadSubScript(rootURI + 'init/00-script-definitions.js', this);
Services.scriptloader.loadSubScript(rootURI + 'init/01-script-loader.js', this);

let scriptPaths = new ScriptDefinitions().getScriptPaths();
let scriptLoader = new ScriptLoader(rootURI);

let scriptPaths = new ScriptDefinitions().getScriptPaths();
let scriptLoader = new ScriptLoader(rootURI);
await scriptLoader.loadScripts(scriptPaths, this);

await scriptLoader.loadScripts(scriptPaths);
firstLoad = false;
}

const directoryManager = new DirectoryManager();
const outputManager = new OutputManager(directoryManager);
const zotmoovDebugger = new ZotMoovDebugger('ZotMoov', outputManager);

const sanitizer = new Sanitizer();
const zotmoovWildcard = new ZotMoovWildcard(sanitizer, ZotMoovCWParser);
const sanitizer = new FileNameSanitizer();

zotmoov = new ZotMoov(id, version, rootURI, zotmoovWildcard, sanitizer, zotmoovDebugger);
zotmoovBindings = new ZotMoovBindings(zotmoov);
zotmoovMenus = new ZotMoovMenus(zotmoov, zotmoovBindings, ZotMoovCMUParser);
const zotmoov = new ZotMoov(id, version, sanitizer, zotmoovDebugger);
const zotmoovBindings = new ZotMoovBindings(zotmoov);
const zotmoovMenus = new ZotMoovMenus(zotmoov, zotmoovBindings, ZotMoovCMUParser);

Zotero.PreferencePanes.register(
{
Expand Down
2 changes: 1 addition & 1 deletion init/01-script-loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ScriptLoader = class {
class ScriptLoader {
constructor(rootURI) {
this.rootURI = rootURI;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/00-zotmoov-debugger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Class representing a Zotmoov Debugger.
*/
var ZotMoovDebugger = class {
class ZotMoovDebugger {
/**
* Constructor for the given class.
*
* @param {string} prefix - The prefix to be appended to log messages.
* @param {object} outputManager - The output manager object.
* @param {IOutputManager} outputManager - The output manager object.
* @remarks Please don't add readability punctuation to the prefix...
* Do not add ':' to the prefix, or '-' that's all taken care of.
*/
Expand All @@ -23,6 +23,9 @@ var ZotMoovDebugger = class {
this.outputManager.write('', true, true);
}

/**
* @private
*/
_log(type, message) {
const now = new Date();
const timestamp = now.toLocaleString() + '.' + ('00' + (now.getMilliseconds())).slice(-3);
Expand Down Expand Up @@ -50,16 +53,16 @@ var ZotMoovDebugger = class {

static getLatestMethod() {
const stackLines = new Error().stack.split('\n');
if (stackLines.length > 3) {
const latestMethod = stackLines[3].trim();
if (stackLines.length > 4) {
const latestMethod = stackLines[4].trim();
return ` -> ${latestMethod}`;
}
return 'Stack trace unavailable';
}

static getStack() {
const stackLines = new Error().stack.split('\n');
let stack = `Last method: ${stackLines[3].trim()}\n`;
let stack = `Last method: ${stackLines[4].trim()}\n`;
for (let i = 4; i < stackLines.length - 1; i++) {
stack += ` -> ${stackLines[i].trim()}\n`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Sanitizer = class {
class FileNameSanitizer {
// Modified from https://github.com/parshap/node-sanitize-filename

_truncate(str, nchars) {
Expand Down
71 changes: 71 additions & 0 deletions lib/02-title-cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class TitleCleaner {
/**
* Truncates the title based on a plethora of rules.
* @param max_title_length {int} The max number of characters for a title.
*/
constructor(max_title_length = 200) {
this.max_title_length = max_title_length;
}

// Note from Hoorn: Why do we not use the sanitizer? What is the difference?
truncateTitle(title) {
let cleanedTitle = this.replaceForbiddenCharacters(title);
cleanedTitle = this.truncateAtFirstSpecialCharacter(cleanedTitle);

if (cleanedTitle.length > this.max_title_length) {
cleanedTitle = cleanedTitle.substring(0, this.max_title_length);
cleanedTitle = this.trimTruncatedTitle(cleanedTitle);

return cleanedTitle;
}

cleanedTitle = this.removeEndingSpecialChars(cleanedTitle);

return cleanedTitle;
}

removeEndingSpecialChars(title) {
const specialChars = [':', '?', '.', '/', '\\', '>', '<', '*', '|'];
const endChar = title.slice(-1);

if (specialChars.includes(endChar)) {
return title.substring(0, title.length - 1);
}

return title;
}

trimTruncatedTitle(title) {
const before_trunc_char = title.charAt(this.max_title_length);

let trimmedTitle = title;

if (trimmedTitle.search(" ") !== -1 && /[a-zA-Z0-9]/.test(before_trunc_char)) {
while (trimmedTitle.charAt(title.length - 1) !== ' ') {
trimmedTitle = title.substring(0, title.length - 1);
}

trimmedTitle = title.substring(0, title.length - 1);
}

return trimmedTitle;
}

// Note by Mr. Hoorn: not sure why this is necessary. It seems to me it's similar to replaceForbiddenCharacters()
truncateAtFirstSpecialCharacter(title) {
const truncate = title.search(/[:.?!]/);

if(truncate !== -1) {
return title.substring(0, truncate);
}

return title;
}

replaceForbiddenCharacters(title) {
let cleanedTitle = title.replace(/[\/\\]/g, '-');
cleanedTitle = cleanedTitle.replace(/[*|"<>]/g, '');
cleanedTitle = cleanedTitle.replace(/[?:]/g, ' -');
return cleanedTitle;
}
}
29 changes: 29 additions & 0 deletions lib/03-creator-concatenator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class CreatorConcatenator {
constructor(max_number_of_creators, delimiter) {
this._max_number_of_authors = max_number_of_creators;
this._delimiter = delimiter;
}

/**
* @param creators {CreatorModel[]} The creators to concatenate.
* @param format {CreatorFormattingType} The format to use for the concatenation.
* @param appendEtAl {boolean} Whether to append the 'et al.' to the concatenated authors when the number exceeds the maximum.
* When turned off, max number of creators has no effect.
* @returns {string} The concatenated authors in the specified format.
*/
concatenate(creators, format, appendEtAl = false) {
let concatenated = '';

for (let i = 0; i < creators.length; ++i) {
if (appendEtAl && i >= this._max_number_of_authors){
concatenated += " et al.";
break;
}

const creator = creators[i];
concatenated += concatenated ? this._delimiter + creator.getFormat(format) : creator.getFormat(format);
}

return concatenated;
}
}
56 changes: 56 additions & 0 deletions lib/04-get-item-collection-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class GetItemCollectionPaths {
/**
* Creates a new instance of the constructor.
*
* @param {FileNameSanitizer} fileSanitizer - The FileSanitizer object used for sanitizing files.
*/
constructor(fileSanitizer) {
this._fileSanitizer = fileSanitizer;
}

/**
* Retrieves the path of the given zoteroItem.
*
* @param {Object} zoteroItem - The Zotero item for which to retrieve the path.
* @returns {string} The path of the collections.
*/
get(zoteroItem) {
// Get parent collection if parent is present
let collection_ids = zoteroItem.parentID ? zoteroItem.parentItem.getCollections() : zoteroItem.getCollections();

let path = ''
if(collection_ids.length)
{
let collections = Zotero.Collections.get(collection_ids);
let collection_names = this._getCollectionNamesHierarchy(collections[0]);

for (let i = collection_names.length - 1; i >= 0; i--) // Iterate backwards
{
let collection_name = collection_names[i];
collection_name = this._fileSanitizer.sanitize(collection_name, '_'); // Convert to file safe string
path = path + '/' + collection_name;
}
}

if (path !== '') path = path.substring(1);

return path;
}

_getCollectionNamesHierarchy(collection)
{
let collectionHierarchy = [];
let currentCollection = collection;

for(let i = 0; i < 10; i++)
{
collectionHierarchy.push(currentCollection.name);

if(!currentCollection.parentID) break;

currentCollection = Zotero.Collections.get(currentCollection.parentID);
}

return collectionHierarchy;
}
}
2 changes: 1 addition & 1 deletion lib/logging/00-directory-manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Components.utils.import("resource://gre/modules/FileUtils.jsm");

var DirectoryManager = class {
class DirectoryManager {
constructor() {
this.dataDirectory = new FileUtils.File(Zotero.DataDirectory.dir);
this.dataDirectory.append('logs');
Expand Down
2 changes: 1 addition & 1 deletion lib/logging/01-output-manager-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Represents an output manager.
* @class
*/
var IOutputManager = class {
class IOutputManager {
write(formattedMessage, overwrite = false, removeEmptyLines = false) {
throw new Error("You have to implement the method write!");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/logging/02-nsi-output-stream.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var NSIOutputStream = class {
class NSIOutputStream {
constructor() {
this.nsIFileOutputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/logging/03-nsi-converter-output-stream.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var NSIConverterOutputStream = class {
class NSIConverterOutputStream {
constructor() {
this.nsIConverterOutputStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/logging/04-output-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var OutputManager = class extends IOutputManager {
class OutputManager extends IOutputManager {
constructor(directoryManager) {
super();
this._logFile = directoryManager.getLogFile();
Expand Down
2 changes: 1 addition & 1 deletion lib/logging/log_types/00-log-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var LogType = class {
class LogType {
constructor(prefix) {
this._prefix = prefix;

Expand Down
2 changes: 1 addition & 1 deletion lib/logging/log_types/01-debug-log-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var DebugLogType = class extends LogType {
class DebugLogType extends LogType {
formatMessage(message, timestamp) {
return `${this._prefix} - [${timestamp}] - DEBUG: ${message}`;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/logging/log_types/02-info-log-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var InfoLogType = class extends LogType {
class InfoLogType extends LogType {
formatMessage(message, timestamp) {
return `${this._prefix} - [${timestamp}] - INFO: ${message}`;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/logging/log_types/03-warn-log-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var WarnLogType = class extends LogType {
class WarnLogType extends LogType {
formatMessage(message, timestamp) {
return `${this._prefix} - [${timestamp}] - WARN: ${message}\n${ZotmoovDebugger.getLatestMethod()}`;
return `${this._prefix} - [${timestamp}] - WARN: ${message}\n${ZotMoovDebugger.getLatestMethod()}`;
}
}
4 changes: 2 additions & 2 deletions lib/logging/log_types/04-error-log-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var ErrorLogType = class extends LogType {
class ErrorLogType extends LogType {
formatMessage(message, timestamp) {
return `${this._prefix} - [${timestamp}] - ERROR: ${message}\n ${ZotmoovDebugger.getStack()}`;
return `${this._prefix} - [${timestamp}] - ERROR: ${message}\n ${ZotMoovDebugger.getStack()}`;
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "ZotMoov",
"version": "1.2.13",
"version": "1.2.14",
"description": "Mooves attachments and links them",
"author": "Wiley Yu & Mr. Hoorn",
"applications": {
Expand Down
Loading