Skip to content

Instantly share code, notes, and snippets.

@jedschneider
Last active December 18, 2015 00:49
Show Gist options
  • Save jedschneider/5699700 to your computer and use it in GitHub Desktop.
Save jedschneider/5699700 to your computer and use it in GitHub Desktop.
class Page
require 'uri'
require 'json'
require 'net/http'
require 'net/https'
def initialize(name, markup, css)
@name = name
@markup = markup
@css = css
end
def render
data = {
text: @markup,
mode: 'markdown'
}
uri = URI('https://api.github.com/markdown')
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
response = https.post(uri.path, data.to_json)
stamp(response.body)
end
def stamp(html)
"""
<!DOCTYPE html>
<html>
<head>
<title>#{@name}</title>
<style>
#{@css}
</style>
</head>
<body>
<div class='wikistyle'>
#{html}
</div>
</body>
</html>
"""
end
end
# example use case
# css from https://github.com/jasonm23/markdown-css-themes
css = File.read('avenir-white.css')
Dir.glob("myfiles/**/*.md") do |file|
garbage, path, name = *file.match(/^(.*)\/(.*)\.md$/)
page = Page.new(name, File.read(file), css)
open("#{path}/#{name}.html", "w+") do |f|
f.write page.render
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment