Class: WordToMarkdown

Inherits:
Object
  • Object
show all
Includes:
Sys
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.4"

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



32
33
34
35
36
# File 'lib/word-to-markdown.rb', line 32

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.



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

def converter
  @converter
end

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

Class Method Details

.osObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/word-to-markdown.rb', line 39

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



78
79
80
81
82
83
84
85
# File 'lib/word-to-markdown.rb', line 78

def self.run_command(*args)
  raise "LibreOffice executable not found" unless soffice?
  raise "LibreOffice already running" if soffice_open?

  output, status = Open3.capture2e(soffice_path, *args)
  raise "Command `#{soffice_path} #{args.join(" ")}` failed: #{output}" if status != 0
  output
end

.soffice?Boolean

Returns:

  • (Boolean)


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

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

.soffice_open?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/word-to-markdown.rb', line 74

def self.soffice_open?
  ProcTable.ps.any? { |p| p.exe == soffice_path }
end

.soffice_pathObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/word-to-markdown.rb', line 57

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



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

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



94
95
96
# File 'lib/word-to-markdown.rb', line 94

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

#to_sObject



98
99
100
# File 'lib/word-to-markdown.rb', line 98

def to_s
  document.to_s
end