-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoq_blog.rb
80 lines (64 loc) · 1.96 KB
/
coq_blog.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
# encoding: utf-8
require 'erb'
require 'redcarpet'
include ERB::Util
class Blog
attr_reader :title, :url, :disqus, :posts
def initialize(title, url, disqus)
@title, @url, @disqus = title, url, disqus
@posts = Dir.glob("posts/*.md").map {|file_name| Post.new(file_name)}
.sort_by {|post| post.date}.reverse
end
def public_posts
@posts.select {|post| not post.wip?}
end
def wip_posts
@posts.select {|post| post.wip?}
end
end
class MarkdownRender < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
def image(link, title, alt_text)
"<img src=\"#{h(link)}\" alt=\"#{h(alt_text)}\" class=\"img-responsive center-block\" /><div class=\"text-center\"><mark>#{h(title)}</mark></div>"
end
end
class Post
attr_reader :name, :date, :html, :url
def initialize(file_name)
if /\A(\d+)-(\d+)-(\d+)\s*(.*)\z/ === File.basename(file_name, ".md") then
@date = Time.local($1, $2, $3)
@name = $4
else
raise "The name #{file_name.inspect} should have the form \"yyyy-mm-dd title.md\"."
end
markdown = File.read(file_name, encoding: "UTF-8")
@html = Redcarpet::Markdown.new(MarkdownRender).render(markdown)
@url = "#{@name.gsub(/[^a-zA-Z0-9]+/, "-").downcase}.html"
end
def date_string
@date.strftime("%B %e, %Y")
end
def wip?
@name.downcase.include?("wip")
end
end
def render_erb(file_name, binding)
ERB.new(File.read(file_name, encoding: "UTF-8")).result(binding)
end
def header(blog, title)
render_erb("templates/header.html.erb", binding)
end
def footer
render_erb("templates/footer.html.erb", binding)
end
blog = Blog.new("Coq blog - Guillaume Claret", "http://coq-blog.clarus.me/", "coqblog")
for page in ["index.html", "wip.html", "rss.xml"] do
File.open("blog/#{page}", "w") do |f|
f << render_erb("#{page}.erb", binding)
end
end
for post in blog.posts do
File.open("blog/#{post.url}", "w") do |f|
f << render_erb("templates/post.html.erb", binding)
end
end