Skip to content

Commit

Permalink
fix: process code files to remove long lines
Browse files Browse the repository at this point in the history
PDF processing will fail with "! Dimension too large." if a line of text
is way too long. Implement a simple processing helper method to call
fold (from coreutils) to fold long lines for all code files.
  • Loading branch information
ublefo committed Mar 27, 2024
1 parent 596ae5c commit 27f120b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/helpers/file_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'zip'
require 'tmpdir'
require 'open3'
require 'shellwords'

module FileHelper
extend LogHelper
Expand Down Expand Up @@ -529,6 +530,23 @@ def task_submission_identifier_path_with_timestamp(type, task, timestamp)
"#{task_submission_identifier_path(type, task)}/#{timestamp.to_s}"
end

def text_line_length_limit(path, width: 200)
tempfile = Tempfile.new('processed-source')
begin
# run fold (from coreutils) on the source file, write the output to the temporary file
fold_cmd = "fold --width #{width} #{path.shellescape} > #{tempfile.path}"
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
# copy the output file to the input file path
FileUtils.cp tempfile.path, path
rescue => e
logger.error "Failed to run fold on #{path} to limit line width to #{width}. Rescued with error:\n\t#{e.message}"
ensure
# ensure the temporary file is closed and deleted immediately
tempfile.close
tempfile.unlink
end
end
# Export functions as module functions
module_function :accept_file
module_function :sanitized_path
Expand Down Expand Up @@ -572,4 +590,5 @@ def task_submission_identifier_path_with_timestamp(type, task, timestamp)
module_function :task_submission_identifier_path_with_timestamp
module_function :known_extension?
module_function :pages_in_pdf
module_function :text_line_length_limit
end
2 changes: 2 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,8 @@ def make_pdf
@files.each do |f|
if f[:type] == "document"
FileHelper.qpdf(f[:path])
elsif f[:type] == "code"
FileHelper.text_line_length_limit(f[:path])
end
end
render_to_string(template: '/task/task_pdf', layout: true)
Expand Down

0 comments on commit 27f120b

Please sign in to comment.