Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created June 30, 2022 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterdavis/1e33110537cd50bedb101bcce4695c67 to your computer and use it in GitHub Desktop.
Save walterdavis/1e33110537cd50bedb101bcce4695c67 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "open3"
PandocCommandError = Class.new(StandardError)
class FileUploader < Shrine
plugin :derivatives, create_on_promote: true
plugin :versions
plugin :remove_attachment
Attacher.derivatives :convert do |original|
doc = original
if doc
output_html = Tempfile.new(%w[pandoc .html], binmode: true)
_stdout, stderr, status = Open3.capture3 *%W[pandoc -f docx #{doc.path} -t html5 -o #{output_html.path}]
raise PandocCommandError, stderr.chomp unless status.success?
output_html.open
record.body_html = output_html.read.force_encoding('UTF-8')
output_html.rewind
{ html: output_html }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment