dbs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // _
  2. // (_)________ ____
  3. // / / ___/ __ \/ __ \
  4. // / (__ ) /_/ / / / /
  5. // __/ /____/\____/_/ /_/
  6. // /___/
  7. module.exports = {
  8. langs:[
  9. {
  10. 'lc':'lat',
  11. 'label':'Latin (Carl Gebhardt)',
  12. 'db':'spinoza-ethica-lat-gebhardt.json'
  13. },
  14. {
  15. 'lc':'fr',
  16. 'label':'Français (Traduction par Charles Appuhn)',
  17. 'db':'spinoza-ethica-fr-appuhn.json'
  18. },
  19. {
  20. 'lc':'bra',
  21. 'label':'Brazilian (Tradução Roberto Brandão)',
  22. 'db':'spinoza-ethica-bra-brandao.json'
  23. },
  24. {
  25. 'lc':'en',
  26. 'label':'English (Translated by R. H. M. Elwes)',
  27. 'db':'spinoza-ethica-en-elwes.json'
  28. }
  29. ],
  30. data:[],
  31. loaded_dbs:0,
  32. data_byid:[],
  33. data_strct:{},
  34. 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)?$/,
  35. id_strct:[
  36. {full:'Partie',dim:'Part.'},
  37. {
  38. 'prop':{full:'Proposition', dim:'Prop.'}, // \d\d
  39. 'app' :{full:'Appendice', dim:'App.'},
  40. 'agd' :{full:'Definition generale des affections'},
  41. 'pr' :{full:'Preface', dim:'Pref.'},
  42. 'ad' :{full:'Definiton des affections'},
  43. 'ap' :{full:'Appendice', dim:'App.'},
  44. 'c' :{full:'Corollaire', dim:'Cor.'},
  45. 'p' :{full:'Postulat', dim:'Post.'},
  46. 'd' :{full:'Definition', dim:'Def.'},
  47. 'a' :{full:'Axiome', dim:'Ax.'},
  48. },
  49. {
  50. // \d\d
  51. // \d
  52. 'cd' :{full:'Corollaire Demonstration'},
  53. 'sc' :{full:'Scolie', dim:'Scol.'},
  54. 'd' :{full:'Demonstration', dim:'Demo.'},
  55. 'c' :{full:'Corollaire', dim:'Cor.'},
  56. 'a' :{full:'Axiome', dim:'Ax.'},
  57. 'l' :{full:'Lemme', dim:'Lem.'},
  58. 'p' :{full:'Postulat', dim:'Post.'},
  59. 'e' :{full:'Explication', dim:'Exp.'},
  60. },
  61. {
  62. // \d
  63. 'e' :{full:'Explication', dim:'Exp.'},
  64. 'sc' :{full:'Scolie', dim:'Scol.'},
  65. 'c' :{full:'Corollaire', dim:'Cor.'},
  66. },
  67. {
  68. 'd' :{full:'Demonstration', dim:'Demo.'},
  69. 'c' :{full:'Corollaire', dim:'Cor.'},
  70. 'a' :{full:'Axiome', dim:'Ax.'},
  71. 'sc' :{full:'Scolie', dim:'Scol.'},
  72. }
  73. ],
  74. // loading progress
  75. loaded_by_file:{},
  76. // totalloaded:0,
  77. loader: document.getElementById('db-loaded'),
  78. load(callback) {
  79. // load all dbs, when all loaded call main app callback function
  80. for (var i = 0; i < this.langs.length; i++) {
  81. this.loaded_by_file[this.langs[i].lc] = 0;
  82. this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
  83. }
  84. },
  85. loadJSON(lc, file, callback){
  86. var xobj = new XMLHttpRequest();
  87. xobj.overrideMimeType("application/json");
  88. // TODO: load and unzip gziped json
  89. // xobj.setRequestHeader('Accept-Encoding', 'gzip');
  90. // Display loading progress
  91. xobj.addEventListener("progress", function(oEvent){
  92. if (oEvent.lengthComputable) {
  93. var percentComplete = oEvent.loaded / oEvent.total * 100;
  94. // console.log(lc+' loaded :',percentComplete);
  95. this.loaded_by_file[lc] = percentComplete;
  96. var totalloaded = 0;
  97. for (var i = 0; i < this.langs.length; i++) {
  98. totalloaded += this.loaded_by_file[this.langs[i].lc];
  99. }
  100. this.loader.style.width = (totalloaded/this.langs.length)+"%";
  101. } else {
  102. // Unable to compute progress information since the total size is unknown
  103. console.log('no progress');
  104. }
  105. }.bind(this));
  106. xobj.onreadystatechange = function () {
  107. // console.log('onreadystatechange', xobj.readyState);
  108. switch(xobj.readyState){
  109. case 3:
  110. // console.log('loading');
  111. break;
  112. break;
  113. case 4:
  114. if (xobj.status === 200) {
  115. this.onJSONLoaded(lc, xobj.responseText, callback);
  116. } else {
  117. console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
  118. }
  119. break;
  120. }
  121. }.bind(this);
  122. xobj.open('GET', file, true);
  123. xobj.send(null);
  124. },
  125. onJSONLoaded(lc, json, callback){
  126. // console.log('onDBLoaded '+lc);
  127. this.data[lc] = JSON.parse(json);
  128. this.loaded_dbs ++;
  129. //
  130. if (this.loaded_dbs == this.langs.length) {
  131. // console.log('All db loaded : data', this.data);
  132. this.parseByID(callback);
  133. }
  134. },
  135. parseByID(callback){
  136. // create a id keyed array of contents
  137. var e_id, c_id, cc_id;
  138. // loop through laguages
  139. for(let l in this.data){
  140. // console.log('l', l);
  141. this.data_byid[l] = {};
  142. // loop through parts
  143. for (let p in this.data[l]) {
  144. if(this.data[l][p].type !== "intro"){
  145. // console.log(this.data[l][p]);
  146. // loop through enonces
  147. for (let e in this.data[l][p].enonces) {
  148. // console.log('e',e);
  149. if(this.data[l][p].enonces[e].id){
  150. e_id = this.data[l][p].enonces[e].id;
  151. // guess the type from the id
  152. // console.log(this.data[l][p].enonces[e].id);
  153. this.data[l][p].enonces[e].dottype = this.setupType(e_id);
  154. // breadcrumb (full tree title)
  155. this.data[l][p].enonces[e].breadcrumb = this.setupBreadcrumb(e_id);
  156. // records childs in array keyed by ids
  157. this.data_byid[l][e_id] = this.data[l][p].enonces[e];
  158. }
  159. // loop through childs
  160. for (let c in this.data[l][p].enonces[e].childs){
  161. // console.log(_db[p][e][c]);
  162. if(this.data[l][p].enonces[e].childs[c].id){
  163. c_id = this.data[l][p].enonces[e].childs[c].id;
  164. // guess the type from the id
  165. this.data[l][p].enonces[e].childs[c].dottype = this.setupType(c_id);
  166. // breadcrumb (full tree title)
  167. this.data[l][p].enonces[e].childs[c].breadcrumb = this.setupBreadcrumb(c_id);
  168. // records childs in array keyed by ids
  169. this.data_byid[l][c_id] = this.data[l][p].enonces[e].childs[c];
  170. }
  171. // loop through childs of childs
  172. for (let cc in this.data[l][p].enonces[e].childs[c].childs){
  173. // console.log(_db[p][e][c]);
  174. if(this.data[l][p].enonces[e].childs[c].childs[cc].id){
  175. cc_id = this.data[l][p].enonces[e].childs[c].childs[cc].id;
  176. // console.log('cc_id', cc_id);
  177. // guess the type from the id
  178. this.data[l][p].enonces[e].childs[c].childs[cc].dottype = this.setupType(cc_id);
  179. // breadcrumb (full tree title)
  180. this.data[l][p].enonces[e].childs[c].childs[cc].breadcrumb = this.setupBreadcrumb(cc_id);
  181. // records childs in array keyed by ids
  182. this.data_byid[l][cc_id] = this.data[l][p].enonces[e].childs[c].childs[cc];
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. // console.log('this.data_byid', this.data_byid);
  191. this.parseStrct(callback);
  192. },
  193. setupBreadcrumb(id){
  194. // /^(\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)?$/
  195. var breadcrumb_array = [];
  196. var m = id.match(this.rx_id);
  197. var mode = 'full';
  198. if(m){
  199. m.shift();
  200. // removing undefined
  201. let m_clean = [];
  202. for (let i = 0; i < m.length; i++) {
  203. if(typeof m[i] !== 'undefined'){
  204. m_clean.push(m[i]);
  205. }
  206. }
  207. // console.log('m_clean', m_clean);
  208. for (let i = 0; i < m_clean.length; i++) { // we loop through match result variables
  209. // for (let i = m_clean.length-1; i >= 0; i--) { // we loop through match result variables in reverse order
  210. // console.log('m_clean['+i+']', m_clean[i]);
  211. if(i == 0){ // first digit is part number
  212. breadcrumb_array.unshift(`${this.id_strct[i]['dim']} ${m_clean[i]}`);
  213. }else{
  214. // display mode
  215. mode = i !== m_clean.length-1 ? 'dim' : 'full';
  216. if(isNaN(m_clean[i])){ // if not a number we get the label
  217. breadcrumb_array.unshift(`${this.id_strct[i][m_clean[i]][mode]}`);
  218. // breadcrumb_array.splice(1, 0, `${m_clean[i]}`);
  219. }else{ // if its a number
  220. if(i == 1){ // we just add the number to the breadcrumb preceded by 'Proposition'
  221. breadcrumb_array.unshift(`${this.id_strct[i]['prop'][mode]} ${m_clean[i]}`);
  222. }else{ // we just add the number to the breadcrumb behind the last added item
  223. // breadcrumb_array.unshift(`${m_clean[i]}`);
  224. // debugger;
  225. breadcrumb_array.splice(1, 0, `${m_clean[i]}`);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. // console.log('breadcrumb_array', breadcrumb_array);
  232. return breadcrumb_array.join(' ');
  233. },
  234. setupType(id){
  235. // console.log('setupType',id);
  236. // /^(\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)?$/
  237. var m = id.match(this.rx_id);
  238. if(m){
  239. switch(true){
  240. case /^\d{2}$/.test(m[2]): // proposition
  241. switch(true){
  242. case /^cd$/.test(m[3]) : return 'corollaire-demo';
  243. case /^sc$/.test(m[3]) : return 'scolie';
  244. case /^d$/.test(m[3]) : return 'demonstration';
  245. case /^c$/.test(m[3]) :
  246. switch(true){
  247. case /^sc$/.test(m[4]): return 'scolie';
  248. case /^d$/.test(m[4]) : return 'demonstration';
  249. case /^sc$/.test(m[5]): return 'scolie';
  250. case /^\d$/.test(m[4]): return 'corollaire';
  251. case !m[4] : return 'corollaire';
  252. }
  253. case /^a$/.test(m[3]) : return 'prop-axiom';
  254. case /^l$/.test(m[3]) :
  255. switch(true){
  256. case /^d$/.test(m[5]) : return 'lemme-demonstration';
  257. case /^sc$/.test(m[5]): return 'lemme-scolie';
  258. case /^c$/.test(m[5]) : return 'lemme-corrollaire';
  259. case !m[5] : return 'lemme';
  260. }
  261. case /^p$/.test(m[3]) : return 'postulat';
  262. case /^\d$/.test(m[3]) : return '??';
  263. case /^\d{2}$/.test(m[3]) : return '??';
  264. case !m[3] : return 'proposition';
  265. }
  266. case /^app|ap$/.test(m[2]) : return 'appendice';
  267. case /^agd$/.test(m[2]) : return 'def-gen-affect';
  268. case /^pr$/.test(m[2]) : return 'preface';
  269. case /^ad$/.test(m[2]) :
  270. switch(true){
  271. case /^e$/.test(m[4]) :return 'explication';
  272. case !m[4] :return 'def-affect';
  273. }
  274. case /^c$/.test(m[2]) : return 'chapitre';
  275. case /^p$/.test(m[2]) : return 'postulat';
  276. case /^d$/.test(m[2]) :
  277. switch(true){
  278. case /^e$/.test(m[4]) : return 'explication';
  279. case !m[4] : return 'definition';
  280. }
  281. case /^a$/.test(m[2]) : return 'axiom';
  282. }
  283. // }
  284. }
  285. // console.log(`${this.id} -> ${this.dottype}`,m);
  286. // TODO: fix false ids
  287. // we have app and ap for appendice (1app | 4ap)
  288. // 213def ??
  289. // 209cd demo ou corollaire-demo ??
  290. // 210csc scolie ou corollaire-scolie ??
  291. // 213l1d demo ??
  292. },
  293. parseStrct(callback){
  294. var id, item, obj, links_match, link, tid;
  295. for (id in this.data_byid[this.langs[0].lc]) {
  296. item = this.data_byid[this.langs[0].lc][id];
  297. // console.log(item);
  298. // skeep titles as they don't have structure data
  299. if(item.type == "title") continue;
  300. obj = {
  301. 'to':[],
  302. 'from':[],
  303. };
  304. // get links
  305. links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
  306. // console.log(links_match);
  307. // if links exist on text
  308. if(links_match){
  309. for(link of links_match){
  310. // for(i in links_match){
  311. // link = links_match[i];
  312. // console.log(link);
  313. // get the target id
  314. tid = link.match(/\((.+)\)/)[1];
  315. // avoid duplicates
  316. if (obj.to.indexOf(tid) == -1)
  317. obj.to.push(tid);
  318. // add id to "from" links in target
  319. // if target exists
  320. if(typeof this.data_strct[tid] !== 'undefined'){
  321. // avoid duplicates
  322. if (this.data_strct[tid].from.indexOf(tid) == -1)
  323. this.data_strct[tid].from.push(id);
  324. }else{
  325. // if targets does not exists, the db has an issue, warn about that
  326. // console.log(`!! warning : ${tid} target id does not exists`);
  327. }
  328. }
  329. }
  330. // add the item links to the main links listings
  331. this.data_strct[id] = obj;
  332. }
  333. // console.log('data_strct',this.data_strct);
  334. callback();
  335. }
  336. }