Skip to content

Instantly share code, notes, and snippets.

@timblair
Created June 29, 2011 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timblair/1054177 to your computer and use it in GitHub Desktop.
Save timblair/1054177 to your computer and use it in GitHub Desktop.
Uses the `markdown` command-line tool to build an HTML doc.
#!/usr/bin/env ruby
# A simple wrapper to the command-line `markdown` tool which wraps
# HTML header and footer, including any style references you choose.
require 'open3'
require 'erb'
# Define the ERB template to wrap the HTML content in.
template = ERB.new <<-EOS
<html>
<head>
<title><%= title %></title>
<link rel="stylesheet" href="https://gist.github.com/raw/516763/632b066c7c18edc91485ba13b37ba50a9b2caac7/screen.css" />
</head>
<body>
<%= content %>
</body>
</html>
EOS
# Run the given file through the markdown command-line tool.
content = Open3.popen3('markdown') do | stdin, stdout, stderr |
stdin.puts File.read(ARGV.first)
stdin.close
stdout.read
end
# Find the first `<h1>` tag to use as a document title.
title = content.match(/<h1[^>]*>(.*)<\/h1>/)[1] || "Unknown"
# Output the rendered HTML to STDOUT.
puts template.result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment