| | @@ -149,21 +149,34 @@ |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | /* |
| 152 | 152 | ** Append a single line of "diff" output to pOut. |
| 153 | 153 | */ |
| 154 | | -static void appendDiffLine(Blob *pOut, char *zPrefix, DLine *pLine){ |
| 155 | | - blob_append(pOut, zPrefix, 1); |
| 156 | | - blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK); |
| 154 | +static void appendDiffLine(Blob *pOut, char cPrefix, DLine *pLine, int html){ |
| 155 | + blob_append(pOut, &cPrefix, 1); |
| 156 | + if( html ){ |
| 157 | + if( cPrefix=='+' ){ |
| 158 | + blob_append(pOut, "<span class=\"diffadd\">", -1); |
| 159 | + }else if( cPrefix=='-' ){ |
| 160 | + blob_append(pOut, "<span class=\"diffrm\">", -1); |
| 161 | + } |
| 162 | + blob_appendf(pOut, "%.*h", (pLine->h & LENGTH_MASK), pLine->z); |
| 163 | + if( cPrefix!=' ' ){ |
| 164 | + blob_append(pOut, "</span>", -1); |
| 165 | + } |
| 166 | + }else{ |
| 167 | + blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK); |
| 168 | + } |
| 157 | 169 | blob_append(pOut, "\n", 1); |
| 158 | 170 | } |
| 159 | 171 | |
| 160 | 172 | /* |
| 161 | 173 | ** Append line numbers to the context diff output. Zero or negative numbers |
| 162 | 174 | ** are blanks. |
| 163 | 175 | */ |
| 164 | | -static void appendDiffLineno(Blob *pOut, int lnA, int lnB){ |
| 176 | +static void appendDiffLineno(Blob *pOut, int lnA, int lnB, int html){ |
| 177 | + if( html ) blob_append(pOut, "<span class=\"diffln\">", -1); |
| 165 | 178 | if( lnA>0 ){ |
| 166 | 179 | blob_appendf(pOut, "%6d ", lnA); |
| 167 | 180 | }else{ |
| 168 | 181 | blob_append(pOut, " ", 7); |
| 169 | 182 | } |
| | @@ -170,10 +183,11 @@ |
| 170 | 183 | if( lnB>0 ){ |
| 171 | 184 | blob_appendf(pOut, "%6d ", lnB); |
| 172 | 185 | }else{ |
| 173 | 186 | blob_append(pOut, " ", 8); |
| 174 | 187 | } |
| 188 | + if( html ) blob_append(pOut, "</span>", -1); |
| 175 | 189 | } |
| 176 | 190 | |
| 177 | 191 | /* |
| 178 | 192 | ** Expand the size of aEdit[] array to hold nEdit elements. |
| 179 | 193 | */ |
| | @@ -218,11 +232,17 @@ |
| 218 | 232 | |
| 219 | 233 | /* |
| 220 | 234 | ** Given a diff context in which the aEdit[] array has been filled |
| 221 | 235 | ** in, compute a context diff into pOut. |
| 222 | 236 | */ |
| 223 | | -static void contextDiff(DContext *p, Blob *pOut, int nContext, int showLn){ |
| 237 | +static void contextDiff( |
| 238 | + DContext *p, /* The difference */ |
| 239 | + Blob *pOut, /* Output a context diff to here */ |
| 240 | + int nContext, /* Number of lines of context */ |
| 241 | + int showLn, /* Show line numbers */ |
| 242 | + int html /* Render as HTML */ |
| 243 | +){ |
| 224 | 244 | DLine *A; /* Left side of the diff */ |
| 225 | 245 | DLine *B; /* Right side of the diff */ |
| 226 | 246 | int a = 0; /* Index of next line in A[] */ |
| 227 | 247 | int b = 0; /* Index of next line in B[] */ |
| 228 | 248 | int *R; /* Array of COPY/DELETE/INSERT triples */ |
| | @@ -282,35 +302,35 @@ |
| 282 | 302 | /* Show the initial common area */ |
| 283 | 303 | a += skip; |
| 284 | 304 | b += skip; |
| 285 | 305 | m = R[r] - skip; |
| 286 | 306 | for(j=0; j<m; j++){ |
| 287 | | - if( showLn ) appendDiffLineno(pOut, a+j, b+j); |
| 288 | | - appendDiffLine(pOut, " ", &A[a+j]); |
| 307 | + if( showLn ) appendDiffLineno(pOut, a+j, b+j, html); |
| 308 | + appendDiffLine(pOut, ' ', &A[a+j], html); |
| 289 | 309 | } |
| 290 | 310 | a += m; |
| 291 | 311 | b += m; |
| 292 | 312 | |
| 293 | 313 | /* Show the differences */ |
| 294 | 314 | for(i=0; i<nr; i++){ |
| 295 | 315 | m = R[r+i*3+1]; |
| 296 | 316 | for(j=0; j<m; j++){ |
| 297 | | - if( showLn ) appendDiffLineno(pOut, a+j, 0); |
| 298 | | - appendDiffLine(pOut, "-", &A[a+j]); |
| 317 | + if( showLn ) appendDiffLineno(pOut, a+j, 0, html); |
| 318 | + appendDiffLine(pOut, '-', &A[a+j], html); |
| 299 | 319 | } |
| 300 | 320 | a += m; |
| 301 | 321 | m = R[r+i*3+2]; |
| 302 | 322 | for(j=0; j<m; j++){ |
| 303 | | - if( showLn ) appendDiffLineno(pOut, 0, b+j); |
| 304 | | - appendDiffLine(pOut, "+", &B[b+j]); |
| 323 | + if( showLn ) appendDiffLineno(pOut, 0, b+j, html); |
| 324 | + appendDiffLine(pOut, '+', &B[b+j], html); |
| 305 | 325 | } |
| 306 | 326 | b += m; |
| 307 | 327 | if( i<nr-1 ){ |
| 308 | 328 | m = R[r+i*3+3]; |
| 309 | 329 | for(j=0; j<m; j++){ |
| 310 | | - if( showLn ) appendDiffLineno(pOut, a+j, b+j); |
| 311 | | - appendDiffLine(pOut, " ", &B[b+j]); |
| 330 | + if( showLn ) appendDiffLineno(pOut, a+j, b+j, html); |
| 331 | + appendDiffLine(pOut, ' ', &B[b+j], html); |
| 312 | 332 | } |
| 313 | 333 | b += m; |
| 314 | 334 | a += m; |
| 315 | 335 | } |
| 316 | 336 | } |
| | @@ -318,12 +338,12 @@ |
| 318 | 338 | /* Show the final common area */ |
| 319 | 339 | assert( nr==i ); |
| 320 | 340 | m = R[r+nr*3]; |
| 321 | 341 | if( m>nContext ) m = nContext; |
| 322 | 342 | for(j=0; j<m; j++){ |
| 323 | | - if( showLn ) appendDiffLineno(pOut, a+j, b+j); |
| 324 | | - appendDiffLine(pOut, " ", &B[b+j]); |
| 343 | + if( showLn ) appendDiffLineno(pOut, a+j, b+j, html); |
| 344 | + appendDiffLine(pOut, ' ', &B[b+j], html); |
| 325 | 345 | } |
| 326 | 346 | } |
| 327 | 347 | } |
| 328 | 348 | |
| 329 | 349 | /* |
| | @@ -335,19 +355,10 @@ |
| 335 | 355 | int n; /* Index of next unused slot in the zLine[] */ |
| 336 | 356 | int width; /* Maximum width of a column in the output */ |
| 337 | 357 | unsigned char escHtml; /* True to escape html characters */ |
| 338 | 358 | }; |
| 339 | 359 | |
| 340 | | -/* |
| 341 | | -** Write a 6-digit line number followed by a single space onto the line. |
| 342 | | -*/ |
| 343 | | -static void sbsWriteLineno(SbsLine *p, int ln){ |
| 344 | | - sqlite3_snprintf(7, &p->zLine[p->n], "%6d", ln+1); |
| 345 | | - p->zLine[p->n+6] = ' '; |
| 346 | | - p->n += 7; |
| 347 | | -} |
| 348 | | - |
| 349 | 360 | /* |
| 350 | 361 | ** Flags for sbsWriteText() |
| 351 | 362 | */ |
| 352 | 363 | #define SBS_NEWLINE 0x0001 /* End with \n\000 */ |
| 353 | 364 | #define SBS_PAD 0x0002 /* Pad output to width spaces */ |
| | @@ -417,10 +428,21 @@ |
| 417 | 428 | ** Append a string to the output only if we are rendering HTML. |
| 418 | 429 | */ |
| 419 | 430 | static void sbsWriteHtml(SbsLine *p, const char *zIn){ |
| 420 | 431 | if( p->escHtml ) sbsWrite(p, zIn, strlen(zIn)); |
| 421 | 432 | } |
| 433 | + |
| 434 | +/* |
| 435 | +** Write a 6-digit line number followed by a single space onto the line. |
| 436 | +*/ |
| 437 | +static void sbsWriteLineno(SbsLine *p, int ln){ |
| 438 | + sbsWriteHtml(p, "<span class=\"diffln\">"); |
| 439 | + sqlite3_snprintf(7, &p->zLine[p->n], "%5d ", ln+1); |
| 440 | + p->n += 6; |
| 441 | + sbsWriteHtml(p, "</span>"); |
| 442 | + p->zLine[p->n++] = ' '; |
| 443 | +} |
| 422 | 444 | |
| 423 | 445 | |
| 424 | 446 | /* |
| 425 | 447 | ** Given a diff context in which the aEdit[] array has been filled |
| 426 | 448 | ** in, compute a side-by-side diff into pOut. |
| | @@ -887,17 +909,17 @@ |
| 887 | 909 | /* Compute the difference */ |
| 888 | 910 | diff_all(&c); |
| 889 | 911 | |
| 890 | 912 | if( pOut ){ |
| 891 | 913 | /* Compute a context or side-by-side diff into pOut */ |
| 914 | + int escHtml = (diffFlags & DIFF_HTML)!=0; |
| 892 | 915 | if( diffFlags & DIFF_SIDEBYSIDE ){ |
| 893 | 916 | int width = diff_width(diffFlags); |
| 894 | | - int escHtml = (diffFlags & DIFF_HTML)!=0; |
| 895 | 917 | sbsDiff(&c, pOut, nContext, width, escHtml); |
| 896 | 918 | }else{ |
| 897 | 919 | int showLn = (diffFlags & DIFF_LINENO)!=0; |
| 898 | | - contextDiff(&c, pOut, nContext, showLn); |
| 920 | + contextDiff(&c, pOut, nContext, showLn, escHtml); |
| 899 | 921 | } |
| 900 | 922 | free(c.aFrom); |
| 901 | 923 | free(c.aTo); |
| 902 | 924 | free(c.aEdit); |
| 903 | 925 | return 0; |
| 904 | 926 | |