Skip to content

Instantly share code, notes, and snippets.

Embed
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 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
@kejadlen

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.

@jmeosbn

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

@sklppr

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:
https://gist.github.com/sklppr/7803789

@mcormier

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.

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
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.