Files
app.ethica-spinoza.net/assets/modules/dbs.js
T

225 lines
7.5 KiB
JavaScript

// _
// (_)________ ____
// / / ___/ __ \/ __ \
// / (__ ) /_/ / / / /
// __/ /____/\____/_/ /_/
// /___/
module.exports = {
lang:'fr',
langs:[
{
'lc':'lat',
'label':'Latin (Carl Gebhardt)',
'db':'spinoza-ethica-lat-gebhardt.json'
},
{
'lc':'fr',
'label':'Français (Traduction par Charles Appuhn)',
'db':'spinoza-ethica-fr-appuhn.json'
},
{
'lc':'bra',
'label':'Brazilian (Tradução Roberto Brandão)',
'db':'spinoza-ethica-bra-brandao.json'
},
{
'lc':'en',
'label':'English (Translated by R. H. M. Elwes)',
'db':'spinoza-ethica-en-elwes.json'
}
],
data:[],
loaded_dbs:0,
data_byid:[],
data_strct:{},
load(callback) {
// load all dbs, when all loaded call main app callback function
for (var i = 0; i < this.langs.length; i++) {
this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
}
},
loadJSON(lc, file, callback){
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
// TODO: load and unzip gziped json
// xobj.setRequestHeader('Accept-Encoding', 'gzip');
xobj.onreadystatechange = function () {
// console.log('onreadystatechange', xobj.readyState);
switch(xobj.readyState){
case 3:
// console.log('loading');
break;
break;
case 4:
if (xobj.status === 200) {
this.onJSONLoaded(lc, xobj.responseText, callback);
} else {
console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
}
break;
}
}.bind(this);
xobj.open('GET', file, true);
xobj.send(null);
},
onJSONLoaded(lc, json, callback){
// console.log('onDBLoaded '+lc);
this.data[lc] = JSON.parse(json);
this.loaded_dbs ++;
//
if (this.loaded_dbs == this.langs.length) {
// console.log('All db loaded : data', this.data);
this.parseByID(callback);
}
},
parseByID(callback){
// create a id keyed array of contents
// loop through laguages
for(l in this.data){
// console.log('l', l);
this.data_byid[l] = {};
// loop through parts
for (p in this.data[l]) {
if(this.data[l][p].type !== "intro"){
// console.log(this.data[l][p]);
// loop through enonces
for (e in this.data[l][p].enonces) {
// console.log('e',e);
if(this.data[l][p].enonces[e].id){
// guess the type from the id
// console.log(this.data[l][p].enonces[e].id);
this.data[l][p].enonces[e].dottype = this.setupType(this.data[l][p].enonces[e].id);
// records childs in array keyed by ids
this.data_byid[l][this.data[l][p].enonces[e].id] = this.data[l][p].enonces[e];
}
// loop through childs
for (c in this.data[l][p].enonces[e].childs){
// console.log(_db[p][e][c]);
if(this.data[l][p].enonces[e].childs[c].id){
// guess the type from the id
this.data[l][p].enonces[e].childs[c].dottype = this.setupType(this.data[l][p].enonces[e].childs[c].id);
// records childs in array keyed by ids
this.data_byid[l][this.data[l][p].enonces[e].childs[c].id] = this.data[l][p].enonces[e].childs[c];
}
}
}
}
}
}
// console.log('this.data_byid', this.data_byid);
this.parseStrct(callback);
},
setupType(id){
// console.log('setupType',id);
var rx_id = /^(\d)(app|agd|\d\d|pr|ad|ap|c|p|d|a)(cd|sc|\d\d|d|c|a|l|p|\d)?(e|\d|sc)?(d|c|a|sc)?$/;
var m = id.match(rx_id);
if(m){
switch(true){
case /^\d{2}$/.test(m[2]): // proposition
switch(true){
case /^cd$/.test(m[3]) : return 'corollaire-demo';
case /^sc$/.test(m[3]) : return 'scolie';
case /^d$/.test(m[3]) : return 'demonstration';
case /^c$/.test(m[3]) :
switch(true){
case /^sc$/.test(m[4]): return 'scolie';
case /^d$/.test(m[4]) : return 'demonstration';
case /^sc$/.test(m[5]): return 'scolie';
case /^\d$/.test(m[4]): return 'corollaire';
case !m[4] : return 'corollaire';
}
case /^a$/.test(m[3]) : return 'prop-axiom';
case /^l$/.test(m[3]) :
switch(true){
case /^d$/.test(m[5]) : return 'lemme-demonstration';
case /^sc$/.test(m[5]): return 'lemme-scolie';
case /^c$/.test(m[5]) : return 'lemme-corrollaire';
case !m[5] : return 'lemme';
}
case /^p$/.test(m[3]) : return 'postulat';
case /^\d$/.test(m[3]) : return '??';
case /^\d{2}$/.test(m[3]) : return '??';
case !m[3] : return 'proposition';
}
case /^app|ap$/.test(m[2]) : return 'appendice';
case /^agd$/.test(m[2]) : return 'def-gen-affect';
case /^pr$/.test(m[2]) : return 'preface';
case /^ad$/.test(m[2]) :
switch(true){
case /^e$/.test(m[4]) :return 'explication';
case !m[4] :return 'def-affect';
}
case /^c$/.test(m[2]) : return 'chapitre';
case /^p$/.test(m[2]) : return 'postulat';
case /^d$/.test(m[2]) :
switch(true){
case /^e$/.test(m[4]) : return 'explication';
case !m[4] : return 'definition';
}
case /^a$/.test(m[2]) : return 'axiom';
}
// }
}
// console.log(`${this.id} -> ${this.dottype}`,m);
// TODO: fix false ids
// we have app and ap for appendice (1app | 4ap)
// 213def ??
// 209cd demo ou corollaire-demo ??
// 210csc scolie ou corollaire-scolie ??
// 213l1d demo ??
},
parseStrct(callback){
var id, item, obj, links_match, link, tid;
for (id in this.data_byid[this.langs[0].lc]) {
item = this.data_byid[this.langs[0].lc][id];
// console.log(item);
// skeep titles as they don't have structure data
if(item.type == "title") continue;
obj = {
'to':[],
'from':[],
};
// get links
links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
// console.log(links_match);
// if links exist on text
if(links_match){
for(link of links_match){
// for(i in links_match){
// link = links_match[i];
// console.log(link);
// get the target id
tid = link.match(/\((.+)\)/)[1];
// avoid duplicates
if (obj.to.indexOf(tid) == -1)
obj.to.push(tid);
// add id to "from" links in target
// if target exists
if(typeof this.data_strct[tid] !== 'undefined'){
// avoid duplicates
if (this.data_strct[tid].from.indexOf(tid) == -1)
this.data_strct[tid].from.push(id);
}else{
// if targets does not exists, the db has an issue, warn about that
// console.log(`!! warning : ${tid} target id does not exists`);
}
}
}
// add the item links to the main links listings
this.data_strct[id] = obj;
}
// console.log('data_strct',this.data_strct);
callback();
}
}