aboutsummaryrefslogtreecommitdiff
path: root/bin/gen-projects.rb
blob: c8983bc009323963472d7411ddd46ec54a5ff116 (plain)
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
#!/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: "%<name>s"
slug: "%<slug>s"
active: %<active>s
repo: "%<repo>s"
text: "%<text>s"
---
%<text>s
'.strip,

  # go modules
  go: '
---
title: "%<name>s"
slug: "%<slug>s"
active: %<active>s
repo: "%<repo>s"
text: "%<text>s"
go_import: "pablotron.org/%<slug>s git %<repo>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'

  # 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 }