diff --git a/config/configuration.yml.example b/config/configuration.yml.example index cdfd516b5..5366a2911 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -242,6 +242,7 @@ default: # Absolute path (e.g. /usr/local/bin/pandoc) to the Pandoc command # used to convert supported attachments to Markdown for preview. #pandoc_command: + #pandoc_extensions: .docx .xlsx .pptx .odt # specific configuration options for production environment # that overrides the default ones diff --git a/lib/redmine/markdownizer.rb b/lib/redmine/markdownizer.rb index 898ef4a9b..112ed02c9 100644 --- a/lib/redmine/markdownizer.rb +++ b/lib/redmine/markdownizer.rb @@ -27,7 +27,7 @@ module Redmine COMMAND = (Redmine::Configuration['pandoc_command'] || 'pandoc').freeze MAX_PREVIEW_SIZE = 100.kilobytes - SUPPORTED_EXTENSIONS = %w(.doc .docx .xls .xlsx .ppt .pptx .odt .ods .odp).freeze + SUPPORTED_EXTENSIONS = (Redmine::Configuration['pandoc_extensions'] || %w(.docx .xlsx .pptx .odt)).freeze def self.supports?(filename) SUPPORTED_EXTENSIONS.include?(File.extname(filename.to_s).downcase) @@ -39,28 +39,29 @@ module Redmine directory = File.dirname(target) FileUtils.mkdir_p(directory) - cmd = "#{shell_quote COMMAND} #{shell_quote source} -t gfm" + args = [COMMAND, source, "-t", "gfm"] pid = nil output = Tempfile.new('markdownized-preview') begin Timeout.timeout(Redmine::Configuration['thumbnails_generation_timeout'].to_i) do - pid = Process.spawn(cmd, out: output.path) + pid = Process.spawn(*args, out: output.path) _, status = Process.wait2(pid) unless status.success? - logger.error("Markdown conversion failed (#{status.exitstatus}):\nCommand: #{cmd}") + logger.error("Markdown conversion failed (#{status.exitstatus}):\nCommand: #{args.shelljoin}") return nil end end rescue Timeout::Error Process.kill('KILL', pid) if pid - logger.error("Markdown conversion timed out:\nCommand: #{cmd}") + logger.error("Markdown conversion timed out:\nCommand: #{args.shelljoin}") return nil rescue => e - logger.error("Markdown conversion failed:\nCommand: #{cmd}\nException was: #{e.message}") + logger.error("Markdown conversion failed:\nCommand: #{args.shelljoin}\nException was: #{e.message}") return nil ensure output.close + Process.detach(pid) end preview = File.binread(output.path, MAX_PREVIEW_SIZE + 1) || +"" @@ -74,8 +75,8 @@ module Redmine return @available if defined?(@available) begin - `#{shell_quote COMMAND} --version` - @available = $?.success? + version = `#{shell_quote COMMAND} --version`[/pandoc\s+([\d.]+)/, 1].split('.').map(&:to_i) + @available = ($?.success? && (version <=> [3, 9]) >= 0) # Pandoc.version >= 3.9 rescue @available = false end