-
Notifications
You must be signed in to change notification settings - Fork 3
/
mahlzeit.rb
executable file
·72 lines (59 loc) · 1.75 KB
/
mahlzeit.rb
1
2
3
4
5
6
7
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'pdf/reader'
require 'date'
require 'json'
require 'optparse'
require './lib/helpers'
@headers = %w[.* Montag Dienstag Mittwoch Donnerstag Freitag]
@ignore = %w[Beilagentausch Zusatzstoffe Allergene Änderungen]
@debug = false
json_file = nil
cleanup = false
lint = false
OptionParser.new do |parser|
parser.banner = "Usage: #{$PROGRAM_NAME} [-d]"
parser.on('-d', '--debug', 'Debug output') { |d| @debug = d }
parser.on('-u', '--update FILE', 'Update <FILE>') { |f| json_file = f }
parser.on('-c', '--cleanup', 'Filter past weeks') { |c| cleanup = c }
parser.on('-l', '--lint', 'Fix some common typos and style issues') { |l| lint = l }
end.parse!
filenames = ARGV.select { |param| param.downcase.include?('.pdf') }
if filenames.empty?
warn 'Give one or more PDF-files to parse as a parameter!'
exit
end
@header_regexp = header_regexp
p @header_regexp if @debug
output = {}
if json_file
begin
File.open json_file do |f|
output = JSON.parse f.read
end
rescue Errno::ENOENT
warn "File '#{json_file}' not found. Will create it."
end
end
filenames.each do |filename|
PDF::Reader.open(filename) do |reader|
reader.pages.each do |page|
date, menu = parse page.text
menu = lint(menu) if lint
# add the week to the output as a hash with ISO 8601 week date, like "2022W40", as a key
output[date.strftime('%GW%V')] = menu
warn "-- new week --#{'-' * 60}" if @debug
end
end
end
# Delete past weeks, if desired
output.delete_if { |w| past? w } if cleanup
# Print json sorted by keys
json_output = JSON.pretty_generate(Hash[*output.sort.flatten])
if json_file
File.open json_file, 'w' do |f|
f.write json_output
end
else
puts json_output
end