Created
December 5, 2013 05:28
-
-
Save brentsimmons/7800568 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
#!/usr/bin/env ruby | |
# PBS 4 Dec. 2013 | |
# Renders HTML for all the .markdown files in the current directory. | |
# Gives each file a .html suffix. | |
# Saves them in a subfolder called HTML. | |
require 'rdiscount' | |
require 'find' | |
require 'fileutils' | |
def markdown_file?(f) | |
filename = File.basename(f) | |
if filename[0] == ?. then return false end | |
if FileTest.directory?(f) then return false end | |
extension = File.extname(filename) | |
if extension == '.markdown' then return true end | |
return false | |
end | |
def text_of_file(f) | |
file = File.open(f, 'r') | |
s = file.read | |
file.close | |
return s | |
end | |
def filename_with_suffix_dropped(filename) | |
filename_array = filename.split('.') | |
filename_array.delete_at(filename_array.length - 1) | |
filename = filename_array.join('.') | |
return filename | |
end | |
def filename_with_suffix_changed(filename, new_suffix) | |
filename = filename_with_suffix_dropped(filename) | |
return filename + new_suffix | |
end | |
def write_file(s, f) | |
FileUtils.mkdir_p(File.dirname(f)) | |
f = File.open(f, 'w') | |
f.puts(s) | |
f.close | |
end | |
def html_text_for_file(f) | |
markdown_text = text_of_file(f) | |
html_text = RDiscount.new(markdown_text).to_html | |
filename = File.basename(f) | |
title = filename_with_suffix_dropped(filename) | |
style = "<style>body {margin-top: 3em;}\n.content {width: 33em; margin-left: 7em; margin-right: auto}</style>\n" | |
file_text = "<html>\n<head><title>#{title}</title>#{style}</head></body><div class=content>#{html_text}</div></body></html>" | |
return file_text | |
end | |
def generate_and_write_html(f) | |
filename = File.basename(f) | |
html_filename = filename_with_suffix_changed(filename, '.html') | |
folder = File.dirname(f) | |
html_folder = folder + "/html/" | |
html_filepath = html_folder + html_filename | |
html_text = html_text_for_file(f) | |
write_file(html_text, html_filepath) | |
end | |
folder = Dir.pwd | |
Find.find(folder) do |f| | |
filename = File.basename(f) | |
if markdown_file?(f) | |
print(filename + "\n") | |
generate_and_write_html(f) | |
end | |
end |
Hi Brent!
Nice example of using rdiscount; however, MultiMarkdown has a batch mode -b
that'll do this, as well as a compatibility mode -c
if you want strict vanilla markdown parsing.
multimarkdown -b -c *.markdown
Additionally, if you start your md file with:
css: mystyles.css
A full HTML file will be created (rather than a snippet, also done with -f
) and the referenced stylesheet used.
(not that writing your own script isn't fun of course! ;)
But since we all love Ruby, here’s a more compact and idiomatic version:
https://gist.github.com/sklppr/7803789
You may want to look at how Jekyll works. It has a simple command that monitors a directory and generates html when a file changes.
https://github.com/mojombo/jekyll/blob/master/lib/jekyll/commands/build.rb
jekyll build --watch
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you thought about using Rake for this? Seems like it would be a natural fit for simplifying this.