{diff}

Shows what changed between two texts - word by word in the flow of the text, or line by line in one column or side by side.

Text and formatting Single tags

Syntax

{diff $old, $new}
{diff $old, $new, lines, from='Monday', to='Friday'}
{diff $old, $new, side, from='before', to='after'}

Parameters

old
The old text.
new
The new text.

Options

lines
Bare option: compare line by line - a table with the old and the new line number, a sign and the line.
side
Bare option: the lines side by side, a changed line beside its counterpart, line numbers on both sides.
from
The name of the old version - a column head side by side, a caption in one column.
to
The name of the new version.

Description

{diff $old, $new} compares two texts and writes the new one with what changed marked: the words that went out as <del>, the words that came in as <ins>. The differences are found by Myers' algorithm, the shortest edit script, as git diff finds them. In a run of changes the deletions come before the insertions, and the space between two changed words joins the change, so a rewritten phrase reads as one change instead of a patchwork.

lines compares line by line, in one column: the old and the new line number, a sign and the line. side puts the two versions next to each other, a changed line beside its counterpart. In both a changed line that has a counterpart marks the words that changed within it; a line without one is struck or added whole. from= and to= name the two versions.

Everything of both texts is escaped. A screen reader hears where each deletion and insertion starts and ends, from words in pseudo-elements that are hidden from the eye. The colours are custom properties on .pad-diff - --pad-diff-del, --pad-diff-ins and their -mark forms, -text, -muted, -line - with light-dark() defaults written once a page. A {diff} without the second text is a strict error.

Examples

Two versions of a sentence

Word by word in the flow of the text - struck what went, marked what came.

Templateexamples/diff/1-words.pad
{diff $draft, $final}
PHPexamples/diff/1-words.php
<?php

  $draft = 'PAD templates drive the application: a page is a template, and the template asks for its data.';
  $final = 'PAD templates drive the application: every page is a template, and the template asks for the data it needs.';

?>
Resultopen alone
PAD templates drive the application: aevery page is a template, and the template asks for itsthe data it needs.

Line by line

A shopping list in one column - the old and the new line number, a sign, the line.

Templateexamples/diff/2-lines.pad
{diff $monday, $friday, lines, from='Monday', to='Friday'}
PHPexamples/diff/2-lines.php
<?php

  $monday = "Milk\nEggs\nBread\nButter\nCoffee";
  $friday = "Milk\nEggs\nSourdough bread\nCoffee\nPears";

?>
Resultopen alone
Monday โ†’ Friday
1 1 Milk
2 2 Eggs
3 - Bread
3 + Sourdough bread
4 - Butter
5 4 Coffee
5 + Pears

Side by side

Two versions of a function next to each other, the changed words marked within each changed line.

Templateexamples/diff/3-side.pad
{diff $before, $after, side, from='before', to='after'}
PHPexamples/diff/3-side.php
<?php

  $before = "function total ( \$rows ) {\n  \$sum = 0;\n  foreach ( \$rows as \$row )\n    \$sum += \$row ['price'];\n  return \$sum;\n}";
  $after  = "function total ( \$rows, \$tax = 0.21 ) {\n  \$sum = 0;\n  foreach ( \$rows as \$row )\n    \$sum += \$row ['price'] * \$row ['quantity'];\n  return round ( \$sum * ( 1 + \$tax ), 2 );\n}";

?>
Resultopen alone
before after
1 function total ( $rows ) { 1 function total ( $rows, $tax = 0.21 ) {
2 $sum = 0; 2 $sum = 0;
3 foreach ( $rows as $row ) 3 foreach ( $rows as $row )
4 $sum += $row ['price']; 4 $sum += $row ['price'] * $row ['quantity'];
5 return $sum; 5 return round ( $sum * ( 1 + $tax ), 2 );
6 } 6 }

See also

  • {excerpt} Cuts a long text to whole words with an ellipsis, centred on the first match of search words, every match marked.
  • {highlight} Colours source code on the server - PAD, PHP, HTML, CSS, JavaScript, JSON, YAML, SQL or shell - with optional line numbers and marked lines.
  • {markdown} Renders its content, or the value of its parameter, as Markdown and writes the HTML.