1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8/**
9 * "Inline" character diff renderer.
10 *
11 * This class renders the diff in "inline" format by characters.
12 *
13 * @package Text_Diff
14 */
15class Text_Diff_Renderer_character_inline extends Tiki_Text_Diff_Renderer
16{
17	protected $orig;
18	protected $final;
19
20	public function __construct($context_lines = 0)
21	{
22		$this->_leading_context_lines = $context_lines;
23		$this->_trailing_context_lines = $context_lines;
24		$this->diff = "";
25		$this->change = "";
26	}
27
28	protected function _startDiff()
29	{
30	}
31
32	protected function _endDiff()
33	{
34		return [$this->diff, $this->change];
35	}
36
37	protected function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
38	{
39	}
40
41	protected function _startBlock($header)
42	{
43		echo $header;
44	}
45
46	protected function _endBlock()
47	{
48	}
49
50	protected function _getChange($lines)
51	{
52		return str_replace("<br />", "↵<br />", join("", $lines));
53	}
54
55	protected function _lines($type, $lines, $prefix = '')
56	{
57		if ($type == 'context') {
58			$this->diff .= join("", $lines);
59		} elseif ($type == 'added' || $type == 'change-added') {
60			$t = $this->_getChange($lines);
61			if (! empty($t)) {
62				$this->diff .= "<span class='diffadded'>$t</span>";
63			}
64		} elseif ($type == 'deleted' || $type == 'change-deleted') {
65			$t = $this->_getChange($lines);
66			if (! empty($t)) {
67				$this->diff .= "<span class='diffinldel'>$t</span>";
68			}
69		} elseif ($type == 'changed') {
70			$t = $this->_getChange($lines[0]);
71			if (! empty($t)) {
72				$this->diff .= "<span class='diffinldel'>$t</span>";
73			}
74			$t = $this->_getChange($lines[1]);
75			if (! empty($t)) {
76				$this->diff .= "<span class='diffadded'>$t</span>";
77			}
78		}
79	}
80
81	protected function _context($lines)
82	{
83		$this->_lines('context', $lines);
84	}
85
86	protected function _added($lines, $changemode = false)
87	{
88		if (! $this->change) {
89			$this->change = "added";
90		}
91		if ($this->change != "added") {
92			$this->change = "changed";
93		}
94
95		if ($changemode) {
96			$this->_lines('change-added', $lines, '+');
97		} else {
98			$this->_lines('added', $lines, '+');
99		}
100	}
101
102	protected function _deleted($lines, $changemode = false)
103	{
104		if (! $this->change) {
105			$this->change = "deleted";
106		}
107		if ($this->change != "deleted") {
108			$this->change = "changed";
109		}
110
111		if ($changemode) {
112			$this->_lines('change-deleted', $lines, '-');
113		} else {
114			$this->_lines('deleted', $lines, '-');
115		}
116	}
117
118	protected function _changed($orig, $final)
119	{
120		$this->change = 'changed';
121		$this->_lines('changed', [$orig, $final], '*');
122	}
123}
124