Skip to content

Commit 5204b8c

Browse files
Fatima-Tahirihalaij1
authored andcommitted
Add 'ignore trailing whitespace' button when comparing submission
Fixes #1457
1 parent dc9b9b8 commit 5204b8c

5 files changed

Lines changed: 72 additions & 17 deletions

File tree

assets/css/main.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/css/main.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/sass/components/_highlight.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ table.src tr td.old {
2626
table.src tr td.new {
2727
background: #d4edda;
2828
}
29+
[data-bs-theme=dark] table.src tr td.old {
30+
background: #542326;
31+
}
32+
[data-bs-theme=dark] table.src tr td.new {
33+
background: #1c4328;
34+
}
2935
.highlight {
3036
border-radius: var(--bs-border-radius);
3137
}

exercise/static/exercise/assessment.js

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,62 @@ $(function () {
9090
const element = $(this);
9191
const fileId = element.data('id');
9292
const fileViewable = element.data('viewable');
93-
const fileUrl = element.data('url');
93+
let fileUrl = element.data('url');
9494
if (fileUrl && fileViewable && !loadedFiles.has(fileId)) {
9595
loadedFiles.add(fileId);
96-
$.get(fileUrl, function (data) {
97-
const text = $('<pre/>').text(data);
98-
text.attr('data-url', fileUrl);
99-
text.attr('data-filename', element.data('filename'));
100-
element.find('.submitted-file-data').html(text);
101-
const extraButtons = element.addStickyButton('submissionSticky');
102-
text.highlightCode({extraButtons, compareMode: fileUrl.includes('compare_to=')});
103-
})
104-
.fail(function () {
105-
element.find('.submitted-file-error').removeClass('d-none');
106-
})
107-
.always(function () {
108-
element.find('.submitted-file-progress').addClass('d-none');
109-
});
96+
97+
const loadFileContent = (url) => {
98+
element.find('.submitted-file-progress').removeClass('d-none');
99+
element.find('.submitted-file-error').addClass('d-none');
100+
101+
$.get(url, function (data) {
102+
const text = $('<pre/>').text(data);
103+
text.attr('data-url', url);
104+
text.attr('data-filename', element.data('filename'));
105+
element.find('.submitted-file-data').html(text);
106+
const extraButtons = element.addStickyButton('submissionSticky');
107+
108+
// Add ignore whitespace toggle button if comparing files
109+
if (url.includes('compare_to=')) {
110+
const urlObj = new URL(url, window.location.origin);
111+
const isIgnoringWhitespace = urlObj.searchParams.get('ignore_trailing_whitespace') === 'yes';
112+
113+
const whitespaceButton = {
114+
action: () => {
115+
// Toggle the parameter in the current file URL
116+
const newFileUrl = new URL(url, window.location.origin);
117+
const currentIgnoring = newFileUrl.searchParams.get('ignore_trailing_whitespace') === 'yes';
118+
119+
if (currentIgnoring) {
120+
newFileUrl.searchParams.delete('ignore_trailing_whitespace');
121+
} else {
122+
newFileUrl.searchParams.set('ignore_trailing_whitespace', 'yes');
123+
}
124+
125+
// Update the stored URL and reload this specific file
126+
fileUrl = newFileUrl.toString();
127+
element.data('url', fileUrl);
128+
loadFileContent(fileUrl);
129+
},
130+
icon: isIgnoringWhitespace ? 'check-square' : 'square',
131+
text: _('Ignore trailing whitespace'),
132+
toggle: true,
133+
};
134+
135+
extraButtons.push(whitespaceButton);
136+
}
137+
138+
text.highlightCode({extraButtons, compareMode: url.includes('compare_to=')});
139+
})
140+
.fail(function () {
141+
element.find('.submitted-file-error').removeClass('d-none');
142+
})
143+
.always(function () {
144+
element.find('.submitted-file-progress').addClass('d-none');
145+
});
146+
};
147+
148+
loadFileContent(fileUrl);
110149
}
111150
});
112151

exercise/views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ def get_compared_submission_file_data(self, submission_id: int):
577577
except ValueError as e:
578578
raise Http404() from e
579579

580+
@staticmethod
581+
def strip_trailing_whitespace(text: str) -> str:
582+
return '\n'.join(line.rstrip() for line in text.splitlines())
583+
580584
def get(self, request, *args, **kwargs):
581585
try:
582586
with self.file.file_object.open() as f:
@@ -593,9 +597,15 @@ def get(self, request, *args, **kwargs):
593597
else self.get_compared_submission_file_data(compare_to)
594598
)
595599
submitted_data = bytedata.decode('utf-8', 'ignore')
600+
601+
if request.GET.get('ignore_trailing_whitespace', 'no') == 'yes':
602+
compared_data = self.strip_trailing_whitespace(compared_data)
603+
submitted_data = self.strip_trailing_whitespace(submitted_data)
604+
596605
# Ensure that both files end with a newline, otherwise the diff output might be mangled
597606
submitted_data += '\n' if not submitted_data.endswith('\n') else ''
598607
compared_data += '\n' if not compared_data.endswith('\n') else ''
608+
599609
diff = ndiff(compared_data.splitlines(keepends=True), submitted_data.splitlines(keepends=True))
600610
diff_text = ''.join([line for line in diff if line[0] != '?'])
601611
bytedata = diff_text.encode('utf-8')

0 commit comments

Comments
 (0)