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 =
{
  unknown_tags: :bypass,
  github_flavored: true
}
VERSION =
"1.1.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, tmpdir = nil) ⇒ WordToMarkdown

Create a new WordToMarkdown object

input - a HTML string or path to an HTML file

Returns the WordToMarkdown object



28
29
30
31
32
# File 'lib/word-to-markdown.rb', line 28

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.



16
17
18
# File 'lib/word-to-markdown.rb', line 16

def converter
  @converter
end

#documentObject (readonly)

Returns the value of attribute document.



16
17
18
# File 'lib/word-to-markdown.rb', line 16

def document
  @document
end

Class Method Details

.osObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/word-to-markdown.rb', line 35

def self.os
  @os ||= (
  host_os = RbConfig::CONFIG['host_os']
  case host_os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    :windows
  when /darwin|mac os/
    :macosx
  when /linux/
    :linux
  when /solaris|bsd/
    :unix
  else
    raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
  end
  )
end

.run_command(*args) ⇒ Object



70
71
72
73
74
75
# File 'lib/word-to-markdown.rb', line 70

def self.run_command(*args)
  raise "LibreOffice executable not found" unless soffice?
  output, status = Open3.capture2e(soffice_path, *args)
  raise "Command `#{soffice_path} #{args.join(" ")}` failed: #{output}" if status != 0
  output
end

.soffice?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/word-to-markdown.rb', line 66

def self.soffice?
  @soffice ||= !(soffice_path.nil? || soffice_version.nil?)
end

.soffice_pathObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/word-to-markdown.rb', line 53

def self.soffice_path
  case os
  when :macosx
    %w[~/Applications /Applications]
      .map  { |f| File.expand_path(File.join(f, "/LibreOffice.app/Contents/MacOS/soffice")) }
      .find { |f| File.file?(f) }
  when :windows
    'C:\Program Files (x86)\LibreOffice 4\program\soffice.exe'
  else
    "soffice"
  end
end

.soffice_versionObject



77
78
79
80
81
# File 'lib/word-to-markdown.rb', line 77

def self.soffice_version
  return if soffice_path.nil?
  output, status = Open3.capture2e(soffice_path, "--version")
  output.strip.sub "LibreOffice ", "" if status == 0
end

Instance Method Details

#inspectObject

Pretty print the class in console



84
85
86
# File 'lib/word-to-markdown.rb', line 84

def inspect
  "<WordToMarkdown path=\"#{@document.path}\">"
end

#to_sObject



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

def to_s
  document.to_s
end