dbs.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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]):
  119. switch(true){
  120. case /^cd$/.test(m[3]) : return 'corollaire-demo';
  121. case /^cd$/.test(m[3]) : return 'corollaire-demo';
  122. case /^sc$/.test(m[3]) : return 'scolie';
  123. case /^sc$/.test(m[3]) : return 'scolie';
  124. case /^d$/.test(m[3]) : return 'demonstration';
  125. case /^d$/.test(m[3]) : return 'demonstration';
  126. case /^c$/.test(m[3]) :
  127. switch(true){
  128. case /^sc$/.test(m[4]): return 'scolie';
  129. case /^sc$/.test(m[4]): return 'scolie';
  130. case /^d$/.test(m[4]) : return 'demonstration';
  131. case /^d$/.test(m[4]) : return 'demonstration';
  132. case /^d$/.test(m[5]) : return 'demonstration';
  133. case /^d$/.test(m[5]) : return 'demonstration';
  134. case /^sc$/.test(m[5]): return 'scolie';
  135. case /^sc$/.test(m[5]): return 'scolie';
  136. case /^\d$/.test(m[4]): return 'corollaire';
  137. case /^\d$/.test(m[4]): return 'corollaire';
  138. case !m[4] : return 'corollaire';
  139. case !m[4] : return 'corollaire';
  140. }
  141. case /^a$/.test(m[3]) : return 'axiom';
  142. case /^a$/.test(m[3]) : return 'axiom';
  143. case /^l$/.test(m[3]) :
  144. switch(true){
  145. case /^d$/.test(m[5]) : return 'demonstration';
  146. case /^d$/.test(m[5]) : return 'demonstration';
  147. case /^sc$/.test(m[5]): return 'scolie';
  148. case /^sc$/.test(m[5]): return 'scolie';
  149. case !m[5] : return 'lemme';
  150. case !m[5] : return 'lemme';
  151. }
  152. case /^p$/.test(m[3]) : return 'postulat';
  153. case /^p$/.test(m[3]) : return 'postulat';
  154. case /^\d$/.test(m[3]) : return '??';
  155. case /^\d$/.test(m[3]) : return '??';
  156. case /^\d{2}$/.test(m[3]) : return '??';
  157. case /^\d{2}$/.test(m[3]) : return '??';
  158. case !m[3] : return 'proposition';
  159. case !m[3] : return 'proposition';
  160. }
  161. case /^app|ap$/.test(m[2]) : return 'appendice';
  162. case /^app|ap$/.test(m[2]) : return 'appendice';
  163. case /^agd$/.test(m[2]) : return 'def-gen-affect';
  164. case /^agd$/.test(m[2]) : return 'def-gen-affect';
  165. case /^pr$/.test(m[2]) : return 'preface';
  166. case /^pr$/.test(m[2]) : return 'preface';
  167. case /^ad$/.test(m[2]) :
  168. switch(true){
  169. case /^e$/.test(m[4]) :return 'explication';
  170. case /^e$/.test(m[4]) :return 'explication';
  171. case !m[4] :return 'def-affect';
  172. case !m[4] :return 'def-affect';
  173. }
  174. case /^c$/.test(m[2]) : return 'chapitre';
  175. case /^c$/.test(m[2]) : return 'chapitre';
  176. case /^p$/.test(m[2]) : return 'postulat';
  177. case /^p$/.test(m[2]) : return 'postulat';
  178. case /^d$/.test(m[2]) :
  179. switch(true){
  180. case /^e$/.test(m[4]) : return 'explication';
  181. case /^e$/.test(m[4]) : return 'explication';
  182. case !m[4] : return 'definition';
  183. case !m[4] : return 'definition';
  184. }
  185. case /^a$/.test(m[2]) : return 'axiom';
  186. case /^a$/.test(m[2]) : return 'axiom';
  187. }
  188. // }
  189. }
  190. // console.log(`${this.id} -> ${this.dottype}`,m);
  191. // TODO: fix false ids
  192. // we have app and ap for appendice (1app | 4ap)
  193. // 213def ??
  194. // 209cd demo ou corollaire-demo ??
  195. // 210csc scolie ou corollaire-scolie ??
  196. // 213l1d demo ??
  197. },
  198. parseStrct(callback){
  199. var id, item, obj, links_match, link, tid;
  200. for (id in this.data_byid[this.langs[0].lc]) {
  201. item = this.data_byid[this.langs[0].lc][id];
  202. // console.log(item);
  203. // skeep titles as they don't have structure data
  204. if(item.type == "title") continue;
  205. obj = {
  206. 'to':[],
  207. 'from':[],
  208. };
  209. // get links
  210. links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
  211. // console.log(links_match);
  212. // if links exist on text
  213. if(links_match){
  214. for(link of links_match){
  215. // for(i in links_match){
  216. // link = links_match[i];
  217. // console.log(link);
  218. // get the target id
  219. tid = link.match(/\((.+)\)/)[1];
  220. // avoid duplicates
  221. if (obj.to.indexOf(tid) == -1)
  222. obj.to.push(tid);
  223. // add id to "from" links in target
  224. // if target exists
  225. if(typeof this.data_strct[tid] !== 'undefined'){
  226. // avoid duplicates
  227. if (this.data_strct[tid].from.indexOf(tid) == -1)
  228. this.data_strct[tid].from.push(id);
  229. }else{
  230. // if targets does not exists, the db has an issue, warn about that
  231. // console.log(`!! warning : ${tid} target id does not exists`);
  232. }
  233. }
  234. }
  235. // add the item links to the main links listings
  236. this.data_strct[id] = obj;
  237. }
  238. // console.log('data_strct',this.data_strct);
  239. callback();
  240. }
  241. }