Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/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 then return false end
extension = File.extname(filename)
if extension == '.markdown' then return true end
return false
def text_of_file(f)
file =, 'r')
s =
return s
def filename_with_suffix_dropped(filename)
filename_array = filename.split('.')
filename_array.delete_at(filename_array.length - 1)
filename = filename_array.join('.')
return filename
def filename_with_suffix_changed(filename, new_suffix)
filename = filename_with_suffix_dropped(filename)
return filename + new_suffix
def write_file(s, f)
f =, 'w')
def html_text_for_file(f)
markdown_text = text_of_file(f)
html_text =
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
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)
folder = Dir.pwd
Find.find(folder) do |f|
filename = File.basename(f)
if markdown_file?(f)
print(filename + "\n")

This comment has been minimized.

Copy link

kejadlen commented Dec 5, 2013

Have you thought about using Rake for this? Seems like it would be a natural fit for simplifying this.


This comment has been minimized.

Copy link

jmeosbn commented Dec 5, 2013

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! ;)


This comment has been minimized.

Copy link

sklppr commented Dec 5, 2013

But since we all love Ruby, here’s a more compact and idiomatic version:


This comment has been minimized.

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.

jekyll build --watch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.