Skip to content

Commit

Permalink
Place non-action functions in their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Marcireau committed Dec 18, 2017
1 parent 78b3944 commit 4174b9a
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 277 deletions.
251 changes: 0 additions & 251 deletions source/actions/manageMenu.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {ipcRenderer} from 'electron'
import {
convertLaTeX,
stringifyLaTeX
} from '../libraries/latex-to-unicode-converter'
import crossrefQueue from '../queues/crossrefQueue'
import doiQueue from '../queues/doiQueue'
import scholarQueue from '../queues/scholarQueue'
import {
RESET,
OPEN_MENU_ITEM,
Expand All @@ -22,16 +15,6 @@ import {
SELECT_GRAPH_DISPLAY,
SELECT_LIST_DISPLAY,
} from '../constants/actionTypes'
import {
SCHOLAR_STATUS_IDLE,
SCHOLAR_STATUS_FETCHING,
SCHOLAR_STATUS_BLOCKED_HIDDEN,
SCHOLAR_STATUS_BLOCKED_VISIBLE,
SCHOLAR_STATUS_UNBLOCKING,
CROSSREF_REQUEST_TYPE_VALIDATION,
CROSSREF_REQUEST_TYPE_CITER_METADATA,
CROSSREF_REQUEST_TYPE_IMPORTED_METADATA,
} from '../constants/enums'

export function reset(state) {
return {
Expand Down Expand Up @@ -128,237 +111,3 @@ export function selectGraphDisplay() {
export function selectListDisplay() {
return {type: SELECT_LIST_DISPLAY};
}

export function stateToJson(state, expand) {
return `${JSON.stringify({
appVersion: state.appVersion,
display: state.menu.display,
knownDois: Array.from(state.knownDois),
crossref: state.crossref.requests.filter(crossrefRequest => (
crossrefRequest.type !== CROSSREF_REQUEST_TYPE_CITER_METADATA
|| state.publications.has(crossrefRequest.parentDoi)
)),
doi: state.doi.requests.filter(doiRequest => state.publications.has(doiRequest.doi)),
graph: state.graph,
saveFilename: expand ? undefined : state.menu.saveFilename,
savable: expand ? undefined : state.menu.savedVersion < state.version,
publications: Array.from(state.publications.entries()),
scholar: {
requests: state.scholar.requests.filter(request => state.publications.has(request.doi)),
minimumRefractoryPeriod: state.scholar.minimumRefractoryPeriod,
maximumRefractoryPeriod: state.scholar.maximumRefractoryPeriod,
},
search: state.search,
tabs: state.tabs.index,
warnings: state.warnings.list,
}, null, expand ? ' ' : null)}${expand ? '\n' : ''}`;
}

export function jsonToState(json, saveFilename, previousState) {
try {
const state = JSON.parse(new TextDecoder('utf-8').decode(json));
state.appVersion = previousState ? previousState.appVersion : undefined;
state.colors = previousState ? previousState.colors : undefined;
state.connected = previousState ? previousState.connected : undefined;
state.crossref = {
status: crossrefQueue.status.IDLE,
requests: state.crossref,
};
state.doi = {
status: doiQueue.status.IDLE,
requests: state.doi,
};
state.knownDois = new Set(state.knownDois);
state.menu = {
activeItem: null,
hash: previousState ? previousState.menu.hash + 1 : 0,
saveFilename: saveFilename ? saveFilename : state.saveFilename,
savedVersion: previousState ? previousState.version + 1 : 0,
display: state.display,
};
delete state.saveFilename;
delete state.display;
state.publications = new Map(state.publications);
state.scholar.status = SCHOLAR_STATUS_IDLE;
state.scholar.beginOfRefractoryPeriod = null;
state.scholar.endOfRefractoryPeriod = null;
state.scholar.url = null;
state.tabs = {
index: state.tabs,
hash: previousState ? previousState.tabs.hash + 1 : 0,
};
state.version = (previousState ?
previousState.version + 1
: (state.savable ?
1
: 0
)
);
delete state.savable;
state.warnings = {
list: state.warnings,
hash: previousState ? previousState.warnings.hash + 1 : 0,
};
return [null, state];
} catch(error) {
return [error, null];
}
}

export function bibtexToPublications(bibtex) {
const bibtexAsString = bibtex.toString('utf8');
const publications = [];
let line = 1;
let position = 0;
let status = 'root';
let nesting = 0;
let publication = {};
let key = '';
const throwError = character => {
throw new Error(`Unexpected character '${character}' on line ${line}:${position}`);
};
const addPublication = () => {
if (publication.title != null && publication.author != null && publication.year != null) {
publications.push({
title: convertLaTeX({
onError: (error, latex) => stringifyLaTeX(latex)
}, publication.title),
authors: convertLaTeX({
onError: (error, latex) => stringifyLaTeX(latex)
}, publication.author).split(' and ').map(
author => author.split(',').reverse().filter(name => name.length > 0).map(name => name.trim()).join(' ')
),
dateAsString: convertLaTeX({
onError: (error, latex) => stringifyLaTeX(latex)
}, publication.year),
});
}
key = '';
publication = {};
status = 'root';
nesting = 0;
}
try {
for (const character of bibtexAsString) {
++position;
switch (status) {
case 'root':
if (character === '@') {
status = 'type';
} else if (/\S/.test(character)) {
status = 'comment';
}
break;
case 'comment':
if (character === '\n') {
status = 'root';
}
break;
case 'type':
if (/(\w|\s)/.test(character)) {
break;
} else if (character === '{') {
status = 'label';
nesting = 1;
} else {
throwError(character);
}
break;
case 'label':
if (character === ',') {
status = 'beforeKey';
} else if (character === '{' || character === '}') {
throwError(character);
}
break;
case 'beforeKey':
if (character === '}') {
addPublication();
} else if (/\w/.test(character)) {
status = 'key';
key += character.toLowerCase();
} else if (/\S/.test(character)) {
throwError(character);
}
break;
case 'key':
if (/(\w|:|-)/.test(character)) {
key += character.toLowerCase();
} else if (character === '=') {
publication[key] = '';
status = 'beforeValue';
} else if (/\s/.test(character)) {
status = 'afterKey';
} else {
throwError(character);
}
break;
case 'afterKey':
if (character === '=') {
publication[key] = '';
status = 'beforeValue';
} else if (/\S/.test(character)) {
throwError(character);
}
break;
case 'beforeValue':
if (/\s/.test(character)) {
break;
} else if (character === '}') {
throwError(character);
} else {
if (character === '{') {
++nesting;
}
publication[key] += character;
status = 'value';
}
break;
case 'value':
if (character === '}') {
--nesting;
if (nesting === 0) {
addPublication();
} else {
publication[key] += character;
}
} else if (character === '{') {
++nesting;
publication[key] += character;
} else if (nesting === 1) {
if (character === ',') {
key = '';
status = 'beforeKey';
} else if (/(\s)/.test(character)) {
status = 'afterValue';
} else {
publication[key] += character;
}
} else {
publication[key] += character;
}
break;
case 'afterValue': {
if (character === ',') {
key = '';
status = 'beforeKey';
} else if (character === '}') {
addPublication();
} else if (/\S/.test(character)) {
throwError(character);
}
break;
}
default:
break;
}
if (character === '\n') {
++line;
position = 0;
}
}
return [null, publications];
} catch (error) {
return [error, null];
}
}
2 changes: 1 addition & 1 deletion source/actors/manageVersion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ipcRenderer} from 'electron'
import {stateToJson} from '../actions/manageMenu'
import {stateToJson} from '../state'

let inhibited = false;
let bufferedState = null;
Expand Down
2 changes: 1 addition & 1 deletion source/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import origamiReducers from './reducers/origami'
import origamiActors from './actors/origami'
import Origami from './containers/Origami'
import {disconnect} from './actions/setConnection'
import {jsonToState} from './actions/manageMenu'
import {jsonToState} from './state'
import {SCHOLAR_STATUS_IDLE} from './constants/enums'

ipcRenderer.once('startWithState', (event, json, appVersion, colors) => {
Expand Down
8 changes: 5 additions & 3 deletions source/containers/Origami.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import SetRefractoryPeriod from './SetRefractoryPeriod'
import Tabs from './Tabs'
import Warnings from './Warnings'
import Tab from '../components/Tab'
import {bibtexToPublications} from '../libraries/utilities'
import {
stateToJson,
jsonToState,
} from '../state'
import {
reset,
resolveSave,
Expand All @@ -28,9 +33,6 @@ import {
rejectImportBibtex,
selectGraphDisplay,
selectListDisplay,
stateToJson,
jsonToState,
bibtexToPublications,
} from '../actions/manageMenu'
import {
PUBLICATION_STATUS_UNVALIDATED,
Expand Down
Loading

0 comments on commit 4174b9a

Please sign in to comment.