Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.