Example #1
0
File: index.js Project: rtorr/d2h
fs.readFile(argv.f, 'utf8', (err, data) => {
  if (err) throw err;
  var json_data = diff2Html.getJsonFromDiff(data, config);
  console.log(`Writing html to ${argv.f}.html`);
  fs.writeFile(`${argv.f}.html`, container(diff2Html.getPrettyHtml(json_data, config)), (err) => {
    if (err) throw err;
    console.log('It\'s saved!');
  });
});
Example #2
0
  }).then(function() {
    if (!self.diffJson || self.diffJson.length == 0) return; // check if diffs are available (binary files do not support them)
    var lineCount = 0;

    if (!self.diffJson[0].isTrimmed) {
      self.diffJson[0].blocks = self.diffJson[0].blocks.reduce(function(blocks, block) {
        var length = block.lines.length;
        if (lineCount < self.loadCount) {
          block.lines = block.lines.slice(0, self.loadCount - lineCount);
          blocks.push(block);
        }
        lineCount += length;
        return blocks;
      }, []);
    }
    self.diffJson[0].isTrimmed = true;

    self.loadMoreCount(Math.min(loadLimit, Math.max(0, lineCount - self.loadCount)));

    var html;

    if (self.textDiffType.value() === 'sidebysidediff') {
      html = diff2html.getPrettySideBySideHtmlFromJson(self.diffJson);
    } else {
      html = diff2html.getPrettyHtmlFromJson(self.diffJson);
    }

    self.numberOfSelectedPatchLines = 0;
    var index = 0;

    // ko's binding resolution is not recursive, which means below ko.bind refresh method doesn't work for
    // data bind at getPatchCheckBox that is rendered with "html" binding.
    // which is reason why manually updating the html content and refreshing kobinding to have it render...
    if (self.patchLineList) {
      html = html.replace(/<span class="d2h-code-line-[a-z]+">(\+|\-)/g, function (match, capture) {
        if (self.patchLineList()[index] === undefined) {
          self.patchLineList()[index] = true;
        }

        return self.getPatchCheckBox(capture, index, self.patchLineList()[index++]);
      });
    }

    if (html !== self.htmlSrc) {
      // diff has changed since last we displayed and need refresh
      self.htmlSrc = html;
      self.isParsed(false);
      self.isParsed(true);
    }
  });
Example #3
0
TextDiffViewModel.prototype.render = function() {
  if (this.diffJson.length == 0) return; // check if diffs are available (binary files do not support them)

  var self = this;
  this.isParsed(false);
  var diffJsonCopy = JSON.parse(JSON.stringify(this.diffJson)); // make a json copy
  var lineCount = 0;

  diffJsonCopy[0].blocks = diffJsonCopy[0].blocks.reduce(function(blocks, block) {
    var length = block.lines.length;
    if (lineCount < self.loadCount) {
      block.lines = block.lines.slice(0, self.loadCount - lineCount);
      blocks.push(block);
    }
    lineCount += length;
    return blocks;
  }, []);

  this.loadMoreCount(Math.min(loadLimit, Math.max(0, lineCount - this.loadCount)));

  var html;

  if (this.textDiffType.value() === 'sidebysidediff') {
    html = diff2html.getPrettySideBySideHtmlFromJson(diffJsonCopy);
  } else {
    html = diff2html.getPrettyHtmlFromJson(diffJsonCopy);
  }

  var index = 0;
  this.numberOfSelectedPatchLines = 0;

  // if self.patchLineList is null then patching is not avaliable so skip this expensive op.x
  if (self.patchLineList) {
    html = html.replace(/<span class="d2h-code-line-[a-z]+">(\+|\-)/g, function (match, capture) {
      if (self.patchLineList()[index] === undefined) {
        self.patchLineList()[index] = true;
      }

      return self.getPatchCheckBox(capture, index, self.patchLineList()[index++]);
    });
  }

  // ko's binding resolution is not recursive, which means below ko.bind refresh method doesn't work for
  // data bind at getPatchCheckBox that is rendered with "html" binding.
  // which is reason why manually updating the html content and refreshing kobinding to have it render...
  this.htmlSrc = html;
  this.isParsed(true);
};
Example #4
0
 return this.server.getPromise('/diff', this.getDiffArguments()).then((diffs) => {
   if (typeof diffs !== 'string') {
     // Invalid value means there is no changes, show dummy diff withotu any changes
     diffs = `diff --git a/${this.filename} b/${this.filename}
               index aaaaaaaa..bbbbbbbb 111111
               --- a/${this.filename}
               +++ b/${this.filename}`;
   }
   this.diffJson = diff2html.getJsonFromDiff(diffs);
 }).catch(err => {
Example #5
0
  Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
    var that = this;
    var config = baseConfig;
    var defaultTemplate = path.resolve(__dirname, '..', 'dist', 'template.html');
    config.wordByWord = (baseConfig.diff === 'word');
    config.charByChar = (baseConfig.diff === 'char');
    config.template = baseConfig.htmlWrapperTemplate || defaultTemplate;

    if (!fs.existsSync(config.template)) {
      return callback(new Error('Template (`' + baseConfig.template + '`) not found!'));
    }

    var jsonContent = diff2Html.getJsonFromDiff(input, config);

    if (baseConfig.format === 'html') {
      config.inputFormat = 'json';

      if (baseConfig.style === 'side') {
        config.outputFormat = 'side-by-side';
      } else {
        config.outputFormat = 'line-by-line';
      }

      if (baseConfig.summary === 'hidden') {
        config.showFiles = false;
      } else {
        config.showFiles = true;
        config.showFilesOpen = baseConfig.summary === 'open';
      }

      config.synchronisedScroll = (baseConfig.synchronisedScroll === 'enabled');

      var htmlContent = diff2Html.getPrettyHtml(jsonContent, config);
      return callback(null, that._prepareHTML(htmlContent, config));
    } else if (baseConfig.format === 'json') {
      return callback(null, JSON.stringify(jsonContent));
    }

    return callback(new Error('Wrong output format `' + baseConfig.format + '`!'));
  };
Example #6
0
    self.server.get('/diff', this.getDiffArguments() , function(err, diffs) {
      if (err) {
        if (self.diffProgressBar) self.diffProgressBar.stop();
        if (err.errorCode == 'no-such-file') {
          // The file existed before but has been removed, but we're trying to get a diff for it
          // Most likely it will just disappear with the next refresh of the staging area
          // so we just ignore the error here
          return true;
        }
        return callback ? callback(err) : null;
      }
      if (typeof diffs == 'string') {
        self.diffJson = diff2html.getJsonFromDiff(diffs);
        self.render();
      }

      if (self.diffProgressBar) self.diffProgressBar.stop();
      if (callback) callback();
    });
Example #7
0
 return self.server.getPromise('/diff', self.getDiffArguments()).then(function(diffs) {
   if (typeof diffs == 'string') {
     self.diffJson = diff2html.getJsonFromDiff(diffs);
   }
 }).catch(function(err) {