Class: Work::Md::Commands::Plast

Inherits:
Object
  • Object
show all
Defined in:
lib/work/md/commands/plast.rb

Class Method Summary collapse

Class Method Details

.execute(argv = []) ⇒ Object



8
9
10
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
# File 'lib/work/md/commands/plast.rb', line 8

def execute(argv = [])
  is_numeric = ->(str) {
    str == "#{str.to_f}" || str == "#{str.to_i}"
  }

  if is_numeric.(argv.first)
    last_n = argv.first.to_i
  else
    Work::Md::Cli.help(
      ::TTY::Box.frame(
        "message: 'plast' command accept only numeric arguments, you give: #{argv.inspect}",
        '',
        'Usage example:',
        '',
        'work-md pl 7       # parse the last 7 days',
        **Work::Md::Cli.error_frame_style
      )
    )
    return
  end

  last_date = Date.today.prev_day
  work_dir = Work::Md::Config.work_dir
  parser = Work::Md::Parser::Engine.new

  (1..last_n).map do
    last_file_name = "#{last_date.strftime('%Y/%m/%d')}.md"
    if ::File.exist?("#{work_dir}/#{last_file_name}")
      parser.add_file("#{work_dir}/#{last_file_name}")
    else
      nil
    end

    last_date = last_date.prev_day
  end

  Work::Md::File.create_and_open_parsed(parser)
rescue Work::Md::Parser::Error => e
  Work::Md::Cli.help(e.message)
rescue StandardError => e
  Work::Md::Cli.help(
    ::TTY::Box.frame(
      "message: Some of verified markdown files may be with an incorrect format",
      '',
      'Usage example:',
      '',
      'work-md pl 7       # parse the last 7 days',
      **Work::Md::Cli.error_frame_style
    )
  )
end