Class: WordToMarkdown

Inherits:
Object
  • Object
show all
Defined in:
lib/word-to-markdown.rb,
lib/word-to-markdown/version.rb,
lib/word-to-markdown/document.rb,
lib/word-to-markdown/converter.rb

Defined Under Namespace

Classes: Converter, Document

Constant Summary collapse

REVERSE_MARKDOWN_OPTIONS =

Options to be passed to Reverse Markdown

{
  unknown_tags: :bypass,
  github_flavored: true
}.freeze
SOFFICE_VERSION_REQUIREMENT =

Minimum version of LibreOffice Required

'> 4.0'
PATHS =

Paths to look for LibreOffice, in order of preference

[
  '*', # Sub'd for ENV["PATH"]
  '~/Applications/LibreOffice.app/Contents/MacOS',
  '/Applications/LibreOffice.app/Contents/MacOS',
  '/Program Files/LibreOffice 5/program',
  '/Program Files (x86)/LibreOffice 4/program'
].freeze
VERSION =
'1.1.9'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, tmpdir = nil) ⇒ WordToMarkdown

Create a new WordToMarkdown object

Parameters:

  • path (string)

    Path to the Word document

  • tmpdir (string) (defaults to: nil)

    Path to a working directory to use



46
47
48
49
50
# 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

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



21
22
23
# File 'lib/word-to-markdown.rb', line 21

def converter
  @converter
end

#documentObject (readonly)

Returns the value of attribute document.



21
22
23
# File 'lib/word-to-markdown.rb', line 21

def document
  @document
end

Class Method Details

.loggerObject

Returns Logger instance.

Returns:

  • Logger instance



87
88
89
90
91
92
93
# File 'lib/word-to-markdown.rb', line 87

def logger
  @logger ||= begin
    logger = Logger.new($stdout)
    logger.level = Logger::ERROR unless ENV['DEBUG']
    logger
  end
end

.run_command(*args) ⇒ string

Run an soffice command

Parameters:

  • args (string)

    one or more arguments to pass to the sofice command

Returns:

  • (string)

    the command output



63
64
65
66
67
68
69
70
71
# 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

.sofficeObject

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?

Returns:

  • Cliver::Dependency instance



82
83
84
# File 'lib/word-to-markdown.rb', line 82

def soffice
  @soffice ||= Cliver::Dependency.new('soffice', *soffice_dependency_args)
end

Instance Method Details

#to_sstring

Helper method to return the document body, as markdown

Returns:

  • (string)

    the document body, as markdown



54
55
56
# File 'lib/word-to-markdown.rb', line 54

def to_s
  document.to_s
end