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.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WordToMarkdown

Create a new WordToMarkdown object

input - a HTML string or path to an HTML file

Returns the WordToMarkdown object



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

def initialize(path)
  @document = WordToMarkdown::Document.new path
  @converter = WordToMarkdown::Converter.new @document
  converter.convert!
end

Instance Attribute Details

#converterObject (readonly)

Returns the value of attribute converter.



14
15
16
# File 'lib/word-to-markdown.rb', line 14

def converter
  @converter
end

#documentObject (readonly)

Returns the value of attribute document.



14
15
16
# File 'lib/word-to-markdown.rb', line 14

def document
  @document
end

Class Method Details

.run_command(*args) ⇒ Object

Ideally this would be done via open3, but Travis CI can’t seen to find soffice when we do



56
57
58
# File 'lib/word-to-markdown.rb', line 56

def self.run_command(*args)
  `#{soffice_path} #{args.join(' ')}`
end

.soffice_pathObject



45
46
47
48
49
50
51
52
53
# File 'lib/word-to-markdown.rb', line 45

def self.soffice_path
  if RUBY_PLATFORM.include?("darwin")
    "/Applications/LibreOffice.app/Contents/MacOS/soffice"
  else
    soffice_path ||= which("soffice")
    soffice_path ||= which("soffice.bin")
    soffice_path ||= "soffice"
  end
end

.soffice_versionObject



60
61
62
# File 'lib/word-to-markdown.rb', line 60

def self.soffice_version
  run_command('--version').strip.sub "LibreOffice ", ""
end

.which(cmd) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/word-to-markdown.rb', line 33

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable? exe
    end
  end

  return nil
end

Instance Method Details

#inspectObject

Pretty print the class in console



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

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

#to_sObject



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

def to_s
  document.to_s
end