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
2 changes: 1 addition & 1 deletion assets/css/main.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/css/main.css.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions assets/sass/components/_highlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ table.src tr td.old {
table.src tr td.new {
background: #d4edda;
}
[data-bs-theme=dark] table.src tr td.old {
background: #542326;
}
[data-bs-theme=dark] table.src tr td.new {
background: #1c4328;
}
.highlight {
border-radius: var(--bs-border-radius);
}
69 changes: 54 additions & 15 deletions exercise/static/exercise/assessment.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,62 @@ $(function () {
const element = $(this);
const fileId = element.data('id');
const fileViewable = element.data('viewable');
const fileUrl = element.data('url');
let fileUrl = element.data('url');
if (fileUrl && fileViewable && !loadedFiles.has(fileId)) {
loadedFiles.add(fileId);
$.get(fileUrl, function (data) {
const text = $('<pre/>').text(data);
text.attr('data-url', fileUrl);
text.attr('data-filename', element.data('filename'));
element.find('.submitted-file-data').html(text);
const extraButtons = element.addStickyButton('submissionSticky');
text.highlightCode({extraButtons, compareMode: fileUrl.includes('compare_to=')});
})
.fail(function () {
element.find('.submitted-file-error').removeClass('d-none');
})
.always(function () {
element.find('.submitted-file-progress').addClass('d-none');
});

const loadFileContent = (url) => {
element.find('.submitted-file-progress').removeClass('d-none');
element.find('.submitted-file-error').addClass('d-none');

$.get(url, function (data) {
const text = $('<pre/>').text(data);
text.attr('data-url', url);
text.attr('data-filename', element.data('filename'));
element.find('.submitted-file-data').html(text);
const extraButtons = element.addStickyButton('submissionSticky');

// Add ignore whitespace toggle button if comparing files
if (url.includes('compare_to=')) {
const urlObj = new URL(url, window.location.origin);
const isIgnoringWhitespace = urlObj.searchParams.get('ignore_trailing_whitespace') === 'yes';

const whitespaceButton = {
action: () => {
// Toggle the parameter in the current file URL
const newFileUrl = new URL(url, window.location.origin);
const currentIgnoring = newFileUrl.searchParams.get('ignore_trailing_whitespace') === 'yes';

if (currentIgnoring) {
newFileUrl.searchParams.delete('ignore_trailing_whitespace');
} else {
newFileUrl.searchParams.set('ignore_trailing_whitespace', 'yes');
}

// Update the stored URL and reload this specific file
fileUrl = newFileUrl.toString();
element.data('url', fileUrl);
loadFileContent(fileUrl);
},
icon: isIgnoringWhitespace ? 'check-square' : 'square',
text: _('Ignore trailing whitespace'),
toggle: true,
};

extraButtons.push(whitespaceButton);
}

text.highlightCode({extraButtons, compareMode: url.includes('compare_to=')});
})
.fail(function () {
element.find('.submitted-file-error').removeClass('d-none');
})
.always(function () {
element.find('.submitted-file-progress').addClass('d-none');
});
};

loadFileContent(fileUrl);
}
});

Expand Down
10 changes: 10 additions & 0 deletions exercise/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ def get_compared_submission_file_data(self, submission_id: int):
except ValueError as e:
raise Http404() from e

@staticmethod
def strip_trailing_whitespace(text: str) -> str:
return '\n'.join(line.rstrip() for line in text.splitlines())

def get(self, request, *args, **kwargs):
try:
with self.file.file_object.open() as f:
Expand All @@ -593,9 +597,15 @@ def get(self, request, *args, **kwargs):
else self.get_compared_submission_file_data(compare_to)
)
submitted_data = bytedata.decode('utf-8', 'ignore')

if request.GET.get('ignore_trailing_whitespace', 'no') == 'yes':
compared_data = self.strip_trailing_whitespace(compared_data)
submitted_data = self.strip_trailing_whitespace(submitted_data)

# Ensure that both files end with a newline, otherwise the diff output might be mangled
submitted_data += '\n' if not submitted_data.endswith('\n') else ''
compared_data += '\n' if not compared_data.endswith('\n') else ''

diff = ndiff(compared_data.splitlines(keepends=True), submitted_data.splitlines(keepends=True))
diff_text = ''.join([line for line in diff if line[0] != '?'])
bytedata = diff_text.encode('utf-8')
Expand Down
Loading