diff2html 1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
diff2html/diff2html.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Compare two files and produce a well formatted diff in an HTML file.
|
|
3
|
+
|
|
4
|
+
A command line wrapper for Python's difftool.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
from difflib import HtmlDiff
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
import re
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DiffDoc:
|
|
15
|
+
"""A container to keep HTML bric-a-brac tidy. Provides a single
|
|
16
|
+
class methods convert a diff to an HTML page.
|
|
17
|
+
"""
|
|
18
|
+
# Create an additional explanation of the webpage:
|
|
19
|
+
BACKBUTTON = '<br><button onclick="history.back()">Go Back</button><br>'
|
|
20
|
+
HEAD1 = '<h1>{}</h1>'
|
|
21
|
+
HEAD3 = '<h3>{}</h3>'
|
|
22
|
+
HEAD = """
|
|
23
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
24
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
25
|
+
|
|
26
|
+
<html>
|
|
27
|
+
|
|
28
|
+
<head>
|
|
29
|
+
<meta http-equiv="Content-Type"
|
|
30
|
+
content="text/html; charset=utf-8" />
|
|
31
|
+
<title></title>
|
|
32
|
+
<style type="text/css">
|
|
33
|
+
h1, h2, h3 {
|
|
34
|
+
font-family: Consolas, Sans, sans-serif
|
|
35
|
+
}
|
|
36
|
+
#code {
|
|
37
|
+
font-family: 'Jetbrains Mono', consolas, 'Courier New', monospace;
|
|
38
|
+
}
|
|
39
|
+
table {
|
|
40
|
+
font-family: 'Jetbrains Mono', consolas, 'Courier New', monospace;
|
|
41
|
+
border-style: solid;
|
|
42
|
+
border-width: 2px;
|
|
43
|
+
margin-top: 5px;
|
|
44
|
+
}
|
|
45
|
+
.legend,
|
|
46
|
+
.legend th,
|
|
47
|
+
.legend td {
|
|
48
|
+
border-style: none;
|
|
49
|
+
}
|
|
50
|
+
td.diff_header {
|
|
51
|
+
text-align:right;
|
|
52
|
+
}
|
|
53
|
+
.diff_next {
|
|
54
|
+
border-left-style: solid;
|
|
55
|
+
border-left-color: black;
|
|
56
|
+
border-left-width: 3px;
|
|
57
|
+
}
|
|
58
|
+
.diff_add {background-color:#aaffaa}
|
|
59
|
+
.diff_chg {background-color:#ffff77}
|
|
60
|
+
.diff_sub {background-color:#ffaaaa}
|
|
61
|
+
</style>
|
|
62
|
+
</head>
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
KEYTABLE = """
|
|
66
|
+
<table class="diff" summary="Legends" id="legend">
|
|
67
|
+
<tr> <th colspan="2"> Legends </th> </tr>
|
|
68
|
+
<tr> <td> <table border="" summary="Colors">
|
|
69
|
+
<tr><th> Colors </th> </tr>
|
|
70
|
+
<tr><td class="diff_add"> Added </td></tr>
|
|
71
|
+
<tr><td class="diff_chg">Changed</td> </tr>
|
|
72
|
+
<tr><td class="diff_sub">Deleted</td> </tr>
|
|
73
|
+
</table></td>
|
|
74
|
+
<td> <table border="" summary="Links">
|
|
75
|
+
<tr><th colspan="2"> Links </th> </tr>
|
|
76
|
+
<tr><td>(f)irst change</td> </tr>
|
|
77
|
+
<tr><td>(n)ext change</td> </tr>
|
|
78
|
+
<tr><td>(t)op</td> </tr>
|
|
79
|
+
</table></td> </tr>
|
|
80
|
+
</table>
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
TAIL = """
|
|
84
|
+
</body>
|
|
85
|
+
</html>
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
@classmethod
|
|
89
|
+
def diff_to_html(
|
|
90
|
+
cls, left, right, title, left_title, right_title, subtitle
|
|
91
|
+
):
|
|
92
|
+
# Carry out comparison:
|
|
93
|
+
htmldiff = HtmlDiff()
|
|
94
|
+
html_data = htmldiff.make_table(
|
|
95
|
+
left,
|
|
96
|
+
right,
|
|
97
|
+
fromdesc=left_title,
|
|
98
|
+
todesc=right_title
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
output = (
|
|
102
|
+
cls.HEAD
|
|
103
|
+
+ cls.BACKBUTTON
|
|
104
|
+
+ cls.HEAD1.format(title)
|
|
105
|
+
+ f'{cls.HEAD3.format(subtitle) if subtitle else ""}'
|
|
106
|
+
+ html_data
|
|
107
|
+
+ cls.BACKBUTTON
|
|
108
|
+
+ cls.KEYTABLE
|
|
109
|
+
+ cls.TAIL
|
|
110
|
+
)
|
|
111
|
+
return output
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def parse_args():
|
|
115
|
+
parser = argparse.ArgumentParser(__doc__)
|
|
116
|
+
parser.add_argument('left', help='The first file to compare.')
|
|
117
|
+
parser.add_argument('right', help='The second file to compare.')
|
|
118
|
+
parser.add_argument(
|
|
119
|
+
'-l', '--left-strip',
|
|
120
|
+
help='Regex expression acting on the left hand side of the diff',
|
|
121
|
+
dest='strip_left'
|
|
122
|
+
)
|
|
123
|
+
parser.add_argument(
|
|
124
|
+
'-r', '--right-strip',
|
|
125
|
+
help='Regex expression acting on the right hand side of the diff',
|
|
126
|
+
dest='strip_right'
|
|
127
|
+
)
|
|
128
|
+
parser.add_argument(
|
|
129
|
+
'-t', '--title',
|
|
130
|
+
help=(
|
|
131
|
+
'Add a title to the page, other than the names of the files'
|
|
132
|
+
' being compared. If set the names of the files being compared'
|
|
133
|
+
' will be added as a subtitle.'
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
parser.add_argument(
|
|
137
|
+
'-n', '--notes',
|
|
138
|
+
help='Additional notes to be added below the title and subtitle.'
|
|
139
|
+
)
|
|
140
|
+
parser.add_argument(
|
|
141
|
+
'-o', '--output',
|
|
142
|
+
help='Output file path. If unset output will be printed to stdout.',
|
|
143
|
+
dest='output'
|
|
144
|
+
)
|
|
145
|
+
return parser.parse_args()
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def read_file(filepath: Path, sub: Optional[str] = None) -> list[str]:
|
|
149
|
+
"""Read file and carry out substutions on lines.
|
|
150
|
+
|
|
151
|
+
Returns a list of strings.
|
|
152
|
+
"""
|
|
153
|
+
lines = Path(filepath).read_text().split('\n')
|
|
154
|
+
if sub:
|
|
155
|
+
lines = [re.sub(sub, '', line) for line in lines]
|
|
156
|
+
return lines
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def main():
|
|
160
|
+
# Parse command line args and environment variables:
|
|
161
|
+
opts = parse_args()
|
|
162
|
+
|
|
163
|
+
parsed_html = DiffDoc.diff_to_html(
|
|
164
|
+
read_file(opts.left, opts.strip_left),
|
|
165
|
+
read_file(opts.right, opts.strip_right),
|
|
166
|
+
opts.title or f'{opts.left} vs {opts.right}',
|
|
167
|
+
opts.left,
|
|
168
|
+
opts.right,
|
|
169
|
+
f'{opts.left} vs {opts.right}' if opts.title else None
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
if opts.output:
|
|
173
|
+
# Write html file
|
|
174
|
+
Path(opts.output).write_text(parsed_html)
|
|
175
|
+
else:
|
|
176
|
+
print(parsed_html)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
if __name__ == '__main__':
|
|
180
|
+
main()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: diff2html
|
|
3
|
+
Version: 1.0
|
|
4
|
+
Summary: CLI wrapper for Pythons Difflib builtin
|
|
5
|
+
Author-email: "tim.pillinger" <tim.pillinger@metoffice.gov.uk>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: diff,html,difftool,difflib,cli
|
|
8
|
+
Classifier: Programming Language :: Python
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.md
|
|
11
|
+
Provides-Extra: test
|
|
12
|
+
Requires-Dist: pytest; extra == "test"
|
|
13
|
+
Requires-Dist: flake8; extra == "test"
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# Diff2HTML
|
|
17
|
+
|
|
18
|
+
Python's difflib is a great standard library feature.
|
|
19
|
+
|
|
20
|
+
But I wanted a command line version.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install diff2html
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
diff2html fileA fileB --output diff.html
|
|
32
|
+
firefox diff.html
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
for advanced usage see:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
diff2html --help
|
|
39
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
diff2html/diff2html.py,sha256=DqanJ_H-4l4ZdpP1zylbSJ069rVHQvRfbLPJDSnxQ8E,5405
|
|
2
|
+
diff2html-1.0.dist-info/licenses/LICENSE.md,sha256=OhjrFdRvz4myvwIv6PKyx-Anw3Aen6QfmzC_IYQ4GKE,1069
|
|
3
|
+
diff2html-1.0.dist-info/METADATA,sha256=Fho-9XDlFBHJcVEG6c4ZHErmul_DRBTQ__08wO434GM,740
|
|
4
|
+
diff2html-1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
diff2html-1.0.dist-info/entry_points.txt,sha256=Po2jceSuheUPc4lpVFkycXYat9tArEnoYATYHzZOO5I,55
|
|
6
|
+
diff2html-1.0.dist-info/top_level.txt,sha256=kLDgU8gBdhBQyz9tFuEjwgYjY-2HMxmWio5NWCJ1Luc,10
|
|
7
|
+
diff2html-1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tim Pillinger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
diff2html
|