Class: AnyStyle::CLI::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/anystyle/cli/commands/base.rb

Direct Known Subclasses

Check, Find, Parse, Train

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/anystyle/cli/commands/base.rb', line 7

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/anystyle/cli/commands/base.rb', line 5

def options
  @options
end

#output_folderObject (readonly)

Returns the value of attribute output_folder.



5
6
7
# File 'lib/anystyle/cli/commands/base.rb', line 5

def output_folder
  @output_folder
end

Instance Method Details

#each_format(&block) ⇒ Object



27
28
29
# File 'lib/anystyle/cli/commands/base.rb', line 27

def each_format(&block)
  options[:format].each(&block)
end

#extsub(path, new_extname) ⇒ Object



63
64
65
66
# File 'lib/anystyle/cli/commands/base.rb', line 63

def extsub(path, new_extname)
  basename = path.basename(path.extname)
  path.dirname.join("#{basename}#{new_extname}")
end

#find(input, opts = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/anystyle/cli/commands/base.rb', line 31

def find(input, opts = {})
  AnyStyle.find(input,
    format: :wapiti,
    layout: opts[:layout],
    crop: opts[:crop].nil? ? nil : opts[:crop].map(&:to_i))
end

#format(dataset, fmt) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/anystyle/cli/commands/base.rb', line 46

def format(dataset, fmt)
  case fmt
  when 'bib'
    AnyStyle.parser.format_bibtex(dataset).to_s
  when 'csl'
    JSON.pretty_generate AnyStyle.parser.format_csl(dataset)
  when 'json'
    JSON.pretty_generate AnyStyle.parser.format_hash(dataset)
  when 'ref', 'txt'
    dataset.to_txt
  when 'xml'
    dataset.to_xml(indent: 2).to_s
  else
    raise ArgumentError, "format not supported: #{fmt}"
  end
end

#overwrite?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/anystyle/cli/commands/base.rb', line 23

def overwrite?
  !!options[:overwrite]
end

#parse(input) ⇒ Object



42
43
44
# File 'lib/anystyle/cli/commands/base.rb', line 42

def parse(input)
  AnyStyle.parse(input, format: :wapiti)
end

#parse_file(file) ⇒ Object



38
39
40
# File 'lib/anystyle/cli/commands/base.rb', line 38

def parse_file(file)
  parse(Wapiti::Dataset.open(file))
end

#report(error, file) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/anystyle/cli/commands/base.rb', line 98

def report(error, file)
  STDERR.puts "Error processing `#{file}'"
  STDERR.puts "  #{error.message}"
  STDERR.puts "  #{error.backtrace[0]}"
  STDERR.puts "  #{error.backtrace[1]}"
  STDERR.puts "  ..."
end

#run(args, params) ⇒ Object

Raises:

  • (NotImplementedYet)


11
12
13
# File 'lib/anystyle/cli/commands/base.rb', line 11

def run(args, params)
  raise NotImplementedYet
end

#say(*args) ⇒ Object



94
95
96
# File 'lib/anystyle/cli/commands/base.rb', line 94

def say(*args)
  STDERR.puts(*args) if verbose?
end

#set_output_folder(path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/anystyle/cli/commands/base.rb', line 76

def set_output_folder(path)
  case path
  when nil, '-'
    options[:stdout] = true
  else
    @output_folder = Pathname.new(path).expand_path
  end
ensure
  unless @output_folder.nil?
    if @output_folder.exist?
      raise ArgumentError,
        "not a directory: #{path}" unless @output_folder.directory?
    else
      @output_folder.mkdir
    end
  end
end

#stdout?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/anystyle/cli/commands/base.rb', line 19

def stdout?
  !!options[:stdout]
end

#transpose(path, base_path) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/anystyle/cli/commands/base.rb', line 68

def transpose(path, base_path)
  if output_folder.nil?
    path
  else
    output_folder.join(path.relative_path_from(base_path))
  end
end

#verbose?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/anystyle/cli/commands/base.rb', line 15

def verbose?
  !!options[:verbose]
end

#walk(input) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/anystyle/cli/commands/base.rb', line 106

def walk(input)
  path = Pathname(input).expand_path
  raise ArgumentError, "path does not exist: #{input}" unless path.exist?

  if path.directory?
    path.each_child do |file|
      begin
        yield file, path unless file.directory?
      rescue => e
        report e, file.relative_path_from(path)
      end
    end
  else
    begin
      yield path, path.dirname
    rescue => e
      report e, path.basename
    end
  end
end

#write(content, path, base_path) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/anystyle/cli/commands/base.rb', line 127

def write(content, path, base_path)
  if stdout?
    STDOUT.puts(content)
  else
    path = transpose(path, base_path)
    if !overwrite? && path.exist?
      raise RuntimeError,
        "file exists, use --overwrite to force saving: #{path}"
    end
    File.write path, content
  end
end