-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssg.rb
165 lines (136 loc) · 4.99 KB
/
ssg.rb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'
require 'fileutils'
require 'erb'
require 'toml'
require 'time'
require_relative 'renderer'
if ARGV.length < 2 then
puts "Usage: ruby compile.rb CONTENT_DIR PUBLISH_DIR"
puts
puts "CONTENT_DIR: Relative to the directory where this script is located"
puts "PUBLISH_DIR: Absolute path of the directory where the site will be published"
exit
end
SITE_ROOT = ''
SRC_DIR = File.expand_path('../', __FILE__)
CONTENT_DIR = File.join(SRC_DIR, ARGV[0])
PUBLISH_DIR = ARGV[1]
class Category
attr_reader :name, :pages
CATEGORY_TEMPLATE = File.read(File.join(SRC_DIR, 'templates/category.erb'))
def initialize(name, pages, site_root)
@name = name
@pages = pages
@site_root = site_root
end
def publish(categories)
FileUtils.mkdir_p(File.join(PUBLISH_DIR, "categories/#{@name}/"))
File.write(File.join(PUBLISH_DIR, "categories/#{@name}/index.html"), ERB.new(CATEGORY_TEMPLATE).result(binding))
end
end
class Tag
TAGS_TEMPLATE = File.read(File.join(SRC_DIR, 'templates/tags.erb'))
def initialize(name, pages, site_root)
@name = name
@pages = pages
@site_root = site_root
end
def publish(categories)
FileUtils.mkdir_p(File.join(PUBLISH_DIR, "tags/#{@name}"))
File.write(File.join(PUBLISH_DIR, "tags/#{@name}/index.html"), ERB.new(TAGS_TEMPLATE).result(binding))
end
end
class Page
attr_reader :title, :description, :category, :date, :key, :path, :href, :tags, :name
PAGE_TEMPLATE = File.read(File.join(SRC_DIR, 'templates/page.erb'))
def initialize(name, lines, site_root)
@name = name
@lines = lines
@site_root = site_root
@markdown = Redcarpet::Markdown.new(HTML.new(filter_html: true, hard_wrap: true), fenced_code_blocks: true, highlight: true)
build
end
def publish(categories)
FileUtils.mkdir_p(@directory)
@content = @markdown.render(@md_content)
File.write(@path, ERB.new(PAGE_TEMPLATE).result(binding))
@images.each { |path| FileUtils.cp(path, @directory) }
end
private
def build
fm_indices = frontmatter_indices
@frontmatter = parse_frontmatter(select_content(@lines, fm_indices[0] + 1, fm_indices[1] - 1))
@directory = File.join(PUBLISH_DIR, "categories/#{@frontmatter.fetch('category', 'Uncategorized')}/#{@name}")
@path = "#{@directory}/index.html"
@href = @directory.split('/').last(3).join('/')
@md_content = select_content(@lines, fm_indices[1] + 1).join
@title = @frontmatter['title']
@description = @frontmatter['description']
@category = @frontmatter.fetch('category', 'Uncategorized')
@key = Time.parse(@frontmatter['date'])
@date = @key.strftime('%A, %e %B %Y')
@tags = @frontmatter.fetch('tags', []).map(&:strip).map
@images = Dir.glob([
"#{CONTENT_DIR}/#{@name}/*.png",
"#{CONTENT_DIR}/#{@name}/*.jpg"
])
end
def frontmatter_indices
([email protected]).find_all { |i| @lines[i].start_with? '+++' }.sort
end
def select_content(content, start, finish = nil)
finish = content.length - 1 if finish.nil?
content.select.each_with_index { |_, i| i >= start and i <= finish }
end
def parse_frontmatter(fm_content)
TOML.load(fm_content.join)
end
end
class Site
INDEX_TEMPLATE = File.read(File.join(SRC_DIR, 'templates/index.erb'))
def initialize(content_dir, site_root)
@content_dir = content_dir
@site_root = site_root
build
end
def publish
@categories.each { |category| category.publish(@categories.map { |category| category.name }) }
@tags.each { |tag| tag.publish(@categories.map { |category| category.name }) }
@pages.each { |page| page.publish(@categories.map { |category| category.name }) }
File.write(File.join(PUBLISH_DIR, '/index.html'), ERB.new(INDEX_TEMPLATE).result(binding))
end
private
def build
md_files = Dir.glob("#{@content_dir}/*/*.md")
category_to_pages = {}
tag_to_pages = {}
all_pages = []
md_files.each do |path|
lines = File.readlines(path)
page = Page.new(path.split('/').reverse[1], lines, @site_root)
category_to_pages[page.category] = category_to_pages.fetch(page.category, []).append(page).sort_by { |page| page.key }.reverse
page.tags.each do |tag|
tag_to_pages[tag] = tag_to_pages.fetch(tag, []).append(page).sort_by { |page| page.key }.reverse
end
all_pages.append(page)
end
all_categories = []
category_to_pages.each do |category, pages|
all_categories.append(Category.new(category, pages, @site_root))
end
all_tags = []
tag_to_pages.each do |tag, pages|
all_tags.append(Tag.new(tag, pages, @site_root))
end
@categories = all_categories
@tags = all_tags
@pages = all_pages.sort_by { |page| page.key }.reverse
end
end
start = Time.now
Site.new(CONTENT_DIR, SITE_ROOT).publish
FileUtils.mkdir_p(File.join(PUBLISH_DIR, 'static'))
Dir.glob(File.join(SRC_DIR, 'static/*')).each { |file| FileUtils.cp(file, File.join(PUBLISH_DIR, 'static/')) }
puts "Done in #{(Time.now - start).to_i}s"