You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
blogtool/src/blogtool.cr

62 lines
1.9 KiB

require "markd"
require "dir"
require "file"
require "path"
begin
Dir.mkdir_p Path["dist/static"]
rescue
if !Dir.exists? Path["dist/static"]
puts "Could not create required folders. Exiting."
exit 1
end
end
Base = begin
if File.file? "base.html"
File.read "base.html"
elsif File.file? Path[ENV["HOME"]+"/.config/blogtool/base.html"]
File.read Path[ENV["HOME"]+"/.config/blogtool/base.html"]
elsif File.file? Path["/etc/blogtool/base.html"]
File.read Path["/etc/blogtool/base.html"]
else
puts "Can't find base.html"
exit 1
end
end
BaseCSS = begin
if File.file? Path[ENV["HOME"]+"/.config/blogtool/styles.css"]
File.read Path[ENV["HOME"]+"/.config/blogtool/styles.css"]
elsif File.file? Path["/etc/blogtool/styles.css"]
File.read Path["/etc/blogtool/styles.css"]
else
""
end
end
def render_file(filename : String | Path)
Base+Markd.to_html(File.read(filename), Markd::Options.new(smart: true, safe: false))+"</body></html>"
end
index = Base
content = ""
Dir.entries("articles").select{ |i| i != "." && i != ".." }.each do |article|
if article[article.size-3, article.size] == ".md"
File.write Path.new("dist", article[0,article.size-3]+".html"), render_file("articles/"+article)
content += "<h3><a href=\"/"+article[0,article.size-3]+".html\">"+article[0,article.size-3].gsub("-", " ")+"</a></h3>\n"
else
File.write Path.new("dist", article+".html"), render_file("articles/"+article)
content += "<h3><a href=\"/"+article+".html\">"+article.gsub("-", " ")+"</a></h3>\n"
end
end
index.gsub("## CONTENT\n") { content }
File.write Path["dist/index.html"], index
Dir.entries("assets").select{ |i| i != "." && i != ".." }.each do |asset|
File.copy Path.new("assets", asset), Path.new("dist", "static", asset)
end
File.write Path.new("dist", "static", "styles.css"), BaseCSS+File.read("styles.css")
File.write Path.new("dist", "about.html"), render_file("about.md")