1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<?php
require_once ABSPATH . WPINC . '/Text/Diff.php'; require_once ABSPATH . WPINC . '/Text/Diff/Renderer.php'; require_once ABSPATH . WPINC . '/Text/Diff/Renderer/inline.php'; require_once ABSPATH . WPINC . '/wp-diff.php';
/** * Diff renderer extending that which WordPress uses for post revisions. */ class Loco_output_DiffRenderer extends WP_Text_Diff_Renderer_Table {
/** * {@inheritdoc} */ public function __construct( $params = [] ){ parent::__construct( $params + [ 'show_split_view' => true, 'leading_context_lines' => 1, 'trailing_context_lines' => 1, ] ); }
/** * Render diff of two files, presumed to be PO or POT * @param Loco_fs_File Left hand file * @param Loco_fs_File Right hand file * @return string HTML table */ public function renderFiles( Loco_fs_File $lhs, Loco_fs_File $rhs ){ loco_require_lib('compiled/gettext.php'); // attempt to raise memory limit to WP_MAX_MEMORY_LIMIT if( function_exists('wp_raise_memory_limit') ){ wp_raise_memory_limit('loco'); } // like wp_text_diff but avoiding whitespace normalization // uses deprecated signature for 'auto' in case of old WordPress return $this->render( new Text_Diff( self::splitFile($lhs), self::splitFile($rhs) ) ); }
/** * @param Loco_fs_File * @return string[] */ private static function splitFile( Loco_fs_File $file ){ $src = $file->getContents(); $src = Loco_gettext_Data::ensureUtf8($src); $arr = preg_split( '/\\r?\\n/', $src ); if( ! is_array($arr) ){ $f = new Loco_mvc_FileParams( [], $file ); throw new Loco_error_Exception('Failed to split '.$f->relpath.' ('.$f->size.')' ); } return $arr; }
/** * {@inheritdoc} */ public function _startDiff() { return "<table class=\"diff\">\n"; }
/** * {@inheritdoc} */ public function _endDiff() { return "</table>\n"; }
/** * {@inheritdoc} */ public function _startBlock( $header ) { return '<tbody data-diff="'.esc_attr($header)."\">\n"; }
/** * {@inheritdoc} */ public function _endBlock() { return "</tbody>\n"; }
}
|