Merge copy and equivalent edits into intelligible chunks.
function process_edits($edits) {
$processed = array();
$current = array_shift($edits);
// Make two passes -- first merge space delimiter copies back into their originals.
while ($chunk = array_shift($edits)) {
if ($chunk->type == 'copy' && $chunk->orig === array(
' ',
)) {
$current->orig = array_merge((array) $current->orig, (array) $chunk->orig);
$current->closing = array_merge((array) $current->closing, (array) $chunk->closing);
}
else {
$processed[] = $current;
$current = $chunk;
}
}
$processed[] = $current;
// Initial setup
$edits = $processed;
$processed = array();
$current = array_shift($edits);
// Second, merge equivalent chunks into each other.
while ($chunk = array_shift($edits)) {
if ($current->type == $chunk->type) {
$current->orig = array_merge((array) $current->orig, (array) $chunk->orig);
$current->closing = array_merge((array) $current->closing, (array) $chunk->closing);
}
else {
$processed[] = $current;
$current = $chunk;
}
}
$processed[] = $current;
return $processed;
}