-
-
Save sklppr/7803789 to your computer and use it in GitHub Desktop.
Batch conversion of Markdown to HTML.
This file contains hidden or 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 | |
require "fileutils" | |
require "redcarpet" | |
OUTPUT_FOLDER = "html" | |
HTML_TEMPLATE = <<-HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta encoding="utf-8" /> | |
<title>%s</title> | |
<style> | |
body { margin-top: 3em; } | |
.content { width: 33em; margin-left: 7em; margin-right: auto; } | |
</style> | |
</head> | |
<body> | |
%s | |
</body> | |
</html> | |
HTML | |
# Delete and recreate output folder. | |
FileUtils.rm_rf(OUTPUT_FOLDER) | |
Dir.mkdir(OUTPUT_FOLDER) | |
# Create Markdown to HTML converter. | |
converter = Redcarpet::Markdown.new(Redcarpet::Render::HTML) | |
# Get all Markdown files in current folder and all subfolders. | |
Dir["**/*.markdown", "**/*.mdown", "**/*.md"].each do |file| | |
begin | |
# Construct output filename including original path. | |
input_file = File.path(file) | |
basename = File.basename(file, File.extname(file)) | |
output_file = File.basename(File.path(file).gsub("/", "-"), File.extname(file)) + ".html" | |
# Convert Markdown to HTML. | |
puts "#{input_file} >> #{output_file}" | |
markdown = IO.read(file) | |
html = converter.render(markdown) | |
output = HTML_TEMPLATE % [basename, html] | |
# Write HTML to new file. | |
IO.write(File.join(OUTPUT_FOLDER, output_file), output) | |
rescue | |
puts $!.inspect, $@ | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment