aboutsummaryrefslogtreecommitdiff
blob: 038ea3ec388b8a50d3e9e37b3a0fec57e46418b9 (plain)
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
require 'date'

module Gentoo
  class DevawayGenerator < Jekyll::Generator
    DEVAWAY_XML = '_data/devaway.xml'
    # ... @ yyyy/mm/dd HH:MMZ
    DATE_REGEXP = /@ (\d+)\/(\d+)\/(\d+) (\d+):(\d+)Z$/

    def generate(site)
      data = Nokogiri::XML(File.open(DEVAWAY_XML))

      site.data['devaway'] ||= {}
      now = Time.now

      data.xpath('/devaway/dev').each do |dev|
        reason = dev.xpath('./reason/text()').first
        if reason.nil?
          puts dev['nick'] + " has an empty devaway!"
          next
        end

        reason = reason.content

        date_match = DATE_REGEXP.match(reason)
        if not date_match.nil?
          reason = date_match.pre_match.rstrip()
        end
        date = DateTime.parse(dev['timestamp'])
        away_sec = now - date.to_time

        site.data['devaway'][dev['nick']] = {
          'reason' => reason,
          'date' => date,
          'away_days' => away_sec / 24 / 3600,
        }
      end
    end
  end
end