Skip to content

Commit 02290f7

Browse files
authored
normalize text by sorting lines (#279)
1 parent 8921846 commit 02290f7

4 files changed

Lines changed: 101 additions & 2 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
boilersuit
2+
indictments
3+
dexes
4+
swacked
5+
remorid
6+
radioallergosorbent
7+
prelaw
8+
deploring
9+
morgens
10+
thriftlessness
11+
polygons
12+
overpromise
13+
militarizations
14+
straddler
15+
stealings
16+
gimmickry
17+
unabbreviated
18+
hexagram
19+
rumbly
20+
lightheartednesses
21+
largeness
22+
monumentalizing
23+
strongbox
24+
waif
25+
derivers
26+
stubborn
27+
disenthral
28+
sorites
29+
cactoid
30+
leukocytic
31+
flabelliform
32+
dockage
33+
necrologist
34+
ridicules
35+
renouncements
36+
composedly
37+
undergraduate
38+
nonphotographic
39+
unendurably
40+
sniffier
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
strongbox
2+
boilersuit
3+
prelaw
4+
sorites
5+
swacked
6+
dockage
7+
remorid
8+
composedly
9+
sniffier
10+
largeness
11+
necrologist
12+
polygons
13+
stubborn
14+
disenthral
15+
undergraduate
16+
waif
17+
overpromise
18+
morgens
19+
unabbreviated
20+
indictments
21+
lightheartednesses
22+
dexes
23+
ridicules
24+
deploring
25+
webdiff
26+
renouncements
27+
gimmickry
28+
radioallergosorbent
29+
monumentalizing
30+
militarizations
31+
nonphotographic
32+
thriftlessness
33+
flabelliform
34+
derivers
35+
rumbly
36+
leukocytic
37+
straddler
38+
unendurably
39+
stealings
40+
hexagram
41+
cactoid

ts/codediff/NormalizeJSONOption.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface NormalizeJSONOptionProps {
1111
export function NormalizeJSONOption(props: NormalizeJSONOptionProps) {
1212
const {filePair} = props;
1313
const language = guessLanguageUsingFileName(filePair.a || filePair.b);
14-
if (language !== 'json') {
14+
if (language !== 'json' && language !== 'txt') {
1515
return null;
1616
}
1717

@@ -27,7 +27,11 @@ export function NormalizeJSONOption(props: NormalizeJSONOptionProps) {
2727
onChange={toggleNormalizeJSON}
2828
id="normalize-json"
2929
/>{' '}
30-
<label htmlFor="normalize-json">Normalize JSON (z): indent, sort keys</label>
30+
<label htmlFor="normalize-json">
31+
{language === 'json'
32+
? 'Normalize JSON (z): indent, sort keys'
33+
: 'Normalize text (z): sort lines'}
34+
</label>
3135
</div>
3236
);
3337
}

webdiff/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,29 @@ def get_pdiff_bbox(diff_path):
179179
}
180180

181181

182+
def normalize_text(in_path: str):
183+
data = [*open(in_path)]
184+
data.sort()
185+
_, norm_path = tempfile.mkstemp(suffix='.txt')
186+
with open(norm_path, 'w') as out:
187+
out.writelines(data)
188+
logging.debug(f'Normalized text file {in_path} -> {norm_path}')
189+
return norm_path
190+
191+
182192
@memoize
183193
def normalize_json(in_path: str):
194+
if in_path.lower().endswith('.txt'):
195+
return normalize_text(in_path)
196+
184197
with open(in_path) as f:
185198
try:
186199
data = json.load(f)
187200
except json.JSONDecodeError:
188201
# This would be a good place to try parsing as JSON5/JSONC.
189202
logging.debug(f'Unable to parse {in_path} as JSON')
190203
return in_path
204+
191205
_, norm_path = tempfile.mkstemp(suffix='.json')
192206
with open(norm_path, 'w') as out:
193207
json.dump(data, out, indent=2, sort_keys=True)

0 commit comments

Comments
 (0)