Created
January 23, 2017 13:48
-
-
Save hmadison/c82f262a9ef4a1b75974d9e4e252aeec to your computer and use it in GitHub Desktop.
Static Blog Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'time' | |
require 'erb' | |
require 'org-ruby' | |
require 'fileutils' | |
source_files = Rake::FileList.new('posts/*.org') | |
task default: [*source_files.pathmap('docs/%n.html'), :build_index, :copy_css] | |
rule '.html' do |t| | |
input_file = t.name.pathmap('posts/%n.org') | |
post = post_content(input_file) | |
render_to_file(t.name, 'assets/post.html.erb', binding) | |
end | |
task :build_index do | |
posts = source_files | |
.map { |i| post_content(i) } | |
.sort { |a, b| b[:date] <=> a[:date] } | |
render_to_file('docs/index.html', 'assets/index.html.erb', binding) | |
end | |
task :copy_css do | |
FileUtils.cp('assets/style.css', 'docs/style.css') | |
end | |
def org_parser(input_file) | |
file = File.read(input_file) | |
Orgmode::Parser.new(file, markup_file: 'assets/html.tags.yml') | |
end | |
def render_to_file(outfile, template, b) | |
template = File.read(template) | |
renderer = ERB.new(template) | |
output = renderer.result(b) | |
File.write(outfile, output) | |
end | |
def post_content(input_file) | |
parser = org_parser(input_file) | |
date = Time.parse(parser.in_buffer_settings['DATE']) | |
title = parser.in_buffer_settings['TITLE'] | |
path = input_file.pathmap('%n.html') | |
{ title: title, date: date, path: path, formatted_date: date.strftime('%B %d, %Y'), content: parser.to_html } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment