From 584657e005b8ad94ca81bbfaa75e88cfb67784b3 Mon Sep 17 00:00:00 2001 From: Brendan Blackwood Date: Mon, 16 Dec 2013 17:55:12 -0500 Subject: [PATCH 1/3] add line numbers --- diff.js | 33 ++++++++++++++++++++++++++++++--- pretty-diff.js | 16 +++++++++++----- template.html | 20 ++++++++++++++------ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/diff.js b/diff.js index 2300195..c3a22f4 100755 --- a/diff.js +++ b/diff.js @@ -27,15 +27,42 @@ module.exports = function( args, fn ) { function splitByFile( diff ) { var filename, - files = {}; + files = {}, + matches, + oldLineNum, + newLineNum; diff.split( "\n" ).forEach(function( line, i ) { if ( line.charAt( 0 ) === "d" ) { filename = line.replace( /^diff --git a\/(\S+).*$/, "$1" ); files[ filename ] = []; + oldLineNum = null; + } else if ( matches = line.match( /@@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.*/ ) ) { + oldLineNum = newLineNum = matches[ 2 ]; + files[ filename ].push({ + line: line, + oldLineNum: null, + newLineNum: null + }); + } else if ( matches = line.match( /^($esc\[[0-9;]+m)*([\ +-])/ ) ) { + files[ filename ].push({ + line: line, + oldLineNum: oldLineNum, + newLineNum: newLineNum + }); + if ( matches[ 2 ] != '-' ) { + newLineNum++; + } + if ( matches[ 2 ] != '+' ) { + oldLineNum++; + } + } else { + files[ filename ].push({ + line: line, + oldLineNum: null, + newLineNum: null + }); } - - files[ filename ].push( line ); }); return files; diff --git a/pretty-diff.js b/pretty-diff.js index c7a95fb..3e124bf 100755 --- a/pretty-diff.js +++ b/pretty-diff.js @@ -24,9 +24,9 @@ function generatePrettyDiff( parsedDiff ) { for ( var file in parsedDiff ) { diffHtml += "

" + file + "

" + - "
" + + "
" + markUpDiff( parsedDiff[ file ] ) + - ""; + "
"; } fs.writeFileSync( "/tmp/diff.html", template.replace( "{{diff}}", diffHtml ) ); @@ -51,10 +51,16 @@ var markUpDiff = function() { .replace( /\t/g, " " ); } + function lineNumTemplate( oldNum, newNum, type ) { + var remove = type == '+' ? '' : oldNum, + add = type == '-' ? '' : newNum; + return '' + (remove || '') + '' + (add || '') + ''; + } + return function( diff ) { - return diff.map(function( line ) { - var type = line.charAt( 0 ); - return "
" + escape( line ) + "
"; + return diff.map(function( lineData ) { + var type = lineData.line.charAt( 0 ); + return '' + lineNumTemplate( lineData.oldLineNum, lineData.newLineNum, type ) + '
' + escape( lineData.line ) + '
'; }).join( "\n" ); }; }(); diff --git a/template.html b/template.html index f432488..987b36e 100644 --- a/template.html +++ b/template.html @@ -10,7 +10,7 @@ #wrapper { display: inline-block; margin-top: 1em; - min-width: 800px; + max-width: 800px; text-align: left; } h2 { @@ -31,17 +31,25 @@ border: 1px solid #d8d8d8; margin-bottom: 1em; overflow: auto; - padding: 0.5em 0; } - .file-diff > div { - width: 100%: + .file-diff > table { + border-spacing: 0; + width: 100%; } - pre { - margin: 0; + .line { font-family: "Bitstream Vera Sans Mono", Courier, monospace; font-size: 12px; + } + .line-number { + color: #aaa; + border-right: 1px solid #e5e5e5; + padding: 0 8px; + } + pre { + margin: 0; line-height: 1.4em; text-indent: 0.5em; + display: inline-block; } .file { color: #aaa; From 2215030d72197204521922b2e96da71c1272ba51 Mon Sep 17 00:00:00 2001 From: Brendan Blackwood Date: Tue, 17 Dec 2013 08:57:41 -0500 Subject: [PATCH 2/3] fix parsed diff for gists --- gist-diff.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gist-diff.js b/gist-diff.js index 1b604b2..f8353aa 100755 --- a/gist-diff.js +++ b/gist-diff.js @@ -36,7 +36,7 @@ diff( args.join( " " ), function( error, parsedDiff ) { var files = {}; for ( var file in parsedDiff ) { files[ file.replace( /\//g, "-" ) + ".diff" ] = { - content: parsedDiff[ file ].join( "\n" ) + content: parsedDiff[ file ].map(function(data) { return data.line; }).join( "\n" ) }; } From 84653d56d391f59c5d3e94651e18c7fc7aea005c Mon Sep 17 00:00:00 2001 From: Brendan Blackwood Date: Tue, 17 Dec 2013 09:48:33 -0500 Subject: [PATCH 3/3] removing unnecessary pattern --- diff.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff.js b/diff.js index c3a22f4..7ddddb1 100755 --- a/diff.js +++ b/diff.js @@ -44,7 +44,7 @@ function splitByFile( diff ) { oldLineNum: null, newLineNum: null }); - } else if ( matches = line.match( /^($esc\[[0-9;]+m)*([\ +-])/ ) ) { + } else if ( matches = line.match( /^(\[[0-9;]+m)*([\ +-])/ ) ) { files[ filename ].push({ line: line, oldLineNum: oldLineNum,