jekyll-filters.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. require 'date'
  2. require 'htmlbeautifier'
  3. module Jekyll
  4. module JekyllFilter
  5. def to_pretty_time(value)
  6. a = (Time.now - value).to_i
  7. case a
  8. when 0 then
  9. 'just now'
  10. when 1 then
  11. 'a second ago'
  12. when 2..59 then
  13. a.to_s + ' seconds ago'
  14. when 60..119 then
  15. 'a minute ago' #120 = 2 minutes
  16. when 120..3540 then
  17. (a / 60).to_i.to_s + ' minutes ago'
  18. when 3541..7100 then
  19. 'an hour ago' # 3600 = 1 hour
  20. when 7101..82800 then
  21. ((a + 99) / 3600).to_i.to_s + ' hours ago'
  22. when 82801..172000 then
  23. 'a day ago' # 86400 = 1 day
  24. when 172001..518400 then
  25. ((a + 800) / (60 * 60 * 24)).to_i.to_s + ' days ago'
  26. when 518400..1036800 then
  27. 'a week ago'
  28. else
  29. ((a + 180000) / (60 * 60 * 24 * 7)).to_i.to_s + ' weeks ago'
  30. end
  31. end
  32. def format_number(value)
  33. value.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
  34. end
  35. def first_letter(value)
  36. value.to_s[0]
  37. end
  38. def first_letters(value)
  39. value.to_s.split.map(&:chr).join
  40. end
  41. def divide(value, number)
  42. value.to_i * 1.0 / number
  43. end
  44. def number_color(value)
  45. value = value.to_i
  46. if value >= 75
  47. 'green'
  48. elsif value >= 30
  49. 'yellow'
  50. else
  51. 'red'
  52. end
  53. end
  54. $random_id_num = 0
  55. def random_id(value)
  56. $random_id_num += 1
  57. end
  58. def svg_icon(value, class_name)
  59. value = value.gsub(/<svg /, '<svg class="icon ' + class_name.to_s + '" ')
  60. end
  61. def replace_regex(input, reg_str, repl_str)
  62. re = Regexp.new(reg_str.to_s, Regexp::MULTILINE)
  63. input.gsub re, repl_str
  64. end
  65. def hex_to_rgb(color)
  66. r, g, b = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/).captures
  67. "rgb(#{r.hex}, #{g.hex}, #{b.hex})"
  68. end
  69. def concat_objects(object, object2)
  70. if object and object2 and object.is_a?(Hash) and object2.is_a?(Hash)
  71. return object.merge(object2)
  72. end
  73. object
  74. end
  75. # def tabler_color(color, variation = false)
  76. # if variation
  77. # color = color + '-' + variation.to_s
  78. # end
  79. #
  80. # Jekyll.sites.first.data['colors'][color]
  81. # end
  82. def seconds_to_minutes(seconds)
  83. seconds = seconds.to_i.round
  84. minutes = (seconds / 60).round
  85. seconds = seconds - (minutes * 60)
  86. minutes.to_s.rjust(2, '0') + ":" + seconds.to_s.rjust(2, '0')
  87. end
  88. def miliseconds_to_minutes(miliseconds)
  89. seconds_to_minutes(miliseconds.to_i / 1000)
  90. end
  91. def timestamp_to_date(timestamp)
  92. DateTime.strptime(timestamp.to_s, '%s').strftime('%F')
  93. end
  94. def htmlbeautifier(output)
  95. HtmlBeautifier.beautify output
  96. end
  97. def hex_to_rgb(hex)
  98. hex.match(/^#(..)(..)(..)$/).captures.map(&:hex)
  99. end
  100. end
  101. end
  102. Liquid::Template.register_filter(Jekyll::JekyllFilter)