Skip to content

Instantly share code, notes, and snippets.

@hyeonseok
Last active December 22, 2015 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hyeonseok/6410108 to your computer and use it in GitHub Desktop.
Save hyeonseok/6410108 to your computer and use it in GitHub Desktop.
Word diff to HTML
<?php
/*
http://hyeonseok.com/soojung/dev/2013/09/02/743.html
git diff -U1 --word-diff > ../diff.txt
Execute this command from repository then run this script.
*/
$diff_file = file('diff.txt');
$count = 1;
$diff_data = array();
foreach ($diff_file as $row) {
if (strpos($row, 'diff --git') !== false) {
$count = 1;
}
if (strpos($row, '@@') === false) {
array_push($diff_data, $row);
} else {
array_push($diff_data, "\n" . '검토의견 ' . $count++ . ')' . "\n");
}
}
$html = implode('', $diff_data);
$html = htmlspecialchars($html);
$html = str_replace('[-', '<del style="color: #c00">', $html);
$html = str_replace('-]', '</del>', $html);
$html = str_replace('{+', '<ins style="color: #0c0">', $html);
$html = str_replace('+}', '</ins>', $html);
$html = nl2br($html);
$html = '<!DOCTYPE html><html lang="ko"><head><meta charset="UTF-8"><title>DIFF</title></head><body>' . $html . '</body></html>';
fwrite(fopen('diff.html', 'w'), $html);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment