class WordToMarkdown

Constants

PATHS

Paths to look for LibreOffice, in order of preference

REVERSE_MARKDOWN_OPTIONS

Options to be passed to Reverse Markdown

SOFFICE_VERSION_REQUIREMENT

Minimum version of LibreOffice Required

VERSION

Attributes

converter[R]
document[R]

Public Class Methods

logger() click to toggle source

@return Logger instance

# File lib/word-to-markdown.rb, line 86
def logger
  @logger ||= begin
    logger = Logger.new(STDOUT)
    logger.level = Logger::ERROR unless ENV['DEBUG']
    logger
  end
end
new(path, tmpdir = nil) click to toggle source

Create a new WordToMarkdown object

@param path [string] Path to the Word document @param tmpdir [string] Path to a working directory to use @return [WordToMarkdown] WordToMarkdown object with the converted document

# File lib/word-to-markdown.rb, line 46
def initialize(path, tmpdir = nil)
  @document = WordToMarkdown::Document.new path, tmpdir
  @converter = WordToMarkdown::Converter.new @document
  converter.convert!
end
run_command(*args) click to toggle source

Run an soffice command

@param args [string] one or more arguments to pass to the sofice command @return [string] the command output

# File lib/word-to-markdown.rb, line 63
def run_command(*args)
  raise 'LibreOffice already running' if soffice.open?

  output, status = Open3.capture2e(soffice.path, *args)
  logger.debug output
  raise "Command `#{soffice.path} #{args.join(' ')}` failed: #{output}" if status.exitstatus != 0
  output
end
soffice() click to toggle source

Returns a Cliver::Dependency object representing our soffice dependency

Attempts to resolve by looking at PATH followed by paths in the PATHS constant

Methods used internally:

path    - returns the resolved path. Raises an error if not satisfied
version - returns the resolved version
open    - is the dependency currently open/running?

@return Cliver::Dependency instance

# File lib/word-to-markdown.rb, line 81
def soffice
  @soffice ||= Cliver::Dependency.new('soffice', *soffice_dependency_args)
end

Private Class Methods

soffice_dependency_args() click to toggle source

Workaround for two upstream bugs:

  1. `soffice.exe –version` on windows opens a popup and retuns a null string when manually closed

  2. Even if the second argument to Cliver is nil, Cliver thinks there's a requirement and will shell out to `soffice.exe –version`

In order to support Windows, don't pass any version requirement to Cliver

# File lib/word-to-markdown.rb, line 101
def soffice_dependency_args
  args = [path: PATHS.join(File::PATH_SEPARATOR)]
  if Gem.win_platform?
    args
  else
    args.unshift SOFFICE_VERSION_REQUIREMENT
  end
end

Public Instance Methods

to_s() click to toggle source

Helper method to return the document body, as markdown @return [string] the document body, as markdown

# File lib/word-to-markdown.rb, line 54
def to_s
  document.to_s
end