Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.83 KB | None | 0 0
  1. import * as jsPDF from 'jspdf';
  2. import * as Blob from 'blob';
  3.  
  4. // var JsonData = [ [ { publication_id: 14,
  5. // country: 'Nepal',
  6. // editor: 'Editor',
  7. // volume: 123,
  8. // edition: null,
  9. // ISBN: '90232131',
  10. // Page_number: null,
  11. // Chapter_number: null,
  12. // Book_author: null,
  13. // chapter_title: null,
  14. // author_id: 6,
  15. // user_email: 'sobit.neupane@gmail.com',
  16. // title: 'Title',
  17. // year: 232,
  18. // publisher: 'Publisher',
  19. // city: 'Kathmandu',
  20. // doi: 'doi',
  21. // impact_factor: null,
  22. // ranked: null,
  23. // rank: null,
  24. // peer_reviewed: null,
  25. // type_of_publication: 'book',
  26. // authors: 'Sobit Neupane' } ],
  27. // [ { publication_id: 15,
  28. // editor: 'Ram Sharma',
  29. // volume: 5,
  30. // journal_name: 'Science',
  31. // page_number: '10-20',
  32. // month: '12',
  33. // day: '10',
  34. // issue: null,
  35. // ISSN: null,
  36. // author_id: 7,
  37. // user_email: 'sobit.neupane@gmail.com',
  38. // title: 'Messenger',
  39. // year: 2019,
  40. // publisher: 'American Association for the Advancement of Science',
  41. // city: 'Kathmandu',
  42. // doi: null,
  43. // impact_factor: null,
  44. // ranked: null,
  45. // rank: null,
  46. // peer_reviewed: null,
  47. // type_of_publication: 'journal',
  48. // authors: 'Prasanga Dhungel, Sobit Neupane' } ],
  49. // [ { publication_id: 12,
  50. // country: 'dfds',
  51. // editor: 'dfd',
  52. // volume: null,
  53. // edition: null,
  54. // ISBN: null,
  55. // Page_number: '343-55',
  56. // Chapter_number: 34,
  57. // Book_author: 'dfa',
  58. // chapter_title: 'asdfadfdsf',
  59. // author_id: 5,
  60. // user_email: 'sobit.neupane@gmail.com',
  61. // title: 'asdfdffd',
  62. // year: 234,
  63. // publisher: 'efedf',
  64. // city: 'df',
  65. // doi: null,
  66. // impact_factor: null,
  67. // ranked: null,
  68. // rank: null,
  69. // peer_reviewed: null,
  70. // type_of_publication: 'chapter',
  71. // authors: ' dfasddfd' },
  72. // { publication_id: 19,
  73. // country: 'Nepal',
  74. // editor: 'noone',
  75. // volume: 5,
  76. // edition: 3,
  77. // ISBN: 988-1231-23131,
  78. // Page_number: '343-55',
  79. // Chapter_number: 3,
  80. // Book_author: 'Prasanga Dhungel',
  81. // chapter_title: 'Somewhere something incredible is waiting to be known',
  82. // author_id: 5,
  83. // user_email: 'sobit.neupane@gmail.com',
  84. // title: 'Universe and Galaxies',
  85. // year: 2014,
  86. // publisher: 'MIT Press',
  87. // city: 'Maschacuttes',
  88. // doi: '10.12310.121.arXiv.org',
  89. // impact_factor: 'yes',
  90. // ranked: 'yes',
  91. // rank: 5,
  92. // peer_reviewed: 'yes',
  93. // type_of_publication: 'chapter',
  94. // authors: 'Prasanga Dhungel, Albert Einstein' } ] ]
  95.  
  96.  
  97. function downloader(JsonData, bibFormat, fileFormat){
  98. console.log("JsonData is",JsonData)
  99. if(bibFormat === 'mla'){
  100. var mlastr = "<html><body style= 'font-family:Times New Roman font-size: 12px'>";
  101. for (var k = 0; k<JsonData.length; k++){
  102. if (JsonData[k].length > 0 && JsonData[k][0].hasOwnProperty("type_of_publication"))
  103. mlastr += "<p><b>"+ JsonData[k][0].type_of_publication.toUpperCase() +"</b></p>";
  104. for(var i=0; i<JsonData[k].length; i++){
  105. mlastr += "<p>";
  106. mlastr += '['+(i+1).toString()+"] ";
  107. var authorsList = JsonData[k][i].authors.split(",");
  108. for(var j= 0; j<authorsList.length; j++){
  109. var authorstr = authorsList[j];
  110. var authorstrList = authorstr.split(' ');
  111. mlastr += authorstrList[authorstrList.length -1].charAt(0).toUpperCase() + authorstrList[authorstrList.length -1].slice(1)+", ";
  112. for(var abc = 0; abc < authorstrList.length -2 ; abc ++){
  113. mlastr += authorstrList[abc]+' ';
  114. }
  115. if(authorstrList.length >= 2){
  116. mlastr += authorstrList[authorstrList.length - 2].charAt(0).toUpperCase() + authorstrList[authorstrList.length - 2].slice(1);
  117. }
  118. if(j == authorsList.length - 2){
  119. mlastr += ", and ";
  120. }
  121. else if(j < authorsList.length - 2){
  122. mlastr += ", ";
  123. }
  124. }
  125. if(JsonData[k][i].hasOwnProperty("chapter_title") && JsonData[k][i].chapter_title != null){
  126. mlastr += `. "`+JsonData[k][i].chapter_title+`" `;
  127. if(JsonData[k][i].hasOwnProperty("book_title") && JsonData[k][i].book_title != null)
  128. mlastr += ". <i>"+JsonData[k][i].book_title + '</i>';
  129. if(JsonData[k][i].hasOwnProperty("title") && JsonData[k][i].title != null)
  130. mlastr += ". <i>"+JsonData[k][i].title + '</i>';
  131. }
  132. else{
  133. mlastr += ". <i>"+JsonData[k][i].title+"</i>";
  134. }
  135.  
  136. if(JsonData[k][i].hasOwnProperty("journal_name") && JsonData[k][i].journal_name != null)
  137. mlastr += ". In "+ JsonData[k][i].journal_name ;
  138.  
  139. if(JsonData[k][i].hasOwnProperty("conference_publication_name") && JsonData[k][i].conference_publication_name != null)
  140. mlastr += ". In Proceedings of "+ JsonData[k][i].conference_publication_name;
  141.  
  142. if(JsonData[k][i].hasOwnProperty("chapter_number") && JsonData[k][i].chapter_number != null)
  143. mlastr += ", chap."+ JsonData[k][i].chapter_number;
  144.  
  145.  
  146. if(JsonData[k][i].hasOwnProperty("issn") && JsonData[k][i].issn != null)
  147. mlastr += ", ISSN: "+ JsonData[k][i].issn;
  148.  
  149. if(JsonData[k][i].hasOwnProperty("isbn") && JsonData[k][i].isbn != null)
  150. mlastr += ", ISBN: "+ JsonData[k][i].isbn;
  151.  
  152. if(JsonData[k][i].hasOwnProperty("doi") && JsonData[k][i].doi != null)
  153. mlastr += ", DOI: "+ JsonData[k][i].doi;
  154.  
  155. if(JsonData[k][i].hasOwnProperty("ISSN") && JsonData[k][i].ISSN != null)
  156. mlastr += ", ISSN: "+ JsonData[k][i].ISSN;
  157.  
  158. if(JsonData[k][i].hasOwnProperty("ISBN") && JsonData[k][i].ISBN != null)
  159. mlastr += ", ISBN: "+ JsonData[k][i].ISBN;
  160.  
  161. if(JsonData[k][i].hasOwnProperty("DOI") && JsonData[k][i].DOI != null)
  162. mlastr += ", DOI: "+ JsonData[k][i].DOI;
  163.  
  164. if(JsonData[k][i].hasOwnProperty("volume") && JsonData[k][i].volume != null)
  165. mlastr += ", vol. "+ JsonData[k][i].volume;
  166.  
  167. if(JsonData[k][i].hasOwnProperty("city") && JsonData[k][i].city != null){
  168. mlastr += ", " + JsonData[k][i].city;
  169. if(JsonData[k][i].hasOwnProperty("country") && JsonData[k][i].country != null)
  170. mlastr += ", "+JsonData[k][i].country + ": ";
  171. else
  172. mlastr += ": " ;
  173. }
  174. if(JsonData[k][i].hasOwnProperty("publisher") && JsonData[k][i].publisher != null && !JsonData[k][i].hasOwnProperty("journal_name") && !JsonData[k][i].hasOwnProperty("conference_publication_name"))
  175. mlastr += JsonData[k][i].publisher;
  176.  
  177. if(JsonData[k][i].hasOwnProperty("page_number") && JsonData[k][i].page_number != null)
  178. mlastr += ", pp. "+ JsonData[k][i].page_number;
  179.  
  180. mlastr += " (";
  181. if(JsonData[k][i].hasOwnProperty("month") && JsonData[k][i].month != null)
  182. mlastr += JsonData[k][i].month + " ";
  183.  
  184. if(JsonData[k][i].hasOwnProperty("day") && JsonData[k][i].day != null)
  185. mlastr += JsonData[k][i].day + ", ";
  186.  
  187. mlastr += JsonData[k][i].year +")";
  188. mlastr += "</p>";
  189. }
  190. }
  191. mlastr += "</body></html>";
  192. if(fileFormat === 'pdf'){
  193. var doc = new jsPDF('p', 'pt', 'letter');
  194. doc.fromHTML(mlastr, 45, 30, {width: 517}); //Convert html string into jsPDF instances
  195. doc.save('MLA.pdf');
  196. }
  197. if(fileFormat === 'docx'){
  198. var blob = new Blob(['\ufeff', mlastr], { //converts html string into blob(Binary Large Object) of type msword
  199. type: 'application/msword'
  200. });
  201. var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(mlastr);
  202. var filename = "MLA.docx"; //file is downloaded as MLA.docx, user can change it while downloading
  203. var downloadLink = document.createElement("a");
  204. document.body.appendChild(downloadLink);
  205. if(navigator.msSaveOrOpenBlob ){
  206. navigator.msSaveOrOpenBlob(blob, filename);
  207. }else{
  208. downloadLink.href = url;
  209. downloadLink.download = filename;
  210. downloadLink.click();
  211. }
  212. document.body.removeChild(downloadLink);
  213. }
  214. if(fileFormat === 'html'){
  215. var element = document.createElement('a');
  216. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(mlastr));
  217. element.setAttribute('download', "MLA.html");
  218. element.style.display = 'none';
  219. document.body.appendChild(element);
  220. element.click();
  221. document.body.removeChild(element);
  222. }
  223. }
  224. if(bibFormat == 'apa'){
  225. var apastr = "<html><body style= 'font-family:Times New Roman font-size: 12px'>"; //apa formatted html string
  226. for(var k = 0; k<JsonData.length; k++){
  227. if (JsonData[k].length > 0 && JsonData[k][0].hasOwnProperty("type_of_publication"))
  228. apastr += "<p><b>"+ JsonData[k][0].type_of_publication.toUpperCase() +"</b></p>";
  229. for(var i=0; i<JsonData[k].length; i++){ //This loop prepares required html string
  230. apastr += "<p>";
  231. apastr += '['+(i+1).toString()+"] ";
  232. var authorsList = JsonData[k][i].authors.split(",");
  233. for(var j= 0; j<authorsList.length; j++){
  234. var authorstr = authorsList[j];
  235. var authorstrList = authorstr.split(' ');
  236. apastr += authorstrList[authorstrList.length -1].charAt(0).toUpperCase() + authorstrList[authorstrList.length -1].slice(1) + ", ";
  237. for(var abc = 0; abc < authorstrList.length -2 ; abc ++){
  238. if(authorstr[0] == ' ' && abc == 0)
  239. continue;
  240. apastr += authorstrList[abc][0].toUpperCase()+'. ';
  241. }
  242. if(authorstrList.length >= 2){
  243. apastr += authorstrList[authorstrList.length - 2][0]+'.';
  244. }
  245. if(j == authorsList.length - 2){
  246. apastr += ", & ";
  247. }
  248. else if(j < authorsList.length - 2){
  249. apastr += ", ";
  250. }
  251. }
  252.  
  253. apastr += "(" + JsonData[k][i].year ;
  254. if(JsonData[k][i].hasOwnProperty("month") && JsonData[k][i].month != null)
  255. apastr += ", " + JsonData[k][i].month + " ";
  256.  
  257. if(JsonData[k][i].hasOwnProperty("day") && JsonData[k][i].day != null)
  258. apastr += JsonData[k][i].day;
  259.  
  260. apastr += ")";
  261.  
  262.  
  263.  
  264. if(JsonData[k][i].hasOwnProperty("chapter_title") && JsonData[k][i].chapter_title != null){
  265. apastr += `. "`+JsonData[k][i].chapter_title+`." `;
  266. if(JsonData[k][i].hasOwnProperty("book_title") && JsonData[k][i].book_title != null)
  267. apastr += "<i>"+JsonData[k][i].book_title + '.</i>';
  268. if(JsonData[k][i].hasOwnProperty("title") && JsonData[k][i].title != null)
  269. apastr += "<i>"+JsonData[k][i].title + '.</i>';
  270.  
  271. }
  272. else{
  273. apastr += ". <i>"+JsonData[k][i].title+". </i>";
  274. }
  275.  
  276. if(JsonData[k][i].hasOwnProperty("city") && JsonData[k][i].city != null){
  277. apastr += JsonData[k][i].city;
  278. if(JsonData[k][i].hasOwnProperty("country") && JsonData[k][i].country != null)
  279. apastr += ", "+JsonData[k][i].country + ": ";
  280. else
  281. apastr += ": " ;
  282. }
  283. if(JsonData[k][i].hasOwnProperty("publisher") && JsonData[k][i].publisher != null && !JsonData[k][i].hasOwnProperty("journal_name") && !JsonData[k][i].hasOwnProperty("conference_publication_name"))
  284. apastr += JsonData[k][i].publisher;
  285.  
  286. if(JsonData[k][i].hasOwnProperty("journal_name") && JsonData[k][i].journal_name != null)
  287. apastr += " In "+ JsonData[k][i].journal_name;
  288.  
  289. if(JsonData[k][i].hasOwnProperty("conference_publication_name") && JsonData[k][i].conference_publication_name != null)
  290. apastr += " In Proceedings of "+ JsonData[k][i].conference_publication_name;
  291.  
  292. if(JsonData[k][i].hasOwnProperty("chapter_number") && JsonData[k][i].chapter_number != null)
  293. apastr += ", chap."+ JsonData[k][i].chapter_number;
  294.  
  295. if(JsonData[k][i].hasOwnProperty("issn") && JsonData[k][i].issn != null)
  296. apastr += ", ISSN: "+ JsonData[k][i].issn ;
  297.  
  298. if(JsonData[k][i].hasOwnProperty("isbn") && JsonData[k][i].isbn != null)
  299. apastr += ", ISBN: "+ JsonData[k][i].isbn;
  300.  
  301. if(JsonData[k][i].hasOwnProperty("doi") && JsonData[k][i].doi != null)
  302. apastr += ", DOI: "+ JsonData[k][i].doi ;
  303.  
  304. if(JsonData[k][i].hasOwnProperty("ISSN") && JsonData[k][i].ISSN != null)
  305. apastr += ", ISSN: "+ JsonData[k][i].ISSN ;
  306.  
  307. if(JsonData[k][i].hasOwnProperty("isbn") && JsonData[k][i].ISBN != null)
  308. apastr += ", ISBN: "+ JsonData[k][i].ISBN ;
  309.  
  310. if(JsonData[k][i].hasOwnProperty("DOI") && JsonData[k][i].DOI != null)
  311. apastr += ", DOI: "+ JsonData[k][i].DOI;
  312.  
  313. if(JsonData[k][i].hasOwnProperty("volume") && JsonData[k][i].volume != null)
  314. apastr += ", vol. "+ JsonData[k][i].volume;
  315.  
  316. if(JsonData[k][i].hasOwnProperty("page_number") && JsonData[k][i].page_number != null)
  317. apastr += ", pp. "+ JsonData[k][i].page_number ;
  318.  
  319. apastr += ".</p>";
  320. }
  321. }
  322. apastr += "</body></html>";
  323. if(fileFormat === 'pdf'){
  324. var doc = new jsPDF('p', 'pt', 'letter');
  325. doc.fromHTML(apastr, 45, 30, {width: 517}); //Convert html string into jsPDF instances
  326. doc.save('APA.pdf');
  327. }
  328. if(fileFormat === 'docx'){
  329. var blob = new Blob(['\ufeff', apastr], { //converts html string into blob(Binary Large Object) of type msword
  330. type: 'application/msword'
  331. });
  332. var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(apastr);
  333. var filename = "APA.docx"; //file is downloaded as MLA.docx, user can change it while downloading
  334. var downloadLink = document.createElement("a");
  335. document.body.appendChild(downloadLink);
  336. if(navigator.msSaveOrOpenBlob ){
  337. navigator.msSaveOrOpenBlob(blob, filename);
  338. }else{
  339. downloadLink.href = url;
  340. downloadLink.download = filename;
  341. downloadLink.click();
  342. }
  343. document.body.removeChild(downloadLink);
  344. }
  345. if(fileFormat === 'html'){
  346. var element = document.createElement('a');
  347. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(apastr));
  348. element.setAttribute('download', "APA.html");
  349. element.style.display = 'none';
  350. document.body.appendChild(element);
  351. element.click();
  352. document.body.removeChild(element);
  353. }
  354. }
  355. if(bibFormat === 'ieee'){
  356. var ieeestr = "<html><body style= 'font-family:Times New Roman; font-size: 11px'>"; //ieee formatted html string
  357. for(var k = 0; k < JsonData.length; k++){
  358. if (JsonData[k].length > 0 && JsonData[k][0].hasOwnProperty("type_of_publication"))
  359. ieeestr += "<p><b>"+ JsonData[k][0].type_of_publication.toUpperCase() +"</b></p>"; //str is a html string
  360. for(var i=0; i<JsonData[k].length; i++){
  361. ieeestr+= "<p>"
  362. ieeestr += '['+(i+1).toString()+"] ";
  363. var authorsList = JsonData[k][i].authors.split(",");
  364. for(var j= 0; j<authorsList.length; j++){
  365. var authorstr = authorsList[j].charAt(0).toUpperCase() + authorsList[j].slice(1);
  366. var authorstrList = authorstr.split(' ');
  367. if(authorstrList.length > 1){
  368. if(authorstr[0] != ' '){
  369. ieeestr += authorstrList[0][0].toUpperCase()+". ";
  370. var star = 1;
  371. }
  372. else{
  373. ieeestr += authorstrList[1][0].toUpperCase()+". ";
  374. var star = 2;
  375. }
  376. }
  377. else
  378. ieeestr += authorstrList[0].charAt(0).toUpperCase() + authorstrList[0].slice(1)+". ";
  379. for(var abc = star; abc <= authorstrList.length -2 ; abc++){
  380. ieeestr += authorstrList[abc][0].toUpperCase()+'. ';
  381. }
  382. ieeestr += authorstrList[authorstrList.length - 1];
  383. if(j == authorsList.length - 2){
  384. ieeestr += ", and ";
  385. }
  386. else if(j < authorsList.length - 2){
  387. ieeestr += ", ";
  388. }
  389. }
  390.  
  391. if(JsonData[k][i].hasOwnProperty("chapter_title") && JsonData[k][i].chapter_title != null){
  392. ieeestr += `. "`+JsonData[k][i].chapter_title+`." `;
  393. if(JsonData[k][i].hasOwnProperty("book_title") && JsonData[k][i].book_title != null)
  394. ieeestr += "<i>"+JsonData[k][i].book_title + '.</i>';
  395. if(JsonData[k][i].hasOwnProperty("title") && JsonData[k][i].title != null)
  396. ieeestr += "<i>"+JsonData[k][i].title + '.</i>';
  397. }
  398. else{
  399. ieeestr += ". <i>"+JsonData[k][i].title+". </i>";
  400. }
  401.  
  402. if(JsonData[k][i].hasOwnProperty("city") && JsonData[k][i].city != null){
  403. ieeestr += JsonData[k][i].city;
  404. if(JsonData[k][i].hasOwnProperty("country") && JsonData[k][i].country != null)
  405. ieeestr += ", "+JsonData[k][i].country + ": ";
  406. else
  407. ieeestr += ": " ;
  408. }
  409. if(JsonData[k][i].hasOwnProperty("publisher") && JsonData[k][i].publisher != null)
  410. ieeestr += JsonData[k][i].publisher + ", ";
  411.  
  412. if(JsonData[k][i].hasOwnProperty("journal_name") && JsonData[k][i].journal_name != null)
  413. ieeestr += "In Journal of "+ JsonData[k][i].journal_name + ", ";
  414.  
  415. if(JsonData[k][i].hasOwnProperty("conference_publication_name") && JsonData[k][i].conference_publication_name != null)
  416. ieeestr += "In Proceedings of "+ JsonData[k][i].conference_publication_name + ", ";
  417.  
  418. ieeestr += "(" + JsonData[k][i].year ;
  419. if(JsonData[k][i].hasOwnProperty("month") && JsonData[k][i].month != null)
  420. ieeestr += ", " + JsonData[k][i].month + " ";
  421.  
  422. if(JsonData[k][i].hasOwnProperty("day") && JsonData[k][i].day != null)
  423. ieeestr += JsonData[k][i].day;
  424.  
  425. ieeestr += ").";
  426.  
  427. ieeestr += "</p>";
  428. }
  429. }
  430. ieeestr += "</body></html>" ;
  431. if(fileFormat === 'pdf'){
  432. var doc = new jsPDF('p', 'pt', 'letter');
  433. doc.fromHTML(ieeestr, 45, 30, {width: 517}); //Convert html string into jsPDF instances
  434. doc.save('IEEE.pdf');
  435. }
  436. if(fileFormat === 'docx'){
  437. var blob = new Blob(['\ufeff', ieeestr], { //converts html string into blob(Binary Large Object) of type msword
  438. type: 'application/msword'
  439. });
  440. var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(ieeestr);
  441. var filename = "IEEE.docx"; //file is downloaded as MLA.docx, user can change it while downloading
  442. var downloadLink = document.createElement("a");
  443. document.body.appendChild(downloadLink);
  444. if(navigator.msSaveOrOpenBlob ){
  445. navigator.msSaveOrOpenBlob(blob, filename);
  446. }else{
  447. downloadLink.href = url;
  448. downloadLink.download = filename;
  449. downloadLink.click();
  450. }
  451. document.body.removeChild(downloadLink);
  452. }
  453. if(fileFormat === 'html'){
  454. var element = document.createElement('a');
  455. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(ieeestr));
  456. element.setAttribute('download', "IEEE.html");
  457. element.style.display = 'none';
  458. document.body.appendChild(element);
  459. element.click();
  460. document.body.removeChild(element);
  461. }
  462. }
  463.  
  464. if(bibFormat === 'ugc'){
  465. var ugcstr = "<html><body style= 'font-family:Times New Roman; font-size: 12px'>"; //ugc table formatted html string
  466. ugcstr += "<table style='width:100%; table-layout=fixed; border: 1px solid black; border-collapse: collapse'>";
  467. var nonugc = "";
  468. ugcstr += "<tr><td style = 'border: 1px solid black; border-collapse: collapse' colspan='3'>Major Research Publication in <strong>Ranked Journals/Proceedings</strong></td></tr>"; //str is a html string
  469. nonugc += "<tr><td style = 'border: 1px solid black; border-collapse: collapse' colspan='3'>Major Research Publication in <strong>Non-Ranked Peer-Reviewed Journals</strong></td></tr>"; //str is a html string
  470. ugcstr += "<tr><td></td><td style = 'border: 1px solid black; border-collapse: collapse'>Format: Authors, Title, Journal, Volume(Number), First page-Last page, (Year)</td> <td style = 'border: 1px solid black; border-collapse: collapse'>Rank*/IF</td></tr>";
  471. nonugc += "<tr><td></td><td style = 'border: 1px solid black; border-collapse: collapse'>Format: Authors, Title, Journal, Volume(Number), First page-Last page, (Year)</td> <td style = 'border: 1px solid black; border-collapse: collapse'>Country</td></tr>";
  472. for (var k = 0; k< JsonData.length; k++){
  473. for(var i=0; i<JsonData[k].length; i++){
  474. if(JsonData.hasOwnProperty("ranked") && JsonData.ranked.toUpperCase === "YES"){
  475. ugcstr += "<tr><td style = 'border: 1px solid black; border-collapse: collapse'>";
  476. ugcstr += (i+1).toString();
  477. ugcstr += "</td><td style = 'border: 1px solid black; border-collapse: collapse'>";
  478. var authorsList = JsonData[k][i].authors.split(",");
  479. for(var j= 0; j<authorsList.length; j++){
  480. var authorstr = authorsList[j];
  481. var authorstrList = authorstr.split(' ');
  482. if(authorstrList.length > 1){
  483. if(authorstr[0] != ' '){
  484. ugcstr += authorstrList[0][0]+". ";
  485. var star = 1;
  486. }
  487. else{
  488. ugcstr += authorstrList[1][0]+". ";
  489. var star = 2;
  490. }
  491. }
  492. else
  493. ugcstr += authorstrList[0]+". ";
  494. for(var abc = star; abc <= authorstrList.length -2 ; abc++){
  495. ugcstr += authorstrList[abc][0]+'. ';
  496. }
  497. ugcstr += authorstrList[authorstrList.length - 1] ;
  498. if(j == authorsList.length - 2){
  499. ugcstr += ", and ";
  500. }
  501. else if(j < authorsList.length - 2){
  502. ugcstr += ", ";
  503. }
  504. }
  505.  
  506. if(JsonData[k][i].hasOwnProperty("chapter_title") && JsonData[k][i].chapter_title != null){
  507. ugcstr += `, "`+JsonData[k][i].chapter_title+`." `;
  508. if(JsonData[k][i].hasOwnProperty("book_title") && JsonData[k][i].book_title != null)
  509. ugcstr += "<i>"+JsonData[k][i].book_title + ', </i>';
  510. if(JsonData[k][i].hasOwnProperty("title") && JsonData[k][i].title != null)
  511. ugcstr += "<i>"+JsonData[k][i].title + ', </i>';
  512. }
  513. else{
  514. ugcstr += ", <i>"+JsonData[k][i].title+", </i>";
  515. }
  516.  
  517.  
  518. if(JsonData[k][i].hasOwnProperty("journal_name") && JsonData[k][i].journal_name != null)
  519. ugcstr += JsonData[k][i].journal_name + ", ";
  520.  
  521.  
  522. else if(JsonData[i].hasOwnProperty("conference_publication_name") && JsonData[k][i].conference_publication_name != null)
  523. ugcstr += JsonData[i].conference_publication_name + ", ";
  524.  
  525. else ugcstr += "- , ";
  526.  
  527. if(JsonData[k][i].hasOwnProperty("volume") && JsonData[k][i].volume != null)
  528. ugcstr += "vol. "+ JsonData[i].volume ;
  529.  
  530. else ugcstr += "- , ";
  531.  
  532.  
  533. if(JsonData[k][i].hasOwnProperty("page_number") && JsonData[k][i].page_number != null)
  534. ugcstr += "pp. "+ JsonData[i].page_number ;
  535.  
  536. else ugcstr += "- , ";
  537.  
  538. ugcstr += "(" + JsonData[k][i].year + ").";
  539. ugcstr += "</td><td style = 'border: 1px solid black; border-collapse: collapse'>";
  540. if(JsonData[k][i].hasOwnProperty("rank") && JsonData[k][i].rank != null){
  541. ugcstr += JsonData[k][i].rank + "/";
  542. }
  543. else ugcstr += "- /";
  544.  
  545. if(JsonData[k][i].hasOwnProperty("impact_factor") && JsonData[k][i].impact_factor != null){
  546. ugcstr += JsonData[k][i].impact_factor + "/";
  547. }
  548. else ugcstr += "-";
  549. ugcstr += "</td>";
  550. ugcstr +="</tr>";
  551. }
  552. else{
  553. nonugc += "<tr><td style = 'border: 1px solid black; border-collapse: collapse'>";
  554. nonugc += (i+1).toString();
  555. nonugc += "</td><td style = 'border: 1px solid black; border-collapse: collapse'>";
  556. var authorsList = JsonData[k][i].authors.split(",");
  557. for(var j= 0; j<authorsList.length; j++){
  558. var authorstr = authorsList[j];
  559. var authorstrList = authorstr.split(' ');
  560. if(authorstrList.length > 1){
  561. if(authorstr[0] != ' '){
  562. nonugc += authorstrList[0][0]+". ";
  563. var star = 1;
  564. }
  565. else{
  566. nonugc += authorstrList[1][0]+". ";
  567. var star = 2;
  568. }
  569. }
  570. else
  571. nonugc += authorstrList[0]+". ";
  572. for(var abc = star; abc <= authorstrList.length -2 ; abc++){
  573. nonugc += authorstrList[abc][0]+'. ';
  574. }
  575. nonugc += authorstrList[authorstrList.length - 1] ;
  576. if(j == authorsList.length - 2){
  577. nonugc += ", and ";
  578. }
  579. else if(j < authorsList.length - 2){
  580. nonugc += ", ";
  581. }
  582. }
  583.  
  584. if(JsonData[k][i].hasOwnProperty("chapter_title") && JsonData[k][i].chapter_title != null){
  585. nonugc += `"`+JsonData[k][i].chapter_title+`." `;
  586. if(JsonData[k][i].hasOwnProperty("book_title") && JsonData[k][i].book_title != null)
  587. nonugc += "<i>"+JsonData[k][i].book_title + ', </i>';
  588. if(JsonData[k][i].hasOwnProperty("title") && JsonData[k][i].title != null)
  589. nonugc += "<i>"+JsonData[k][i].title + ', </i>';
  590. }
  591. else{
  592. nonugc += ", <i>"+JsonData[k][i].title+", </i>";
  593. }
  594.  
  595. if(JsonData[k][i].hasOwnProperty("journal_name") && JsonData[k][i].journal_name != null)
  596. nonugc += JsonData[k][i].journal_name + ", ";
  597.  
  598. else if(JsonData[k][i].hasOwnProperty("conference_publication_name") && JsonData[k][i].conference_publication_name != null)
  599. nonugc += JsonData[k][i].conference_publication_name + ", ";
  600.  
  601. else nonugc += "- , ";
  602.  
  603. if(JsonData[k][i].hasOwnProperty("volume") && JsonData[k][i].volume != null)
  604. nonugc += "vol. "+ JsonData[k][i].volume ;
  605.  
  606. else nonugc += "- , ";
  607.  
  608. if(JsonData[k][i].hasOwnProperty("page_number") && JsonData[k][i].page_number != null)
  609. nonugc += "pp. "+ JsonData[k][i].page_number ;
  610.  
  611. else nonugc += "- , ";
  612.  
  613. nonugc += "(" + JsonData[k][i].year + ").";
  614. nonugc += "</td><td style = 'border: 1px solid black; border-collapse: collapse'>";
  615. if(JsonData[k][i].hasOwnProperty("country") && JsonData[k][i].country != null){
  616. nonugc += JsonData[k][i].country;
  617. }
  618. else{
  619. nonugc += "-"
  620. }
  621. }
  622. }
  623. }
  624. ugcstr += nonugc;
  625. ugcstr += "</table>";
  626. ugcstr += "</body></html>" ;
  627. // if(fileFormat === 'pdf'){
  628. // // var doc = new jsPDF('p', 'pt', 'letter');
  629. // // doc.fromHTML(ugcstr, 45, 30, {width: 517}); //Convert html string into jsPDF instances
  630. // // doc.save('UGC.pdf');
  631. // var doc = new jsPDF('p', 'pt', 'a4');
  632. // var source = ugcstr;
  633. // var margins = {
  634. // top: 10,
  635. // bottom: 10,
  636. // left: 10,
  637. // width: 595
  638. // };
  639.  
  640. // doc.fromHTML(
  641. // source, // HTML string or DOM elem ref.
  642. // margins.left,
  643. // margins.top, {
  644. // 'width': margins.width,
  645. // },
  646.  
  647. // function(dispose) {
  648. // doc.save('Test.pdf');
  649. // }, margins);
  650.  
  651. // }
  652. if(fileFormat === 'docx' || fileFormat === 'pdf'){
  653. var blob = new Blob(['\ufeff', ugcstr], { //converts html string into blob(Binary Large Object) of type msword
  654. type: 'application/msword'
  655. });
  656. var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(ugcstr);
  657. var filename = "UGC.docx"; //file is downloaded as MLA.docx, user can change it while downloading
  658. var downloadLink = document.createElement("a");
  659. document.body.appendChild(downloadLink);
  660. if(navigator.msSaveOrOpenBlob ){
  661. navigator.msSaveOrOpenBlob(blob, filename);
  662. }else{
  663. downloadLink.href = url;
  664. downloadLink.download = filename;
  665. downloadLink.click();
  666. }
  667. document.body.removeChild(downloadLink);
  668. }
  669. if(fileFormat === 'html'){
  670. var element = document.createElement('a');
  671. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(ugcstr));
  672. element.setAttribute('download', "UGC.html");
  673. element.style.display = 'none';
  674. document.body.appendChild(element);
  675. element.click();
  676. document.body.removeChild(element);
  677. }
  678. }
  679.  
  680. }
  681. export default downloader;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement