summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/shortmathj/index.js
blob: 6a477f363bdd2e2901043b2b4dee3f96598386d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const csv = require('csv-parser')
const fs = require('fs')
const results = [];

function readCSVsimple(filename, callback){
	
	fs.createReadStream(filename)
	  .pipe(csv())
	  .on('data', (data) => results.push(data))
	  .on('end', () => {
		callback();
	  });
}

readCSVsimple('annser.csv', ()=>{
	readCSVsimple('custom.csv', ()=>{
		writeSTY()
	});
});

function createSynonyms(results){
	for(let i = 0; i < results.length; i++) {
		let res = results[i];
		if(/^The\s+/.test(res['Full Title'])){
			results.push({
				'Full Title' : res['Full Title'].replace(/^The\s+/,''),
				'Abbrev' : res['Abbrev']
			});
		}		
		if(/\s*\(*New\Series\.*\)*$/.test(res['Full Title'])){
			results.push({
				'Full Title' : res['Full Title'].replace(/\s*\(*New\Series\.*\)*$/,''),
				'Abbrev' : res['Abbrev']
			});
		}
	}

}

function resultsToReplacementMap(results){
	const replacementMap = [];
	createSynonyms(results);
	results.map(res => {
		if(res['Full Title'] === res['Abbrev']){
			// Full and Abbreviated titles are equal, thus do nothing
			return;
		}
		if(!res['Full Title'] || !res['Abbrev']){
			// Nothing to replace!
			return;
		}
		if(res['Full Title'].search(/\$/) !== -1 || res['Abbrev'].search(/\$/) !== -1){
			// Just skip journals with formulas in titles
			return;
		}
		res['Full Title'] = res['Full Title'].replace(/&/g,'\\&');
		res['Abbrev'] = res['Abbrev'].replace(/&/g,'\\&');
		replacementMap.push([res['Full Title'],res['Abbrev']]);
	});
	return replacementMap;
}



function writeSTY(){
	styContent = fs.readFileSync('shortmathj.template.sty','utf8');

	let alphabet = {};
	let replacementMap = resultsToReplacementMap(results);
	replacementMap.map(rule => {
		alphabet[rule[0].substr(0,1)] = [];
	});
	replacementMap.map(rule => {
		alphabet[rule[0].substr(0,1)].push(
			"\\ifthenelse{\\equal{\\givenAMSjournalname}{\\detokenize{"+rule[0]+"}}}{\\def\\shortifiedAMSjournalname{"+rule[1]+"}}{}%\n"
		);
	});

	let replacement="";

	for(let letter in alphabet){
		replacement +=
			"\\ifthenelse{\\equal{\\firstletter}{\\detokenize{"+letter+"}}}{%\n" +
			alphabet[letter].join('') +
			"}{}%\n";
	}



	styContent = styContent.replace(/\%\%\%\{\{SHORTMAP[\s\S]*\%\%\%\}\}SHORTMAP/, replacement + "%");

	fs.writeFileSync('shortmathj.sty', styContent);
}