diff options
Diffstat (limited to 'bin')
-rw-r--r-- | bin/gen-projects.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/bin/gen-projects.rb b/bin/gen-projects.rb new file mode 100644 index 0000000..ef4c547 --- /dev/null +++ b/bin/gen-projects.rb @@ -0,0 +1,51 @@ +#!/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_TMPL = ' +--- +title: "%<name>s" +slug: "%<slug>s" +active: %<active>s +repo: "%<repo>s" +text: "%<text>s" +--- +%<text>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' + + # generate and write body + File.write(path, PROJECT_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 } |