dbs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. load(callback) {
  76. // load all dbs, when all loaded call main app callback function
  77. for (var i = 0; i < this.langs.length; i++) {
  78. this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
  79. }
  80. },
  81. loadJSON(lc, file, callback){
  82. var xobj = new XMLHttpRequest();
  83. xobj.overrideMimeType("application/json");
  84. // TODO: load and unzip gziped json
  85. // xobj.setRequestHeader('Accept-Encoding', 'gzip');
  86. xobj.onreadystatechange = function () {
  87. // console.log('onreadystatechange', xobj.readyState);
  88. switch(xobj.readyState){
  89. case 3:
  90. // console.log('loading');
  91. break;
  92. break;
  93. case 4:
  94. if (xobj.status === 200) {
  95. this.onJSONLoaded(lc, xobj.responseText, callback);
  96. } else {
  97. console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
  98. }
  99. break;
  100. }
  101. }.bind(this);
  102. xobj.open('GET', file, true);
  103. xobj.send(null);
  104. },
  105. onJSONLoaded(lc, json, callback){
  106. // console.log('onDBLoaded '+lc);
  107. this.data[lc] = JSON.parse(json);
  108. this.loaded_dbs ++;
  109. //
  110. if (this.loaded_dbs == this.langs.length) {
  111. // console.log('All db loaded : data', this.data);
  112. this.parseByID(callback);
  113. }
  114. },
  115. parseByID(callback){
  116. // create a id keyed array of contents
  117. var e_id, c_id, cc_id;
  118. // loop through laguages
  119. for(let l in this.data){
  120. // console.log('l', l);
  121. this.data_byid[l] = {};
  122. // loop through parts
  123. for (let p in this.data[l]) {
  124. if(this.data[l][p].type !== "intro"){
  125. // console.log(this.data[l][p]);
  126. // loop through enonces
  127. for (let e in this.data[l][p].enonces) {
  128. // console.log('e',e);
  129. if(this.data[l][p].enonces[e].id){
  130. e_id = this.data[l][p].enonces[e].id;
  131. // guess the type from the id
  132. // console.log(this.data[l][p].enonces[e].id);
  133. this.data[l][p].enonces[e].dottype = this.setupType(e_id);
  134. // breadcrumb (full tree title)
  135. this.data[l][p].enonces[e].breadcrumb = this.setupBreadcrumb(e_id);
  136. // records childs in array keyed by ids
  137. this.data_byid[l][e_id] = this.data[l][p].enonces[e];
  138. }
  139. // loop through childs
  140. for (let c in this.data[l][p].enonces[e].childs){
  141. // console.log(_db[p][e][c]);
  142. if(this.data[l][p].enonces[e].childs[c].id){
  143. c_id = this.data[l][p].enonces[e].childs[c].id;
  144. // guess the type from the id
  145. this.data[l][p].enonces[e].childs[c].dottype = this.setupType(c_id);
  146. // breadcrumb (full tree title)
  147. this.data[l][p].enonces[e].childs[c].breadcrumb = this.setupBreadcrumb(c_id);
  148. // records childs in array keyed by ids
  149. this.data_byid[l][c_id] = this.data[l][p].enonces[e].childs[c];
  150. }
  151. // loop through childs of childs
  152. for (let cc in this.data[l][p].enonces[e].childs[c].childs){
  153. // console.log(_db[p][e][c]);
  154. if(this.data[l][p].enonces[e].childs[c].childs[cc].id){
  155. cc_id = this.data[l][p].enonces[e].childs[c].childs[cc].id;
  156. console.log('cc_id', cc_id);
  157. // guess the type from the id
  158. this.data[l][p].enonces[e].childs[c].childs[cc].dottype = this.setupType(cc_id);
  159. // breadcrumb (full tree title)
  160. this.data[l][p].enonces[e].childs[c].childs[cc].breadcrumb = this.setupBreadcrumb(cc_id);
  161. // records childs in array keyed by ids
  162. this.data_byid[l][cc_id] = this.data[l][p].enonces[e].childs[c].childs[cc];
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. // console.log('this.data_byid', this.data_byid);
  171. this.parseStrct(callback);
  172. },
  173. setupBreadcrumb(id){
  174. // /^(\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)?$/
  175. var breadcrumb_array = [];
  176. var m = id.match(this.rx_id);
  177. var mode = 'full';
  178. if(m){
  179. m.shift();
  180. // removing undefined
  181. m_clean = [];
  182. for (let i = 0; i < m.length; i++) {
  183. if(typeof m[i] !== 'undefined'){
  184. m_clean.push(m[i]);
  185. }
  186. }
  187. // console.log('m_clean', m_clean);
  188. for (let i = 0; i < m_clean.length; i++) { // we loop through match result variables
  189. // for (let i = m_clean.length-1; i >= 0; i--) { // we loop through match result variables in reverse order
  190. // console.log('m_clean['+i+']', m_clean[i]);
  191. if(i == 0){ // first digit is part number
  192. breadcrumb_array.unshift(`${this.id_strct[i]['dim']} ${m_clean[i]}`);
  193. }else{
  194. // display mode
  195. mode = i !== m_clean.length-1 ? 'dim' : 'full';
  196. if(isNaN(m_clean[i])){ // if not a number we get the label
  197. breadcrumb_array.unshift(`${this.id_strct[i][m_clean[i]][mode]}`);
  198. // breadcrumb_array.splice(1, 0, `${m_clean[i]}`);
  199. }else{ // if its a number
  200. if(i == 1){ // we just add the number to the breadcrumb preceded by 'Proposition'
  201. breadcrumb_array.unshift(`${this.id_strct[i]['prop'][mode]} ${m_clean[i]}`);
  202. }else{ // we just add the number to the breadcrumb behind the last added item
  203. // breadcrumb_array.unshift(`${m_clean[i]}`);
  204. // debugger;
  205. breadcrumb_array.splice(1, 0, `${m_clean[i]}`);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. // console.log('breadcrumb_array', breadcrumb_array);
  212. return breadcrumb_array.join(' ');
  213. },
  214. setupType(id){
  215. // console.log('setupType',id);
  216. // /^(\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)?$/
  217. var m = id.match(this.rx_id);
  218. if(m){
  219. switch(true){
  220. case /^\d{2}$/.test(m[2]): // proposition
  221. switch(true){
  222. case /^cd$/.test(m[3]) : return 'corollaire-demo';
  223. case /^sc$/.test(m[3]) : return 'scolie';
  224. case /^d$/.test(m[3]) : return 'demonstration';
  225. case /^c$/.test(m[3]) :
  226. switch(true){
  227. case /^sc$/.test(m[4]): return 'scolie';
  228. case /^d$/.test(m[4]) : return 'demonstration';
  229. case /^sc$/.test(m[5]): return 'scolie';
  230. case /^\d$/.test(m[4]): return 'corollaire';
  231. case !m[4] : return 'corollaire';
  232. }
  233. case /^a$/.test(m[3]) : return 'prop-axiom';
  234. case /^l$/.test(m[3]) :
  235. switch(true){
  236. case /^d$/.test(m[5]) : return 'lemme-demonstration';
  237. case /^sc$/.test(m[5]): return 'lemme-scolie';
  238. case /^c$/.test(m[5]) : return 'lemme-corrollaire';
  239. case !m[5] : return 'lemme';
  240. }
  241. case /^p$/.test(m[3]) : return 'postulat';
  242. case /^\d$/.test(m[3]) : return '??';
  243. case /^\d{2}$/.test(m[3]) : return '??';
  244. case !m[3] : return 'proposition';
  245. }
  246. case /^app|ap$/.test(m[2]) : return 'appendice';
  247. case /^agd$/.test(m[2]) : return 'def-gen-affect';
  248. case /^pr$/.test(m[2]) : return 'preface';
  249. case /^ad$/.test(m[2]) :
  250. switch(true){
  251. case /^e$/.test(m[4]) :return 'explication';
  252. case !m[4] :return 'def-affect';
  253. }
  254. case /^c$/.test(m[2]) : return 'chapitre';
  255. case /^p$/.test(m[2]) : return 'postulat';
  256. case /^d$/.test(m[2]) :
  257. switch(true){
  258. case /^e$/.test(m[4]) : return 'explication';
  259. case !m[4] : return 'definition';
  260. }
  261. case /^a$/.test(m[2]) : return 'axiom';
  262. }
  263. // }
  264. }
  265. // console.log(`${this.id} -> ${this.dottype}`,m);
  266. // TODO: fix false ids
  267. // we have app and ap for appendice (1app | 4ap)
  268. // 213def ??
  269. // 209cd demo ou corollaire-demo ??
  270. // 210csc scolie ou corollaire-scolie ??
  271. // 213l1d demo ??
  272. },
  273. parseStrct(callback){
  274. var id, item, obj, links_match, link, tid;
  275. for (id in this.data_byid[this.langs[0].lc]) {
  276. item = this.data_byid[this.langs[0].lc][id];
  277. // console.log(item);
  278. // skeep titles as they don't have structure data
  279. if(item.type == "title") continue;
  280. obj = {
  281. 'to':[],
  282. 'from':[],
  283. };
  284. // get links
  285. links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
  286. // console.log(links_match);
  287. // if links exist on text
  288. if(links_match){
  289. for(link of links_match){
  290. // for(i in links_match){
  291. // link = links_match[i];
  292. // console.log(link);
  293. // get the target id
  294. tid = link.match(/\((.+)\)/)[1];
  295. // avoid duplicates
  296. if (obj.to.indexOf(tid) == -1)
  297. obj.to.push(tid);
  298. // add id to "from" links in target
  299. // if target exists
  300. if(typeof this.data_strct[tid] !== 'undefined'){
  301. // avoid duplicates
  302. if (this.data_strct[tid].from.indexOf(tid) == -1)
  303. this.data_strct[tid].from.push(id);
  304. }else{
  305. // if targets does not exists, the db has an issue, warn about that
  306. // console.log(`!! warning : ${tid} target id does not exists`);
  307. }
  308. }
  309. }
  310. // add the item links to the main links listings
  311. this.data_strct[id] = obj;
  312. }
  313. // console.log('data_strct',this.data_strct);
  314. callback();
  315. }
  316. }