The following code converts a piece of source code to HTML, highlighting the lines that have changed compared to a previous (unshown) version of the code. Call the code with two arguments like perl bolddiff.pl oldfile newfile to show newfiles with the files that have been added or changed since old bolded.

#!perl use warnings; use strict; use CGI; 1 == @ARGV || 2 == @ARGV or die "usage: perl bolddiff.pl file1 file2"; my $two = 2 <= @ARGV; my ($f0n, $f1n) = @ARGV; my $key = "BOLDDIFF_" . join("", map { int(rand(10)) } 0..5); my $H; if ($two) { open $H, "-|", "diff", "-bD", $key, "--", $f1n, $f0n or die; } else { open $H, "<", $f0n or die; } print qq{<pre class="clisting">\n}; my $tap = 1; my $diff = 0; while (<$H>) { if (/\b\Q$key\E\b/) { if (/\A#ifdef \Q$key\E\b/) { $tap = 0; $diff = 1; } elsif (/\A#ifndef \Q$key\E\b/) { $tap = 1; $diff = 1; } elsif (/\A#else \/\* \Q$key\E\b/) { $tap = !$tap; } elsif (/\A#endif \/\* (! )?\Q$key\E\b/) { $tap = 1; $diff = 0; } else { die "ifdef key unexpected: $_"; } } else { if ($tap) { s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; $diff and s/(.*)/<strong>$1<\/strong>/; print; } } } print qq{</pre>\n}; __END__

In reply to Htmlify code with differences by ambrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.