Last active
August 29, 2015 14:10
-
-
Save garybernhardt/51148cebc131daff19de to your computer and use it in GitHub Desktop.
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
module RenderToHTML | |
def self.book | |
[title, css, body].join("\n") | |
end | |
private | |
def self.title | |
commit_hash = `git log -1 --pretty="format:%H"` | |
%{ | |
<div class="title"> | |
<h1>#{TITLE}</h1> | |
<p>by Gary Bernhardt</p> | |
<p>partial build on commit #{commit_hash}</p> | |
</div> | |
<div class="do_not_distribute"> | |
<h1>DO NOT DISTRIBUTE</h1> | |
</div> | |
} | |
end | |
def self.css | |
%{ | |
<style> | |
body { | |
font-family: Helvetica; | |
width: 550px; | |
margin: auto; | |
} | |
.do_not_distribute { margin: 16em 0; } | |
.title { margin: 4em 0 8em 0; } | |
.title, h1, h2, h3 { text-align: center; } | |
h2 { margin-top: 8em; } | |
h3 { margin-top: 8em; } | |
p, li { line-height: 1.5em; } | |
figure, img { margin: 0 0 1em 0; padding: 0; } | |
</style> | |
} | |
end | |
def self.body | |
# Only render to markdown once we have all of the body stuff (sections and | |
# chapters). | |
RenderToMarkdown.sections.map do |section| | |
RenderExternalFormats.markdown(section) | |
end | |
end | |
end | |
class RenderToMarkdown | |
def self.sections | |
BOOK.sections.map { |section| section(section) }.flatten | |
end | |
private | |
def self.section(section) | |
[ | |
"## Part #{section.number}. #{section.name}" | |
] + chapters(section.chapters) | |
end | |
def self.chapters(chapters) | |
chapters.map do |chapter| | |
[ | |
"### #{chapter.number}. #{chapter.title}", | |
chapter(chapter) | |
] | |
end | |
end | |
def self.chapter(chapter) | |
RenderExternalFormats.erb(chapter.content) | |
end | |
end | |
class RenderExternalFormats | |
def self.erb(markdown) | |
context = Context.new | |
rendered = ERB.new(markdown).result(context.binding) | |
end | |
def self.markdown(markdown) | |
Open3.popen2("multimarkdown") do |stdin, stdout| | |
stdin.write(markdown) | |
stdin.close | |
stdout.read | |
end | |
end | |
end |
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
# Here's the whole RenderToMarkdown class inlined into the RenderToHTML.book method: | |
def self.book | |
[ | |
title, | |
css, | |
BOOK.sections.map do |section| | |
[ | |
"## Part #{section.number}. #{section.name}", | |
section.chapters.map do |chapter| | |
[ | |
"### #{chapter.number}. #{chapter.title}", | |
RenderExternalFormats.erb(chapter.content), | |
] | |
end, | |
] | |
end.flatten.map do |markdown| | |
RenderExternalFormats.markdown(markdown) | |
end | |
].flatten.join("\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment