Class: HTMLFilter::CLI

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

Overview

Simple Command line interface for HTMLFilter.

It can be configured via a YAML file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



663
664
665
666
667
# File 'lib/htmlfilter.rb', line 663

def initialize
  require 'optparse'
  @config_file = nil
  @options = {}
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



659
660
661
# File 'lib/htmlfilter.rb', line 659

def config_file
  @config_file
end

#optionsObject (readonly)

Returns the value of attribute options.



661
662
663
# File 'lib/htmlfilter.rb', line 661

def options
  @options
end

Class Method Details

.runObject



655
656
657
# File 'lib/htmlfilter.rb', line 655

def self.run
  new.run
end

Instance Method Details

#parserObject



669
670
671
672
673
674
# File 'lib/htmlfilter.rb', line 669

def parser
  OptionParser.new do |opt|
    opt.on('--config <YAML_FILE>', 'filter with custom configuration'){ |file| @config_file = file }
    opt.on('--debug', 'run in debug mode to see error details'){ $DEBUG = true }
  end
end

#runObject



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/htmlfilter.rb', line 683

def run
  parser.parse!
  begin
    files = ARGV
    files.each do |f|
      raise "htmlfilter: file not found -- #{f}" unless File.exist?(f)
    end
    files.each do |file|
      html = File.read(file)
      puts HTMLFilter.new(options).filter(html)
    end
  rescue => error
    raise error if $DEBUG
    $stderr.puts error
  end
end