quran-json/bin/json/insert-chapter-metadata

51 lines
1.4 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
##
# This script inserts metadata about each chapter in
# The Qur'an. The metadata is inserted into the JSON
# for each chapter (eg src/json/en/1.json, src/json/en/2.json,
# src/json/en/3.json, ...).
#
# The metadata is an object that's inserted as the first element
# in an array that otherwise contains the contents of a chapter.
##
# Set process name - primarily for the "ps" command
Process.setproctitle("quran-pull (insert-chapter-metadata)")
##
# Dependencies
require "bundler/setup"
require "json"
require "paint"
require_relative "../../binlib/io/line"
##
# Utils
def parse_json(path)= JSON.parse(File.read(path))
##
# Configuration variables
lc_glob = ARGV[0] || "*"
base_dir = File.join("src", "json")
chapters = parse_json(File.join(base_dir, "chapter-metadata.json"))
dirs = Dir.glob(File.join(base_dir, lc_glob)).select { File.directory?(_1) }
line = IO::Line.new($stdout)
print "\n", " " * 3, Paint["Add metadata", :bold], "\n\n"
print "Locale", " " * 3
print "Location", "\n"
dirs.each do |dir|
1.upto(114) do |number|
ch = chapters[number - 1]
lc = File.basename(dir)
fp = File.join(dir, "#{number}.json")
line.rewind.print File.basename(dir), " " * 7, File.join(lc, "#{number}.json").center(9)
contents = parse_json(fp)
contents.unshift(ch)
File.write(fp, JSON.pretty_generate(contents))
sleep 0.01
end
puts
end