Class: Docx2gfm::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/docx2gfm.rb

Class Method Summary collapse

Class Method Details

.runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/docx2gfm.rb', line 11

def self.run

  # set default values for options
  options = {}
  options[:jekyll] = true
  options[:ref_style_links] = true

  # specify available options for the CLI
  parser = OptionParser.new do |opts|
    opts.banner = 'Usage: docx2gfm [options]'

    opts.on('-f', '--file FILE', '(required) The .docx file to convert to markdown') do |v|
      options[:file] = v
    end
    opts.on('-j', '--[no-]jekyll', '(optional) Prefix the markdown output with a jekyll frontmatter. Default: --jekyll') do |v|
      options[:jekyll] = v
    end
    opts.on('-r', '--[no-]ref-style-links', '(optional) Create reference style links at the end of the markdown. Default: --ref-style-links') do |v|
      options[:ref_style_links] = v
    end
    opts.on('-h', '--help', 'Display this help screen') do
      puts opts
      exit
    end
    opts.on('-v', '--version', 'Show version of docx2gfm') do
      puts Docx2gfm::VERSION
      exit
    end
  end

  # most useful way of creating a required parameter with OptionParser
  # https://stackoverflow.com/questions/1541294/how-do-you-specify-a-required-switch-not-argument-with-ruby-optionparser/1542658#1542658
  begin
    parser.parse!
    mandatory = [:file]
    missing = mandatory.select{ |param| options[param].nil? }
    raise OptionParser::MissingArgument, missing.join(', ') unless missing.empty?
  rescue OptionParser::ParseError => e
    puts e
    puts parser
    exit
  end

  # pass on options to the Docx2Gfm Converter, and run the conversion
  doc = DocxGfmConverter.new(options)
  doc.process_markdown
  puts doc

end