-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDefs.js
More file actions
109 lines (93 loc) · 3.85 KB
/
getDefs.js
File metadata and controls
109 lines (93 loc) · 3.85 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module.exports = function(body){// calls getHash
// regex used in sliceHash
var regAllEquals = /\n?={2,}/g, // gets ==
regHash = /# \S/g, // gets '# someword'
regHashCurly = /# [{\w]/g, // gets '# {' or '# someword'
regNoHash = /\n?#+\*/, // gets '#*'
sections = body.split(regAllEquals);
definitionString = getHash(sections);
return definitionString;
function getHash(sectionsArray){
//console.log(sectionsArray);
var allDefs = {};
// loops for all possible parts of speech
for (var i = 0; i < sectionsArray.length; i++){
var partOfSpeech,
defs = [];
// if this contains hashes, gimme gimme gimme
if (sectionsArray[i].match(regHash)){
partOfSpeech = getSpeech(sectionsArray[i]);
//console.log("part of speech:" + partOfSpeech);
defs = sliceHash(sectionsArray[i]);
// console.log(partOfSpeech);
// console.log(defs);
if (!(partOfSpeech in allDefs)){
allDefs[partOfSpeech] = defs;
} else {
allDefs[partOfSpeech].concat(defs);
}
allDefs[partOfSpeech] = allDefs[partOfSpeech].slice(0,4);
}
}
return makePretty(allDefs);
}
function getSpeech(hashblockString){
var result = "No part of speech found",
pos = [/noun/,/verb/, /adj/, /adv/, /conj/, /prep/];
for (var i = 0; i < pos.length; i++){
if (hashblockString.match(pos[i])){
result = pos[i];
break;
}
}
return result;//.replace(/\//g, ""); // returns to getHash
}
function sliceHash(hashblockArray){
var eachDef = [];
var noMoreStars = hashblockArray.split("\n").filter(function(e){
return e.match(regNoHash) === null;
});
for (var i = 0; i < noMoreStars.length; i++){
if (noMoreStars[i].match(regHashCurly)){
eachDef.push(noMoreStars[i]);
}
}
eachDef = eachDef.map(function(def){
return def.replace(/[\[\]]/g, ''); // remove anything between []
});
eachDef = eachDef.map(function(def){
return def.replace(/{.*?}}/g, ''); // removes anything between {}
});
return eachDef; // array returns to getHash
}
function makePretty(defsObject){
// console.log("defsobject: ");
// console.log(defsObject);
var definitionString = "";
for (var partOfSpeech in defsObject){
// console.log("here i am");
// console.log(defsObject[partOfSpeech]);
defsObject[partOfSpeech] = defsObject[partOfSpeech].map(function(e){
return e.replace(/ \w+\|/g, " ");
});
defsObject[partOfSpeech] = defsObject[partOfSpeech].map(function(e){
return e.replace(/''/g, "");
});
defsObject[partOfSpeech] = defsObject[partOfSpeech].map(function(e){
return e.replace(/#+/g, "");
});
// console.log("object after prettifying");
// console.log(defsObject[partOfSpeech]);
var bulletNum = 1;
defsObject[partOfSpeech].forEach(function(definition){
//console.log("definition:" + definition);
if (definition.match(/[^\s]/)){ // if anything other than a whitespace char exists
definitionString += "<strong>" + bulletNum + ". " + partOfSpeech + "</strong> " + definition + "<br><br>";
bulletNum++;
}
});
}
//console.log("string in getdefs:" + definitionString);
return definitionString; // returns a string to uberfunction getDefs
}
};