Skip to content

Commit c27035a

Browse files
committed
Add choosing marking levels to html example
Sample text A is altered to see difference between word and line level more clearly.
1 parent 75358da commit c27035a

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

example/a.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<h2>This line is removed from version2.</h2>
99
<h2>This line is also removed from version2.</h2>
1010
<h2>This line is the same for both versions.</h2>
11-
<h2>This line has inline differences between both versions.</h2>
11+
<h2>this line has inline differences between both versions.</h2>
1212
<h2>This line is the same for both versions.</h2>
1313
<h2>This line also has inline differences between both versions.</h2>
1414
<h2>This line is the same for both versions.</h2>

example/example.php

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$sampleB = file_get_contents(dirname(__FILE__) . '/b.txt');
1616

1717
// Options for generating the diff.
18-
$customOptions = [
18+
$diffOptions = [
1919
'context' => 2,
2020
'trimEqual' => false,
2121
'ignoreWhitespace' => true,
@@ -24,8 +24,14 @@
2424

2525
// Choose one of the initializations.
2626
$diff = new Diff($sampleA, $sampleB); // Initialize the diff class with default options.
27-
//$diff = new Diff($sampleA, $sampleB, $customOptions); // Initialize the diff class with custom options.
28-
?><!DOCTYPE html>
27+
//$diff = new Diff($sampleA, $sampleB, $diffOptions); // Initialize the diff class with custom options.
28+
29+
// Options for rendering the diff.
30+
$rendererOptions = [
31+
'inlineMarking' => $_GET['inlineMarking'] ?? Diff\Renderer\MainRenderer::CHANGE_LEVEL_LINE,
32+
]
33+
?>
34+
<!DOCTYPE html>
2935
<html lang="en">
3036
<head>
3137
<meta charset="utf-8"/>
@@ -46,50 +52,58 @@ function changeCSS(cssFile, cssLinkIndex) {
4652
</script>
4753
</head>
4854
<body>
49-
<h1>PHP LibDiff - Examples</h1>
50-
<aside>
51-
<h2>Change Theme</h2>
52-
<a href="#" onclick="changeCSS('styles.css', 0);">Light Theme</a>
53-
<a href="#" onclick="changeCSS('dark-theme.css', 0);">Dark Theme</a>
54-
</aside>
55-
<hr>
55+
<h1>PHP LibDiff - Examples</h1>
56+
<aside>
57+
<h2>Change Theme</h2>
58+
<a href="#" onclick="changeCSS('styles.css', 0);">Light Theme</a>
59+
<a href="#" onclick="changeCSS('dark-theme.css', 0);">Dark Theme</a>
60+
</aside>
61+
<hr>
62+
<aside>
63+
<h2>Inline Marking</h2>
64+
<a href="example.php?inlineMarking=2">Line</a>
65+
<a href="example.php?inlineMarking=1">Word</a>
66+
<a href="example.php?inlineMarking=0">Character</a>
67+
<a href="example.php?inlineMarking=4">None</a>
68+
</aside>
69+
<hr>
5670

57-
<h2>HTML Side by Side Diff</h2>
71+
<h2>HTML Side by Side Diff</h2>
5872

59-
<?php
60-
// Generate a side by side diff.
61-
$renderer = new SideBySide();
62-
echo $diff->isIdentical() ? 'No differences found.' : $diff->Render($renderer);
63-
?>
73+
<?php
74+
// Generate a side by side diff.
75+
$renderer = new SideBySide($rendererOptions);
76+
echo $diff->isIdentical() ? 'No differences found.' : $diff->Render($renderer);
77+
?>
6478

65-
<h2>HTML Inline Diff</h2>
66-
<?php
67-
// Generate an inline diff.
68-
$renderer = new Inline();
69-
echo $diff->isIdentical() ? 'No differences found.' : $diff->Render($renderer);
70-
?>
79+
<h2>HTML Inline Diff</h2>
80+
<?php
81+
// Generate an inline diff.
82+
$renderer = new Inline($rendererOptions);
83+
echo $diff->isIdentical() ? 'No differences found.' : $diff->Render($renderer);
84+
?>
7185

72-
<h2>HTML Unified Diff</h2>
73-
<?php
74-
// Generate a unified diff.
75-
$renderer = new HtmlUnified();
76-
echo $diff->isIdentical() ? 'No differences found.' : '<pre>' . $diff->Render($renderer) . '</pre>';
77-
?>
86+
<h2>HTML Unified Diff</h2>
87+
<?php
88+
// Generate a unified diff.
89+
$renderer = new HtmlUnified($rendererOptions);
90+
echo $diff->isIdentical() ? 'No differences found.' : '<pre>' . $diff->Render($renderer) . '</pre>';
91+
?>
7892

79-
<h2>Text Unified Diff</h2>
80-
<?php
81-
// Generate a unified diff.
82-
$renderer = new Unified();
83-
echo $diff->isIdentical() ?
84-
'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
85-
?>
93+
<h2>Text Unified Diff</h2>
94+
<?php
95+
// Generate a unified diff.
96+
$renderer = new Unified();
97+
echo $diff->isIdentical() ?
98+
'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
99+
?>
86100

87-
<h2>Text Context Diff</h2>
88-
<?php
89-
// Generate a context diff.
90-
$renderer = new Context();
91-
echo $diff->isIdentical() ?
92-
'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
93-
?>
101+
<h2>Text Context Diff</h2>
102+
<?php
103+
// Generate a context diff.
104+
$renderer = new Context();
105+
echo $diff->isIdentical() ?
106+
'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
107+
?>
94108
</body>
95109
</html>

0 commit comments

Comments
 (0)