Skip to content

Commit

Permalink
Merge pull request #34 from aMarcireau/editor
Browse files Browse the repository at this point in the history
Editor
  • Loading branch information
Alexandre Marcireau authored Dec 5, 2017
2 parents 645fffb + 725ca47 commit 9675cd4
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 141 deletions.
32 changes: 0 additions & 32 deletions source/actions/managePublication.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,3 @@ export function updateAllPublications(timestamp) {
timestamp,
};
}

export function isOlderThan(firstDate, secondDate) {
const firstIsNull = firstDate == null || firstDate.length == 0 || (firstDate.length == 1 && firstDate[0] == null);
const secondIsNull = secondDate == null || secondDate.length == 0 || (secondDate.length == 1 && secondDate[0] == null);
if (firstIsNull && secondIsNull) {
throw new Error('isOlderThan was called with two null dates');
}
if (firstIsNull) {
return false;
}
if (secondIsNull) {
return true;
}
let isOlder = false;
firstDate.every((part, index) => {
if (index >= secondDate.length) {
return false;
}
if (part != secondDate[index]) {
isOlder = (part < secondDate[index]);
return false;
}
return true;
});
return isOlder;
}

export function pad(number) {
return (number < 10 ? '0' + number.toString() : number.toString());
}

export const doiPattern = /^\s*(?:https?:\/\/doi\.org\/)?(10\.[0-9]{4,}(?:\.[0-9]+)*\/(?:(?![%"#? ])\S)+)\s*$/;
72 changes: 51 additions & 21 deletions source/actions/manageScholarPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ipcRenderer} from 'electron'
import htmlparser from 'htmlparser2'
import cssSelect from 'css-select'
import {publicationFromCiterMetadata} from './getPublicationFromMetadata'
import {levenshteinDistance} from '../libraries/utilities'
import {
FETCH_SCHOLAR_PAGE,
RESOLVE_SCHOLAR_INITIAL_PAGE,
Expand Down Expand Up @@ -55,9 +56,9 @@ export function scholarDisconnect() {
const firstCategoryRecaptchaQuery = cssSelect.compile('#gs_captcha_c');
const secondCategoryRecaptchaQuery = cssSelect.compile('.g-recaptcha');
const titleQuery = cssSelect.compile('head title');
const citersLinkCandidatesQuery = cssSelect.compile('.gs_r.gs_or.gs_scl:first-child .gs_fl a');
const publicationsQuery = cssSelect.compile('.gs_r.gs_or.gs_scl .gs_ri');
const titleFromPublicationQuery = cssSelect.compile('.gs_rt a');
const citersLinkFromPublicationQuery = cssSelect.compile('.gs_fl a');
const metadataFromPublicationQuery = cssSelect.compile('.gs_a');

export function resolveHtml(url, text) {
Expand Down Expand Up @@ -134,42 +135,71 @@ export function resolveHtml(url, text) {
} else {
switch (state.scholar.pages[0].type) {
case PAGE_TYPE_INITIALIZE: {
const citersLinkCandidates = cssSelect(citersLinkCandidatesQuery, dom);
let found = false;
for (let citersLinkCandidate of citersLinkCandidates) {
const publications = cssSelect(publicationsQuery, dom);
const candidates = [];
for (const publication of publications) {
const titleCandidates = cssSelect(titleFromPublicationQuery, publication);
if (
citersLinkCandidate.children
&& citersLinkCandidate.children.length > 0
&& citersLinkCandidate.children[0].type === 'text'
&& citersLinkCandidate.attribs
&& citersLinkCandidate.attribs.href
titleCandidates.length === 0
|| !titleCandidates[0].children
|| titleCandidates[0].children.length === 0
|| titleCandidates[0].children[0].type !== 'text'
) {
const numberOfCitersMatch = citersLinkCandidate.children[0].data.match(/^Cited by (\d+)$/);
const scholarIdMatch = citersLinkCandidate.attribs.href.match(/^\/scholar\?cites=(\d+)/);
if (numberOfCitersMatch && scholarIdMatch) {
dispatchAndEcho({
type: RESOLVE_SCHOLAR_INITIAL_PAGE,
numberOfCiters: Math.min(parseInt(numberOfCitersMatch[1]), 999),
scholarId: scholarIdMatch[1],
}, refractoryPeriod);
found = true;
break;
continue;
}
for (let citersLinkCandidate of cssSelect(citersLinkFromPublicationQuery, publication)) {
if (
citersLinkCandidate.children
&& citersLinkCandidate.children.length > 0
&& citersLinkCandidate.children[0].type === 'text'
&& citersLinkCandidate.attribs
&& citersLinkCandidate.attribs.href
) {
const numberOfCitersMatch = citersLinkCandidate.children[0].data.match(/^Cited by (\d+)$/);
const scholarIdMatch = citersLinkCandidate.attribs.href.match(/^\/scholar\?cites=(\d+)/);
if (numberOfCitersMatch && scholarIdMatch) {
candidates.push({
title: titleCandidates[0].children[0].data,
numberOfCiters: Math.min(parseInt(numberOfCitersMatch[1]), 999),
scholarId: scholarIdMatch[1],
});
break;
}
}
}
}
if (!found) {
if (candidates.length === 0) {
dispatchAndEcho({
type: RESOLVE_SCHOLAR_INITIAL_PAGE,
numberOfCiters: 0,
scholarId: null,
}, refractoryPeriod);
} else {
const bestCandidate = candidates.reduce((best, candidate) => {
const distance = levenshteinDistance(
candidate.title,
state.publications.get(state.scholar.pages[0].doi).title
);
if (!best || distance < best.distance) {
return {
...candidate,
distance,
};
}
return best;
}, null);
dispatchAndEcho({
type: RESOLVE_SCHOLAR_INITIAL_PAGE,
numberOfCiters: bestCandidate.numberOfCiters,
scholarId: bestCandidate.scholarId,
}, refractoryPeriod);
}
break;
}
case PAGE_TYPE_CITERS: {
const publications = cssSelect(publicationsQuery, dom);
if (publications.length > 0) {
for (let publication of publications) {
for (const publication of publications) {
const titleCandidates = cssSelect(titleFromPublicationQuery, publication);
if (
titleCandidates.length === 0
Expand Down
77 changes: 4 additions & 73 deletions source/actors/manageCrossref.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
rejectPublicationFromMetadata,
rejectPublicationFromMetadataConnection,
} from '../actions/getPublicationFromMetadata'
import {isOlderThan} from '../actions/managePublication'
import {
isOlderThan,
levenshteinDistance,
} from '../libraries/utilities'
import {
PUBLICATION_STATUS_UNVALIDATED,
PUBLICATION_STATUS_DEFAULT,
Expand All @@ -25,78 +28,6 @@ import {
PUBLICATION_REQUEST_TYPE_IMPORTED_METADATA,
} from '../constants/enums'

function smallestOfThreePlusOne(first, second, third, incrementSecondIfSmallest) {
return (first < second || third < second ?
(first > third ? third + 1 : first + 1)
: (incrementSecondIfSmallest ? second : second + 1)
);
}

function levenshteinDistance(first, second) {
if (first === second) {
return 0;
}
if (first.length > second.length) {
[first, second] = [second, first];
}
let firstLength = first.length;
let secondLength = second.length;
while (firstLength > 0 && (first.charCodeAt(firstLength - 1) === second.charCodeAt(secondLength - 1))) {
firstLength--;
secondLength--;
}
let offset = 0;
while (offset < firstLength && (first.charCodeAt(offset) === second.charCodeAt(offset))) {
offset++;
}
firstLength -= offset;
secondLength -= offset;
if (firstLength === 0 || secondLength === 1) {
return secondLength;
}
const distances = new Array(firstLength << 1);
for (let y = 0; y < firstLength;) {
distances[firstLength + y] = first.charCodeAt(offset + y);
distances[y] = ++y;
}
let temporary0;
let temporary1;
let temporary2;
let temporary3;
let result;
let x;
for (x = 0; (x + 3) < secondLength;) {
let secondX0 = second.charCodeAt(offset + (temporary0 = x));
let secondX1 = second.charCodeAt(offset + (temporary1 = x + 1));
let secondX2 = second.charCodeAt(offset + (temporary2 = x + 2));
let secondX3 = second.charCodeAt(offset + (temporary3 = x + 3));
result = (x += 4);
for (let y = 0; y < firstLength;) {
let firstY = first.charCodeAt(offset + y);
let distanceY = distances[y];
temporary0 = smallestOfThreePlusOne(distanceY, temporary0, temporary1, secondX0 === firstY);
temporary1 = smallestOfThreePlusOne(temporary0, temporary1, temporary2, secondX1 === firstY);
temporary2 = smallestOfThreePlusOne(temporary1, temporary2, temporary3, secondX2 === firstY);
result = smallestOfThreePlusOne(temporary2, temporary3, result, secondX3 === firstY);
distances[y++] = result;
temporary3 = temporary2;
temporary2 = temporary1;
temporary1 = temporary0;
temporary0 = distanceY;
}
}
for (; x < secondLength;) {
let secondX0 = second.charCodeAt(offset + (temporary0 = x));
result = ++x;
for (let y = 0; y < firstLength; y++) {
let distanceY = distances[y];
distances[y] = result = smallestOfThreePlusOne(distanceY, temporary0, result, secondX0 === distances[firstLength + y]);
temporary0 = distanceY;
}
}
return result;
}

export default function manageCrossref(store) {
const state = store.getState();
if (state.connected) {
Expand Down
2 changes: 1 addition & 1 deletion source/containers/AddDoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import Radium from 'radium'
import {connect} from 'react-redux'
import {publicationFromDoi} from '../actions/getPublicationFromDoi'
import {doiPattern} from '../actions/managePublication'
import {doiPattern} from '../libraries/utilities'

class AddDoi extends React.Component {

Expand Down
6 changes: 4 additions & 2 deletions source/containers/Information.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import {connect} from 'react-redux'
import PropTypes from 'prop-types'
import ConfirmationButton from '../components/ConfirmationButton'
import List from '../components/List'
import {
isOlderThan,
pad,
} from '../libraries/utilities'
import {
addPublicationToCollection,
selectPublication,
updatePublication,
updateAllPublications,
removePublication,
pad,
isOlderThan,
} from '../actions/managePublication'
import {
PUBLICATION_STATUS_UNVALIDATED,
Expand Down
4 changes: 3 additions & 1 deletion source/containers/PublicationsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import PropTypes from 'prop-types'
import List from '../components/List'
import {
isOlderThan,
pad,
} from '../libraries/utilities'
import {
selectPublication,
unselectPublication,
pad,
} from '../actions/managePublication'
import {
PUBLICATION_STATUS_UNVALIDATED,
Expand Down
4 changes: 2 additions & 2 deletions source/containers/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {connect} from 'react-redux'
import PropTypes from 'prop-types'
import {setSearch} from '../actions/manageSearch'
import List from '../components/List'
import {addPublicationToCollection} from '../actions/managePublication'
import {
addPublicationToCollection,
selectPublication,
isOlderThan,
} from '../actions/managePublication'
} from '../libraries/utilities'
import {
PUBLICATION_STATUS_UNVALIDATED,
PUBLICATION_STATUS_DEFAULT,
Expand Down
Loading

0 comments on commit 9675cd4

Please sign in to comment.