From 8215793b4dc865aebdc14a241ec34c6e59475db5 Mon Sep 17 00:00:00 2001 From: ublefo <90136978+ublefo@users.noreply.github.com> Date: Fri, 19 Apr 2024 14:04:38 +1000 Subject: [PATCH] enhance: render line-wrapped code files instead Run fold on provided files and compare the output with diff. If the file doesn't contain any lines that are over the configured threshold, it will be identical to the original. In this case we replace the temp file with a symlink for easy identification in the template. --- app/helpers/file_helper.rb | 16 ++++++++++++++-- app/views/task/task_pdf.pdf.erb | 9 ++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/helpers/file_helper.rb b/app/helpers/file_helper.rb index 9b90697156..b226c6bee8 100644 --- a/app/helpers/file_helper.rb +++ b/app/helpers/file_helper.rb @@ -1,3 +1,4 @@ +require 'English' require 'zip' require 'tmpdir' require 'open3' @@ -535,9 +536,20 @@ def line_wrap(path, width: 200) basename = File.basename(path) output = File.join(dir, "WRAPPED-#{basename}") begin - fold_cmd = "fold --width #{width} #{path.shellescape} > #{output}" logger.debug "Running fold on #{path} to limit line width to #{width}" - system_try_within 5, "Failed running fold on #{path} to limit line width to #{width}", fold_cmd + system("fold --width #{width} #{path.shellescape} > #{output}", exception: true) + # compare input and output files, replace the output with a symlink if they are identical + system "diff #{path.shellescape} #{output}" + case $CHILD_STATUS.exitstatus + when 0 + logger.debug "File #{path} does not contain lines longer than #{width}" + File.unlink(output) + File.symlink(path, output) + when 1 + logger.debug "File #{path} contains lines longer than #{width}, line wrapping will be applied" + else + raise "Error comparing original and line-wrapped files!" + end rescue => e logger.error "Failed to run fold on #{path} to limit line width to #{width}. Rescued with error:\n\t#{e.message}" end diff --git a/app/views/task/task_pdf.pdf.erb b/app/views/task/task_pdf.pdf.erb index 2a2d95f244..b869f69a75 100644 --- a/app/views/task/task_pdf.pdf.erb +++ b/app/views/task/task_pdf.pdf.erb @@ -112,8 +112,15 @@ No Tutor % Supervisor's Name <% if file[:type] == 'code' if (File.extname(file[:path])[1..-1]) != 'ipynb' + # for code files (except Jupyter Notebook), always use files with a WRAPPED- prefix in the filename + # Jupyter Notebooks are not processed with line wraps + dir = File.dirname(file[:path]) + name = File.basename(file[:path]) + pygments_lang = Task.pygments_lang(File.extname(file[:path])[1..-1]) + wrapped_file = File.join(dir, "WRAPPED-" + name) + # [TODO] add notice about line-wrapped files, files that aren't line-wrapped will be a symlink %> -\inputminted[breaklines,linenos,breakanywhere,tabsize=4]{<%= Task.pygments_lang(File.extname(file[:path])[1..-1]) %>}{<%= file[:path] %>} +\inputminted[breaklines,linenos,breakanywhere,tabsize=4]{<%= pygments_lang %>}{<%= wrapped_file %>} <% else %> \jupynotex{<%= file[:path] %>} <%