Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Created December 5, 2013 05:28
Show Gist options
  • Save brentsimmons/7800568 to your computer and use it in GitHub Desktop.
Save brentsimmons/7800568 to your computer and use it in GitHub Desktop.
#!/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
@sklppr
Copy link

sklppr commented Dec 5, 2013

But since we all love Ruby, here’s a more compact and idiomatic version:
https://gist.github.com/sklppr/7803789

@mcormier
Copy link

mcormier commented Dec 5, 2013

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

http://jekyllrb.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment