Advertisement
Guest User

mefi2book_index_array_bug_buildIndex

a guest
Jul 29th, 2015
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * buildIndex
  3.      *
  4.      *  builds the Contributors Index
  5.      * @memberof mefi2book
  6.      *
  7.      */
  8.     function buildIndex() {
  9.         var userNames = [], // an alphabetized array of all users in thread
  10.             userIndex = {}; // a hash of usernames, where each one has an array of page #s where their comments are
  11.         logger("Converting PDF to json");
  12.         extractpdf(CACHE_DIR+mefiSubSite+'_metafilter_'+mefiThreadNumber+'_first_pass.pdf', function (err, pages) {
  13.           if (err) {
  14.             logger("PDF extract for Contributor Index erroring out");
  15.             done(err);
  16.           } else {
  17.             logger("Found " + pages.length + " pages");
  18.  
  19.             pages.forEach( function(element, index, array) {
  20.                 var currentPage = index + 1;
  21.                 logger("Finding contributors - page: "+currentPage);
  22.                 // find the names of the authors of each comment on the page
  23.                 var usersOnPage = element.match(new RegExp("   — (.*)\n", "g")) || [];
  24.                 logger("Cleaning up names - page: "+currentPage);
  25.                 // clean up the names (shouldn't have to do this, what's the right way?)
  26.                 usersOnPage.forEach( function(element, index, array) {
  27.                     array[index] = element.match(new RegExp("   — (.*)\n"))[1];
  28.                 });
  29.                 //
  30.                 usersOnPage.forEach( function(element, index, array) {
  31.                     var userArray;
  32.                     if ( userIndex[element] ) {
  33.                         userArray = userIndex[element];
  34.                         if ( userArray[userArray.length-1] != currentPage ) {
  35.                             userArray[userArray.length] = currentPage;
  36.                         }
  37.                     } else {
  38.                         userNames.push(element);
  39.                         userIndex[element] = [];
  40.                         userArray = userIndex[element];
  41.                         userArray[0] = currentPage;
  42.                     }
  43.                 });
  44.             });
  45.             logger("Sorting contributors' usernames");
  46.             userNames.sort(function (a, b) {
  47.                 return a.toLowerCase().localeCompare(b.toLowerCase());
  48.             });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement