File: helpers.rb

package info (click to toggle)
ruby-jekyll-data 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 420 kB
  • sloc: ruby: 909; sh: 95; makefile: 10
file content (138 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require "fileutils"
require "jekyll"
require "open3"
require "time"
require "safe_yaml/load"

class Paths
  SOURCE_DIR = Pathname.new(File.expand_path("../..", __dir__))
  def self.test_dir; source_dir.join("tmp", "jekyll-data"); end

  def self.output_file; test_dir.join("jekyll_output.txt"); end

  def self.status_file; test_dir.join("jekyll_status.txt"); end

  def self.source_dir; SOURCE_DIR; end
end

def file_content_from_hash(input_hash)
  matter_hash = input_hash.reject { |k, _v| k == "content" }
  matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join
  matter.chomp!
  content = if input_hash["input"] && input_hash["filter"]
              "{{ #{input_hash["input"]} | #{input_hash["filter"]} }}"
            else
              input_hash["content"]
            end

  <<~RESULT
    ---
    #{matter}
    ---

    #{content}
  RESULT
end

def source_dir(*files)
  Paths.test_dir(*files)
end

def all_steps_to_path(path)
  source = source_dir
  dest   = Pathname.new(path).expand_path
  paths  = []

  dest.ascend do |f|
    break if f == source

    paths.unshift f.to_s
  end

  paths
end

def jekyll_run_output
  Paths.output_file.read if Paths.output_file.file?
end

def jekyll_run_status
  Paths.status_file.read if Paths.status_file.file?
end

def run_bundle(args)
  run_in_shell("bundle", *args.strip.split(" "))
end

def run_rubygem(args)
  run_in_shell("gem", *args.strip.split(" "))
end

def run_jekyll(args)
  args = args.strip.split(" ") # Shellwords?
  process = run_in_shell("ruby", Paths.jekyll_bin.to_s, *args, "--trace")
  process.exitstatus.zero?
end

# rubocop:disable Metrics/AbcSize
def run_in_shell(*args)
  i, o, e, p = Open3.popen3(*args)
  out = o.read.strip
  err = e.read.strip

  [i, o, e].each(&:close)

  File.write(Paths.status_file, p.value.exitstatus)
  File.open(Paths.output_file, "wb") do |f|
    f.print "$ "
    f.puts args.join(" ")
    f.puts out
    f.puts err
    f.puts "EXIT STATUS: #{p.value.exitstatus}"
  end

  p.value
end
# rubocop:enable Metrics/AbcSize

def slug(title = nil)
  if !title
    Time.now.strftime("%s%9N") # nanoseconds since the Epoch
  else title.downcase.gsub(%r![^\w]!, " ").strip.gsub(%r!\s+!, "-")
  end
end

def location(folder, direction)
  if folder
    before = folder if direction == "in"
    after  = folder if direction == "under"
  end

  [
    before || ".",
     after || ".",
  ]
end

def file_contents(path)
  Pathname.new(path).read
end

def seconds_agnostic_datetime(datetime = Time.now)
  date, time, zone = datetime.to_s.split(" ")
  time = seconds_agnostic_time(time)

  [
    Regexp.escape(date),
    "#{time}:\\d{2}",
    Regexp.escape(zone),
  ].join("\\ ")
end

def seconds_agnostic_time(time)
  time = time.strftime("%H:%M:%S") if time.is_a?(Time)
  hour, minutes, = time.split(":")
  "#{hour}:#{minutes}"
end