Make stuff work in chrome and add headerless output option
This commit is contained in:
parent
02fac034b9
commit
3cccc70ac4
1 changed files with 32 additions and 15 deletions
47
html_diff.py
47
html_diff.py
|
|
@ -136,7 +136,7 @@ HTML_TEMPLATE = r'''
|
||||||
|
|
||||||
.filename {
|
.filename {
|
||||||
max-width: 30em;
|
max-width: 30em;
|
||||||
text-overflow: ellipsis clip;
|
text-overflow: ellipsis;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
direction: rtl;
|
direction: rtl;
|
||||||
|
|
@ -254,7 +254,9 @@ HTML_TEMPLATE = r'''
|
||||||
.lineno.left.change, .lineno.right.change {
|
.lineno.left.change, .lineno.right.change {
|
||||||
grid-column: 1 / span 2;
|
grid-column: 1 / span 2;
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: subgrid;
|
grid-template-columns: 1fr 1fr;
|
||||||
|
padding-left: 0;
|
||||||
|
padding-right: 0;
|
||||||
grid-auto-flow: dense;
|
grid-auto-flow: dense;
|
||||||
/* To make alignment of left line number work, since we loose margin and padding control using ::before. */
|
/* To make alignment of left line number work, since we loose margin and padding control using ::before. */
|
||||||
column-gap: 10px;
|
column-gap: 10px;
|
||||||
|
|
@ -265,7 +267,7 @@ HTML_TEMPLATE = r'''
|
||||||
align-self: stretch;
|
align-self: stretch;
|
||||||
grid-column: 1;
|
grid-column: 1;
|
||||||
border-right: 1px solid #e0e0e0;
|
border-right: 1px solid #e0e0e0;
|
||||||
margin-right: -5px;
|
margin-right: -6px; /* move border into column gap, and 1px over to align with other borders */
|
||||||
}
|
}
|
||||||
|
|
||||||
.lineno.left.change::before {
|
.lineno.left.change::before {
|
||||||
|
|
@ -273,7 +275,7 @@ HTML_TEMPLATE = r'''
|
||||||
align-self: stretch;
|
align-self: stretch;
|
||||||
grid-column: 2;
|
grid-column: 2;
|
||||||
border-left: 1px solid #e0c8c8; /* pick a darker border color inside the light red gutter */
|
border-left: 1px solid #e0c8c8; /* pick a darker border color inside the light red gutter */
|
||||||
margin-left: calc(-5px - 1px); /* move border into column gap, and 1px over to align with other borders */
|
margin-left: -5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.lineno.left.insert {
|
.lineno.left.insert {
|
||||||
|
|
@ -618,7 +620,7 @@ def html_diff_content(old, new, lexer):
|
||||||
def html_diff_block(old, new, filename, lexer):
|
def html_diff_block(old, new, filename, lexer):
|
||||||
code = html_diff_content(old, new, lexer)
|
code = html_diff_content(old, new, lexer)
|
||||||
return textwrap.dedent(f'''<div class="file-container">
|
return textwrap.dedent(f'''<div class="file-container">
|
||||||
<div class="file-title"><div class="filename">{filename}</div></div>
|
<div class="file-title"><div class="filename">‭{filename}</div></div>
|
||||||
<div class="diff">
|
<div class="diff">
|
||||||
{code}
|
{code}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -647,6 +649,23 @@ creates an html page which highlights the differences between the two. """
|
||||||
print(f'{longname:<20} alias {"/".join(aliases)} for {", ".join(filename_patterns)}')
|
print(f'{longname:<20} alias {"/".join(aliases)} for {", ".join(filename_patterns)}')
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
if args.pagetitle or (args.old and args.new):
|
||||||
|
pagetitle = args.pagetitle or f'diff: {args.old} / {args.new}'
|
||||||
|
else:
|
||||||
|
pagetitle = 'diff'
|
||||||
|
|
||||||
|
if args.syntax_css:
|
||||||
|
syntax_css = Path(args.syntax_css).read_text()
|
||||||
|
else:
|
||||||
|
syntax_css = PYGMENTS_CSS
|
||||||
|
|
||||||
|
if args.header:
|
||||||
|
print(string.Template(HTML_TEMPLATE).substitute(
|
||||||
|
title=pagetitle,
|
||||||
|
pygments_css=syntax_css,
|
||||||
|
body='$body'), file=args.output)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
if not (args.old and args.new):
|
if not (args.old and args.new):
|
||||||
print('Error: The command line arguments "old" and "new" are required.', file=sys.stderr)
|
print('Error: The command line arguments "old" and "new" are required.', file=sys.stderr)
|
||||||
parser.print_usage()
|
parser.print_usage()
|
||||||
|
|
@ -679,12 +698,6 @@ creates an html page which highlights the differences between the two. """
|
||||||
for fn in new.glob('**/*'):
|
for fn in new.glob('**/*'):
|
||||||
found_files[str(fn.relative_to(new))][1] = fn
|
found_files[str(fn.relative_to(new))][1] = fn
|
||||||
|
|
||||||
pagetitle = args.pagetitle or f'diff: {old} / {new}'
|
|
||||||
if args.syntax_css:
|
|
||||||
syntax_css = Path(args.syntax_css).read_text()
|
|
||||||
else:
|
|
||||||
syntax_css = PYGMENTS_CSS
|
|
||||||
|
|
||||||
diff_blocks = []
|
diff_blocks = []
|
||||||
for suffix, (old, new) in sorted(found_files.items()):
|
for suffix, (old, new) in sorted(found_files.items()):
|
||||||
old_text = '' if old is None else old.read_text()
|
old_text = '' if old is None else old.read_text()
|
||||||
|
|
@ -703,11 +716,15 @@ creates an html page which highlights the differences between the two. """
|
||||||
lexer = get_lexer_by_name('text')
|
lexer = get_lexer_by_name('text')
|
||||||
|
|
||||||
diff_blocks.append(html_diff_block(old_text, new_text, suffix, lexer))
|
diff_blocks.append(html_diff_block(old_text, new_text, suffix, lexer))
|
||||||
|
body = '\n'.join(diff_blocks)
|
||||||
|
|
||||||
print(string.Template(HTML_TEMPLATE).substitute(
|
if args.content:
|
||||||
title=pagetitle,
|
print(body, file=args.output)
|
||||||
pygments_css=syntax_css,
|
else:
|
||||||
body='\n'.join(diff_blocks)), file=args.output)
|
print(string.Template(HTML_TEMPLATE).substitute(
|
||||||
|
title=pagetitle,
|
||||||
|
pygments_css=syntax_css,
|
||||||
|
body=body), file=args.output)
|
||||||
|
|
||||||
if args.open:
|
if args.open:
|
||||||
webbrowser.open('file://' + str(Path(args.output.name).absolute()))
|
webbrowser.open('file://' + str(Path(args.output.name).absolute()))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue