dbs.js 13 KB

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