dbs.js 9.8 KB

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