-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.rb
executable file
·131 lines (92 loc) · 3.11 KB
/
generate.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
#!/usr/bin/env ruby
require "front_matter_parser"
require "tempfile"
require "tilt"
require "yaml"
LINK_REGEXP = /\[\[\s*(\S+)\s*\]\]/
MARKDOWN_TEMPLATE = """---
title: %s
---
# %s"""
MARKDOWN_LOG_TEMPLATE ="""---
title: %s
---
# There for Sunrise
## %s
%s
%s %s
"""
SCRIPT_DIR = File.expand_path(File.dirname(__FILE__))
def generate_required_markdown_files_from_magic_links_in_markdown_file(markdown_file)
markdown_file_contents = File.read(markdown_file)
markdown_file_contents.to_enum(:scan, LINK_REGEXP).map { Regexp.last_match }.each do |markdown_file_link|
markdown_title = markdown_file_link[1]
markdown_file_to_create = "#{markdown_title}.md"
unless File.exist? markdown_file_to_create
File.open(markdown_file_to_create, 'w') do |f|
f.write(MARKDOWN_TEMPLATE % [markdown_title, markdown_title])
end
end
end
end
def generate_html_file_from_markdown(markdown_file)
markdown_file_contents = File.read(markdown_file).gsub(LINK_REGEXP, '[\1](\1.html)')
html_file = markdown_file.gsub(".md", ".html" )
parsed = FrontMatterParser::Parser.new(:md).call(markdown_file_contents)
layout = Tilt['erb'].new("layout.html.erb")
data = Tilt['markdown'].new { parsed.content }
html_file_contents = layout.render(self, parsed.front_matter) { data.render }
File.open(html_file, "w") do |f|
f.write(html_file_contents)
end
end
def generate_content(file)
f = File.read(file)
return "" if f.empty?
title = File.basename(file).gsub('.md', '').gsub('log/', '')
content = ""
content << "### #{title}\n\n"
content << f
content << "<br/><br/>\n"
end
def generate_log
log_file_slices = Dir.glob("#{SCRIPT_DIR}/log/*.md")
.select { |file| file =~ /log\/\d{4}-\d{2}-\d{2}\.md$/ }
.sort
.reverse
.each_slice(3)
log_file_slices.each_with_index do |slice, index|
log_file = "#{SCRIPT_DIR}/log/#{index}.md"
File.open(log_file, "w") do |f|
slice_content = ""
slice.each do |l|
slice_content << generate_content(l)
end
is_first_iteration = index == 0
is_last_iteration = (index == log_file_slices.to_a.length - 1)
previous_link = !is_last_iteration ? "[Previous](/log/#{index+1}.html)" : ""
next_link = !is_first_iteration ? "[Next](/log/#{index-1}.html)" : ""
STDERR.puts "Generating #{log_file}..."
f.write(MARKDOWN_LOG_TEMPLATE % ["Log", "Log", slice_content, previous_link, next_link])
end
generate_html_file_from_markdown log_file
end
end
def markdown_files
Dir.glob("*.md").reject { |x| x[/(README.md|log)/] }
end
def html_files
Dir.glob("*.html")
end
STDERR.puts "Generating site..."
markdown_files.each do |markdown_file|
generate_required_markdown_files_from_magic_links_in_markdown_file markdown_file
end
markdown_files.each do |markdown_file|
generate_html_file_from_markdown markdown_file
end
generate_log
html_files.each do |html_file|
end
File.delete("#{SCRIPT_DIR}/log/index.html") if File.exist?("#{SCRIPT_DIR}/log/index.html")
File.symlink("#{SCRIPT_DIR}/log/0.html", "#{SCRIPT_DIR}/log/index.html")