5
5
6
6
from __future__ import annotations
7
7
8
+ import collections
8
9
import datetime
9
10
import json
10
11
import os
11
12
import re
12
13
import shutil
13
- from collections import Counter
14
14
15
15
from dataclasses import dataclass
16
16
from typing import Any , Dict , Iterable , List , Optional , Tuple , TYPE_CHECKING , cast
@@ -370,9 +370,12 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
370
370
# Write the HTML page for this file.
371
371
file_data = self .datagen .data_for_file (ftr .fr , ftr .analysis )
372
372
373
- contexts = Counter (c for cline in file_data .lines for c in cline .contexts )
373
+ contexts = collections . Counter (c for cline in file_data .lines for c in cline .contexts )
374
374
context_codes = {y : i for (i , y ) in enumerate (x [0 ] for x in contexts .most_common ())}
375
- contexts_json = json .dumps ({v : k for (k , v ) in context_codes .items ()}, indent = 2 )
375
+ if context_codes :
376
+ contexts_json = json .dumps ({v : k for (k , v ) in context_codes .items ()}, indent = 2 )
377
+ else :
378
+ contexts_json = None
376
379
377
380
for ldata in file_data .lines :
378
381
# Build the HTML for the line.
@@ -382,13 +385,11 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
382
385
html_parts .append (escape (tok_text ))
383
386
else :
384
387
tok_html = escape (tok_text ) or ' '
385
- html_parts .append (
386
- f'<span class="{ tok_type } ">{ tok_html } </span>'
387
- )
388
+ html_parts .append (f'<span class="{ tok_type } ">{ tok_html } </span>' )
388
389
ldata .html = '' .join (html_parts )
389
-
390
390
ldata .context_str = "," .join (
391
- str (context_codes [c_context ]) for c_context in ldata .context_list )
391
+ str (context_codes [c_context ]) for c_context in ldata .context_list
392
+ )
392
393
393
394
if ldata .short_annotations :
394
395
# 202F is NARROW NO-BREAK SPACE.
@@ -422,13 +423,10 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
422
423
)
423
424
ldata .css_class = ' ' .join (css_classes ) or "pln"
424
425
425
- if context_codes :
426
- file_data .__dict__ ["contexts_json" ] = contexts_json
427
- else :
428
- file_data .__dict__ ["contexts_json" ] = None
429
426
html_path = os .path .join (self .directory , ftr .html_filename )
430
427
html = self .source_tmpl .render ({
431
428
** file_data .__dict__ ,
429
+ "contexts_json" : contexts_json ,
432
430
'prev_html' : prev_html ,
433
431
'next_html' : next_html ,
434
432
})
0 commit comments