/
build
executable file
·120 lines (101 loc) · 3.2 KB
/
build
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
#!/usr/bin/env ruby
require "erb"
require "fileutils"
require "set"
require "yaml"
require_relative "lib/frontmatter"
require_relative "lib/deep_transform_keys"
require_relative "lib/html_preview"
require_relative "lib/markdown"
# Read config
config = File.read("config.yml")
config = YAML.load(config).deep_transform_keys(&:to_sym)
# Create dist folder
dist_path = config.fetch("dist_path", "dist")
FileUtils.mkdir(dist_path) unless Dir.exist?(dist_path)
FileUtils.rm_r(Dir.glob("#{dist_path}/**/*.{html,css,js,woff,ttf,eot,md}"))
# TODO: Remove empty folders in dist
# Defaults
variable_defaults = {
node_modules: File.join(Dir.getwd, "node_modules"),
separator: '^\n==\n',
separator_vertical: '^\n--\n',
separator_notes: '^Notas?:',
theme: config[:theme],
preview_tags: config[:preview_tags]
}
template = File.read("template.html.erb")
js_template = File.read("template.js.erb")
# Collect all md files to build
paths = config[:paths].map do |path_object|
path_object.merge({
files: Dir["#{path_object[:path]}/**/*.md"]
})
end
# Collect all used themes
themes = Set.new
# Add default variables and front matter config to each file
# Same structure as in config.yml plus a +files+ key.
# files:
# path_to_file: { config_1: true, config_2: 'something else' }
# another_path: { config_1: false, config_2: 'something' }
paths = paths.map do |path_object|
path_object[:files] = path_object[:files].to_h do |file|
frontmatter = Frontmatter.new(file)
variables = variable_defaults.merge(frontmatter.config)
variables[:theme] = path_object[:theme] if path_object[:theme] && frontmatter.config[:theme].nil?
variables[:path] = "/#{file}"
themes.add(variables[:theme])
[
file,
variables
]
end
path_object
end
# Create JS files
erb = ERB.new(js_template)
themes.each do |theme|
next if theme.nil?
puts "> Creating #{theme} JS file"
variables = variable_defaults.merge({theme: theme})
content = erb.result_with_hash(variables)
File.open("#{dist_path}/#{theme}.js", "w") { |file| file.write(content) }
system("npx esbuild --bundle "\
"#{dist_path}/#{theme}.js "\
"--bundle "\
"--outfile=#{dist_path}/#{theme}.js "\
"--allow-overwrite "\
"--loader:.html=text "\
"--loader:.woff=file "\
"--loader:.eot=file "\
"--loader:.ttf=file")
end
# Copy assets
puts "> Copying assets"
FileUtils.cp_r("assets", dist_path)
paths.each do |data_path|
path = data_path[:path]
puts "\n\n> Create #{path}"
FileUtils.mkdir_p("#{dist_path}/#{path}")
data_path[:files].each do |file, config|
if config.fetch(:published, false)
puts "\t> #{file}"
erb = ERB.new(template)
content = erb.result_with_hash(config)
# Create HTML file
File.open("#{dist_path}/#{path}/#{File.basename(file, ".md")}.html", "w") do |f|
f.write(content)
end
# Create Markdown companion file
md = Markdown.new(source: file, config: config)
File.open("#{dist_path}/#{file}", "w") do |f|
f.write(md.render)
end
# Remove frontmatter from Markdown file
Frontmatter.new("#{dist_path}/#{file}").delete
else
puts "\t> #{file} (skipped)"
end
end
end