Skip to content

Commit

Permalink
fix: Show only filename in HTML output (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
kesara committed Jul 13, 2022
1 parent 9e7081d commit 480521a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 11 additions & 4 deletions iddiff/iddiff.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from argparse import ArgumentParser
from difflib import _mdiff as mdiff, SequenceMatcher
from html import escape
from pathlib import Path
from re import compile
from string import whitespace
from sys import exit, stderr, stdout
Expand Down Expand Up @@ -142,6 +143,12 @@
'\uFEFF']) # zero width non-breaking space


def get_filename(filename):
'''Return HTML escaped filename form user input path'''

return escape(Path(filename).name)


def cleanup(lines, skip_whitespace):
'''Removes skippable content, shrinks multiple empty lines
If a link only contains WHITESPACE,
Expand Down Expand Up @@ -238,8 +245,8 @@ def get_diff_rows(first_id_lines, second_id_lines, context):

def get_html_table(filename1, filename2, rows):
'''Return HTML table'''
return TABLE.format(filename1=escape(filename1),
filename2=escape(filename2),
return TABLE.format(filename1=get_filename(filename1),
filename2=get_filename(filename2),
rows=rows)


Expand All @@ -248,8 +255,8 @@ def get_iddiff(file1, file2, context_lines=None, table_only=False,
'''Return iddiff output'''

title = 'Diff: {file1} - {file2}'.format(
file1=escape(file1),
file2=escape(file2))
file1=get_filename(file1),
file2=get_filename(file2))

if wdiff:
with open(file1, 'r') as file:
Expand Down
16 changes: 14 additions & 2 deletions tests/test_iddiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import sys

from iddiff.iddiff import (
add_span, cleanup, get_diff_rows, get_html_table, get_iddiff,
get_wdiff, main, parse_args)
add_span, cleanup, get_diff_rows, get_filename, get_html_table,
get_iddiff, get_wdiff, main, parse_args)

HEADERS_AND_FOOTERS = """
Crocker [Page 5]
Expand Down Expand Up @@ -250,3 +250,15 @@ def test_main_file_error(self):
main()

self.assertTrue(output.getvalue().startswith('iddiff:'))

def test_get_filename(self):
PATH_INPUTS = [
'foobar.txt',
'foobar/foobar.txt',
'foo/bar/foobar.txt',
'/foo/bar/foobar.txt',
'../foo/bar/foobar.txt',
'./foobar.txt']

for path in PATH_INPUTS:
self.assertEqual(get_filename(path), 'foobar.txt')

0 comments on commit 480521a

Please sign in to comment.