Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
with:
python-version: 3.9

- name: 'generate report'
- name: Generate report
run: |
pip install -r requirements.dev.txt
coverage run -m unittest discover tests
Expand All @@ -54,6 +54,39 @@ jobs:
flags: unittests
fail_ci_if_error: true

validate-html-output:
name: Validate HTML output
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@master
with:
python-version: 3.9

- name: Install HTML validator
run:
npm install html-validate

- name: Download two drafts
run: |
wget https://www.ietf.org/archive/id/draft-iab-xml2rfcv2-01.txt
wget https://www.ietf.org/archive/id/draft-iab-xml2rfcv2-02.txt

- name: Generate HTML outputs
run: |
python iddiff/iddiff.py draft-iab-xml2rfcv2-01.txt draft-iab-xml2rfcv2-02.txt > iddiff.html
python iddiff/iddiff.py -w draft-iab-xml2rfcv2-01.txt draft-iab-xml2rfcv2-02.txt > wdiff.html

- name: Validate iddiff HTML output
run: npm exec html-validate iddiff.html

- name: Validate iddiff wdiff HTML output
run: npm exec html-validate wdiff.html

tests-linux:
name: Unit Tests (Linux)
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions .htmlvalidate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"html-validate:recommended"
],
"rules": {
"long-title": "off"
}
}
48 changes: 29 additions & 19 deletions iddiff/iddiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,36 @@
compile(r'^draft-[-a-z0-9_.]+.*[0-9][0-9][0-9][0-9]$')]

TABLE = """
<table cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
<th class="header">{filename1}</th>
<td>&nbsp;</td>
<th class="header">{filename2}</th>
</tr>
{rows}
<table>
<tbody>
<tr>
<td>&nbsp;</td>
<th class="header" scope="col">{filename1}</th>
<td>&nbsp;</td>
<th class="header" scope="col">{filename2}</th>
</tr>{rows}
</tbody>
</table>"""

HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<title>{title}</title>
<style>
body {{font-family: monospace}}
table {{
border-spacing: 0;
}}
td {{
padding: 0;
white-space: pre;
vertical-align: top;
font-size: 0.86em;
}}
th {{
padding: 0;
text-align: center;
}}
.left {{ background-color: #EEE; }}
Expand All @@ -60,9 +66,7 @@
}}
</style>
</head>
<body>
{output}
</body>
<body>{output}</body>
</html>"""

UNCHANGED_ROW = """
Expand Down Expand Up @@ -192,21 +196,21 @@ def get_wdiff(first_id_lines, second_id_lines):
b=second_id_lines)
for tag, f1, f2, s1, s2 in seq.get_opcodes():
if tag == 'equal':
rows += first_id_lines[f1:f2]
rows += escape(first_id_lines[f1:f2])
elif tag == 'delete':
rows += '<span class="w-delete">{}</span>'.format(
first_id_lines[f1:f2])
escape(first_id_lines[f1:f2]))
elif tag == 'replace':
rows += '<span class="w-delete">{}</span>'.format(
first_id_lines[f1:f2])
escape(first_id_lines[f1:f2]))
rows += '<span class="w-insert">{}</span>'.format(
second_id_lines[s1:s2])
escape(second_id_lines[s1:s2]))
elif tag == 'insert':
rows += '<span class="w-insert">{}</span>'.format(
second_id_lines[s1:s2])
escape(second_id_lines[s1:s2]))
output = '<pre>{}</pre>'.format(rows)

return HTML.format(output=output)
return output


def get_diff_rows(first_id_lines, second_id_lines, context):
Expand Down Expand Up @@ -243,13 +247,19 @@ def get_iddiff(file1, file2, context_lines=None, table_only=False,
wdiff=False, skip_whitespaces=False):
'''Return iddiff output'''

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

if wdiff:
with open(file1, 'r') as file:
id_a_lines = ''.join(cleanup(file.readlines(), skip_whitespaces))
with open(file2, 'r') as file:
id_b_lines = ''.join(cleanup(file.readlines(), skip_whitespaces))

output = get_wdiff(id_a_lines, id_b_lines)

output = HTML.format(output=output, title=title)
else:
with open(file1, 'r') as file:
id_a_lines = cleanup(file.readlines(), skip_whitespaces)
Expand All @@ -261,7 +271,7 @@ def get_iddiff(file1, file2, context_lines=None, table_only=False,
output = get_html_table(file1, file2, rows)

if not table_only:
output = HTML.format(output=output)
output = HTML.format(output=output, title=title)

return output

Expand Down