#!/usr/bin/env ruby # frozen_string_literal: true # # gen-projects.rb: generate front matter from list of projects in # data/projects.yaml. # # Note: running this will blindly overwrite the contents of # content/projects/$PROJECT.md! # # load libraries require 'yaml' # build absolute paths to source projects.yaml and absolute path to # content/projects directory YAML_PATH = File.join(__dir__, '..', 'data', 'projects.yaml') PROJECTS_DIR = File.join(__dir__, '..', 'content', 'projects') # project template TMPLS = { # default template default: ' --- title: "%s" slug: "%s" active: %s repo: "%s" text: "%s" --- %s '.strip, # go modules go: ' --- title: "%s" slug: "%s" active: %s repo: "%s" text: "%s" go_import: "pablotron.org/%s git %s" --- %s '.strip, } # # write front matter for project to projects directory # def write_project(row) # build absolute path to destination file path = File.expand_path(File.join(PROJECTS_DIR, row['slug'])) + '.md' # get template tmpl = row['type'] ? TMPLS[row['type'].intern] : TMPLS[:default] # generate and write body File.write(path, tmpl % { name: row['name'], slug: row['slug'], active: !row['old'], repo: row['repo'], text: row['text'], }) end # write projects in green threads, then join threads YAML.load(File.read(YAML_PATH)).map do |row| Thread.new(row) { |row| write_project(row) } end.each { |th| th.join }