jekyll-filters.rb 2.8 KB
Newer Older
C
codecalm 已提交
1
require 'date'
C
codecalm 已提交
2 3
require 'htmlbeautifier'

C
codecalm 已提交
4

C
chomik 已提交
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
module Jekyll
  module JekyllFilter
    def to_pretty_time(value)
      a = (Time.now - value).to_i

      case a
      when 0 then
        'just now'
      when 1 then
        'a second ago'
      when 2..59 then
        a.to_s + ' seconds ago'
      when 60..119 then
        'a minute ago' #120 = 2 minutes
      when 120..3540 then
        (a / 60).to_i.to_s + ' minutes ago'
      when 3541..7100 then
        'an hour ago' # 3600 = 1 hour
      when 7101..82800 then
        ((a + 99) / 3600).to_i.to_s + ' hours ago'
      when 82801..172000 then
        'a day ago' # 86400 = 1 day
      when 172001..518400 then
        ((a + 800) / (60 * 60 * 24)).to_i.to_s + ' days ago'
      when 518400..1036800 then
        'a week ago'
      else
        ((a + 180000) / (60 * 60 * 24 * 7)).to_i.to_s + ' weeks ago'
      end
    end

    def format_number(value)
      value.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
    end

    def first_letter(value)
      value.to_s[0]
    end

    def first_letters(value)
      value.to_s.split.map(&:chr).join
    end

    def divide(value, number)
      value.to_i * 1.0 / number
    end

    def number_color(value)
      value = value.to_i

      if value >= 75
        'green'
C
codecalm 已提交
57
      elsif value >= 30
C
chomik 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        'yellow'
      else
        'red'
      end
    end

    $random_id_num = 0

    def random_id(value)
      $random_id_num += 1
    end

    def svg_icon(value, class_name)
      value = value.gsub(/<svg /, '<svg class="icon ' + class_name.to_s + '" ')
    end
C
chomik 已提交
73 74


75
    def replace_regex(input, reg_str, repl_str)
C
codecalm 已提交
76
      re = Regexp.new(reg_str.to_s, Regexp::MULTILINE)
C
chomik 已提交
77

78
      input.gsub re, repl_str
C
chomik 已提交
79
    end
C
codecalm 已提交
80 81 82 83 84

    def hex_to_rgb(color)
      r, g, b = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/).captures
      "rgb(#{r.hex}, #{g.hex}, #{b.hex})"
    end
C
codecalm 已提交
85

C
codecalm 已提交
86 87 88 89 90 91 92 93
    def concat_objects(object, object2)
      if object and object2 and object.is_a?(Hash) and object2.is_a?(Hash)
        return object.merge(object2)
      end

      object
    end

94 95 96 97 98 99 100
    # def tabler_color(color, variation = false)
    #   if variation
    #     color = color + '-' + variation.to_s
    #   end
    #
    #   Jekyll.sites.first.data['colors'][color]
    # end
C
codecalm 已提交
101

C
codecalm 已提交
102 103 104 105 106 107 108 109 110 111 112 113
    def seconds_to_minutes(seconds)
      seconds = seconds.to_i.round

      minutes = (seconds / 60).round
      seconds = seconds - (minutes * 60)

      minutes.to_s.rjust(2, '0') + ":" + seconds.to_s.rjust(2, '0')
    end

    def miliseconds_to_minutes(miliseconds)
      seconds_to_minutes(miliseconds.to_i / 1000)
    end
C
codecalm 已提交
114 115 116 117

    def timestamp_to_date(timestamp)
      DateTime.strptime(timestamp.to_s, '%s').strftime('%F')
    end
C
codecalm 已提交
118 119 120 121

    def htmlbeautifier(output)
      HtmlBeautifier.beautify output
    end
C
chomik 已提交
122 123 124 125
  end
end

Liquid::Template.register_filter(Jekyll::JekyllFilter)