dbs.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. load(callback) {
  36. // load all dbs, when all loaded call main app callback function
  37. for (var i = 0; i < this.langs.length; i++) {
  38. this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
  39. }
  40. },
  41. loadJSON(lc, file, callback){
  42. var xobj = new XMLHttpRequest();
  43. xobj.overrideMimeType("application/json");
  44. // TODO: load and unzip gziped json
  45. // xobj.setRequestHeader('Accept-Encoding', 'gzip');
  46. xobj.onreadystatechange = function () {
  47. // console.log('onreadystatechange', xobj.readyState);
  48. switch(xobj.readyState){
  49. case 3:
  50. // console.log('loading');
  51. break;
  52. break;
  53. case 4:
  54. if (xobj.status === 200) {
  55. this.onJSONLoaded(lc, xobj.responseText, callback);
  56. } else {
  57. console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
  58. }
  59. break;
  60. }
  61. }.bind(this);
  62. xobj.open('GET', file, true);
  63. xobj.send(null);
  64. },
  65. onJSONLoaded(lc, json, callback){
  66. // console.log('onDBLoaded '+lc);
  67. this.data[lc] = JSON.parse(json);
  68. this.loaded_dbs ++;
  69. //
  70. if (this.loaded_dbs == this.langs.length) {
  71. // console.log('All db loaded : data', this.data);
  72. this.parseByID(callback);
  73. }
  74. },
  75. parseByID(callback){
  76. // create a id keyed array of contents
  77. // loop through laguages
  78. for(l in this.data){
  79. // console.log('l', l);
  80. this.data_byid[l] = {};
  81. // loop through parts
  82. for (p in this.data[l]) {
  83. if(this.data[l][p].type !== "intro"){
  84. // console.log(this.data[l][p]);
  85. // loop through enonces
  86. for (e in this.data[l][p].enonces) {
  87. // console.log('e',e);
  88. if(this.data[l][p].enonces[e].id){
  89. // guess the type from the id
  90. // console.log(this.data[l][p].enonces[e].id);
  91. this.data[l][p].enonces[e].dottype = this.setupType(this.data[l][p].enonces[e].id);
  92. // records childs in array keyed by ids
  93. this.data_byid[l][this.data[l][p].enonces[e].id] = this.data[l][p].enonces[e];
  94. }
  95. // loop through childs
  96. for (c in this.data[l][p].enonces[e].childs){
  97. // console.log(_db[p][e][c]);
  98. if(this.data[l][p].enonces[e].childs[c].id){
  99. // guess the type from the id
  100. this.data[l][p].enonces[e].childs[c].dottype = this.setupType(this.data[l][p].enonces[e].childs[c].id);
  101. // records childs in array keyed by ids
  102. this.data_byid[l][this.data[l][p].enonces[e].childs[c].id] = this.data[l][p].enonces[e].childs[c];
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. // console.log('this.data_byid', this.data_byid);
  110. this.parseStrct(callback);
  111. },
  112. setupType(id){
  113. // console.log('setupType',id);
  114. 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)?$/;
  115. var m = id.match(rx_id);
  116. if(m){
  117. switch(true){
  118. case /^\d{2}$/.test(m[2]): // proposition
  119. switch(true){
  120. case /^cd$/.test(m[3]) : return 'corollaire-demo';
  121. case /^sc$/.test(m[3]) : return 'scolie';
  122. case /^d$/.test(m[3]) : return 'demonstration';
  123. case /^c$/.test(m[3]) :
  124. switch(true){
  125. case /^sc$/.test(m[4]): return 'scolie';
  126. case /^d$/.test(m[4]) : return 'demonstration';
  127. case /^sc$/.test(m[5]): return 'scolie';
  128. case /^\d$/.test(m[4]): return 'corollaire';
  129. case !m[4] : return 'corollaire';
  130. }
  131. case /^a$/.test(m[3]) : return 'prop-axiom';
  132. case /^l$/.test(m[3]) :
  133. switch(true){
  134. case /^d$/.test(m[5]) : return 'lemme-demonstration';
  135. case /^sc$/.test(m[5]): return 'lemme-scolie';
  136. case /^c$/.test(m[5]) : return 'lemme-corrollaire';
  137. case !m[5] : return 'lemme';
  138. }
  139. case /^p$/.test(m[3]) : return 'postulat';
  140. case /^\d$/.test(m[3]) : return '??';
  141. case /^\d{2}$/.test(m[3]) : return '??';
  142. case !m[3] : return 'proposition';
  143. }
  144. case /^app|ap$/.test(m[2]) : return 'appendice';
  145. case /^agd$/.test(m[2]) : return 'def-gen-affect';
  146. case /^pr$/.test(m[2]) : return 'preface';
  147. case /^ad$/.test(m[2]) :
  148. switch(true){
  149. case /^e$/.test(m[4]) :return 'explication';
  150. case !m[4] :return 'def-affect';
  151. }
  152. case /^c$/.test(m[2]) : return 'chapitre';
  153. case /^p$/.test(m[2]) : return 'postulat';
  154. case /^d$/.test(m[2]) :
  155. switch(true){
  156. case /^e$/.test(m[4]) : return 'explication';
  157. case !m[4] : return 'definition';
  158. }
  159. case /^a$/.test(m[2]) : return 'axiom';
  160. }
  161. // }
  162. }
  163. // console.log(`${this.id} -> ${this.dottype}`,m);
  164. // TODO: fix false ids
  165. // we have app and ap for appendice (1app | 4ap)
  166. // 213def ??
  167. // 209cd demo ou corollaire-demo ??
  168. // 210csc scolie ou corollaire-scolie ??
  169. // 213l1d demo ??
  170. },
  171. parseStrct(callback){
  172. var id, item, obj, links_match, link, tid;
  173. for (id in this.data_byid[this.langs[0].lc]) {
  174. item = this.data_byid[this.langs[0].lc][id];
  175. // console.log(item);
  176. // skeep titles as they don't have structure data
  177. if(item.type == "title") continue;
  178. obj = {
  179. 'to':[],
  180. 'from':[],
  181. };
  182. // get links
  183. links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
  184. // console.log(links_match);
  185. // if links exist on text
  186. if(links_match){
  187. for(link of links_match){
  188. // for(i in links_match){
  189. // link = links_match[i];
  190. // console.log(link);
  191. // get the target id
  192. tid = link.match(/\((.+)\)/)[1];
  193. // avoid duplicates
  194. if (obj.to.indexOf(tid) == -1)
  195. obj.to.push(tid);
  196. // add id to "from" links in target
  197. // if target exists
  198. if(typeof this.data_strct[tid] !== 'undefined'){
  199. // avoid duplicates
  200. if (this.data_strct[tid].from.indexOf(tid) == -1)
  201. this.data_strct[tid].from.push(id);
  202. }else{
  203. // if targets does not exists, the db has an issue, warn about that
  204. // console.log(`!! warning : ${tid} target id does not exists`);
  205. }
  206. }
  207. }
  208. // add the item links to the main links listings
  209. this.data_strct[id] = obj;
  210. }
  211. // console.log('data_strct',this.data_strct);
  212. callback();
  213. }
  214. }