Skip to content

Commit

Permalink
enhance: truncate super long lines in code files rendered in pdf
Browse files Browse the repository at this point in the history
Set a hard limit of 1000 characters, and truncate everything in the same
line after the limit is reached. Otherwise we could get PDF files with
hundreds of pages which is completely unreadable.
  • Loading branch information
ublefo committed Apr 19, 2024
1 parent d70c46a commit d83678c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions app/helpers/file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,14 +531,20 @@ def task_submission_identifier_path_with_timestamp(type, task, timestamp)
"#{task_submission_identifier_path(type, task)}/#{timestamp.to_s}"
end

# Apply line wrapping to a given file. The default 160-character limit is about two lines in the rendered PDF.
# Apply line wrapping to a given file.
# The default 160-character limit is about two lines in the rendered PDF.
# There is also a hard limit of 1000 characters, anything longer will be truncated to 1000 characters.
def line_wrap(path, width: 160)
hard_limit = 1000
dir = File.dirname(path)
basename = File.basename(path)
temp_file = Tempfile.new('truncated_file')
output = File.join(dir, "WRAPPED-#{basename}")
begin
logger.debug "Running fold on #{path} to limit line width to #{width}"
system("fold --width #{width} #{path.shellescape} > #{output}", exception: true)
logger.debug "Applying hard column width limit of #{hard_limit} to #{path}"
system("cut -c-#{hard_limit} #{path.shellescape} > #{temp_file.path}", exception: true)
logger.debug "Applying line wrapping on #{temp_file.path} to limit line width to #{width}"
system("fold --width #{width} #{temp_file.path} > #{output}", exception: true)
# compare input and output files, replace the output with a symlink if they are identical
system "diff #{path.shellescape} #{output} > /dev/null"
case $CHILD_STATUS.exitstatus
Expand All @@ -547,13 +553,16 @@ def line_wrap(path, width: 160)
File.unlink(output)
File.symlink(path, output)
when 1
logger.debug "File #{path} contains lines longer than #{width}, line wrapping will be applied"
logger.debug "File #{path} contains lines longer than #{width}, line wrapping has been 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}"
logger.error "Failed to apply line wrapping on #{path}. Rescued with error:\n\t#{e.message}"
end
ensure
temp_file.close
temp_file.unlink
end
# Export functions as module functions
module_function :accept_file
Expand Down
2 changes: 1 addition & 1 deletion app/views/task/task_pdf.pdf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ No Tutor % Supervisor's Name
%>
<% unless File.symlink?(wrapped_file) %>
\begin{tcolorbox}[colback=blue!5!white,colframe=blue!75!black]
This file has additional line breaks applied by <%= @doubtfire_product_name %> because they contain lines longer than the configured limit. The orginal submission can be retrieved via the "Download Uploaded Files" function.
This file has additional line breaks applied by <%= @doubtfire_product_name %> because they contain lines longer than the configured limit. Lines over 1000 characters long have been truncated to limit PDF page count. The orginal submission can be retrieved via the "Download Uploaded Files" function.
\end{tcolorbox}
<% end %>
\inputminted[breaklines,linenos,breakanywhere,tabsize=4]{<%= pygments_lang %>}{<%= wrapped_file %>}
Expand Down

0 comments on commit d83678c

Please sign in to comment.