diff options
Diffstat (limited to 'Rakefile')
-rw-r--r-- | Rakefile | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..38adb95 --- /dev/null +++ b/Rakefile @@ -0,0 +1,130 @@ +%w[json erb] + .each { require _1 } + +def fputs(f, c) + puts "fputs #{f}" + File.write f, c +end +def erb(tmpl, ctx={}) = + ERB.new(File.read(tmpl), trim_mode: "-") + .result_with_hash(ctx) + +module Component + def Component.parse(str) + script = []; style = []; html = [] + cur = html + str.lines.each do |line| + line = line.chomp + if line == "<script>" and cur.object_id == html.object_id + cur = script + elsif line == "<style>" and cur.object_id == html.object_id + cur = style + elsif line =~ /^<\/(style|script)>$/ and cur.object_id != html.object_id + cur = html + else + cur.push line + end + end + {style: style * "\n", + html: html * "\n", + script: script * "\n"} + end + + private + def Component.amalg(list, key) = + list.values.map { _1[key] }.filter { _1 != "" } * "\n" + public + def Component.parse_many(deps) + start = deps.map { [_1.split(".")[0], parse(read(_1, []))] }.to_h + start.merge(style: amalg(start, :style), + html: amalg(start, :html), + script: amalg(start, :script)) + end + + def Component.read(f, deps) + if not f =~ /\.erb/ + File.read f + else + erb f, parse_many(deps) + end + end +end + +DB = "protobowl-2019-10-15-ALL.json" +KEPT_KEYS = %w[category subcategory difficulty + year source tournament round num + question answer] + +$data = nil +def get_data + return $data if $data + $data = + File.read(DB) + .split("\n") + .map! { JSON.parse _1 } + .filter! { _1["type"] == "qb" } +end + +file DB do + sh "wget -N https://github.com/neotenic/database-dumps/raw/refs/heads/master/2019-10-15-ALL.json.xz" + sh "unxz 2019-10-15-ALL.json.xz" + mv "2019-10-15-ALL.json", DB +end + +file "summary.json": [DB] do + data = get_data + + difficulties = data.map { _1["difficulty"] }.uniq + categories = data.map { _1["category"] }.uniq + + fputs "summary.json", + JSON.generate({difficulties:, categories:}) +end.invoke # ensure it's generated now + +$difficulties, $categories = + JSON.parse(File.read "summary.json") + .values_at("difficulties", "categories") + +def dir_from(dif) = "qdb/#{dif.downcase}" +def name_from(dif, cat) = "#{dir_from dif}/#{cat.downcase.gsub(/\s/, "-")}.json" + +$files = [] +$difficulties.each do |dif| + dir = dir_from(dif) + directory dir + $categories.each do |cat| + f = name_from(dif, cat) + $files.push f + file f => [DB, dir] do + d = get_data + .filter { _1["difficulty"] == dif && + _1["category"] == cat } + .map { _1.slice(*KEPT_KEYS) } + fputs f, JSON.generate(d) + end + end +end + +file qdb: [DB, *$files] + +file "deck.html": ["deck.erb.html", "summary.json"] do + fputs "deck.html", erb("deck.erb.html") +end + +file "_.html": ["_.erb.html", + "utils.html", "storage.html", + "card.html", "reviewer.html", + "deck.html", "settings.html", + "ie.html"] do |t| + fputs "_.html", Component.read("_.erb.html", t.prereqs[1..-1]) +end + +task :clean do + ["qdb", "summary.json", "_.html", "deck.html"].map { rm_r _1 } +end + +task default: ["qdb", "_.html"] + +task :publish do + sh "rsync -rutv --delete qdb _.html manifest.json root@ba.ln.ea.cx:/var/www/onpoint/" +end |