Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jxck
Created August 14, 2012 07:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jxck/3347239 to your computer and use it in GitHub Desktop.
Save Jxck/3347239 to your computer and use it in GitHub Desktop.
markdown rendering using Github API by ruby
readme.md
rendered.html
remain.txt
<html>
<head>
<!-- wget --header=Content-Type: text/plain --post-file=readme.md https://api.github.com/markdown/raw -->
<title>Markdown Rendering via GitHub API</title>
<link href="http://developer.github.com/css/reset.css" rel="stylesheet" type="text/css" />
<link href="http://developer.github.com/css/960.css" rel="stylesheet" type="text/css" />
<link href="http://developer.github.com/css/uv_active4d.css" rel="stylesheet" type="text/css" />
<link href="http://developer.github.com/shared/css/documentation.css" media="screen" rel="stylesheet" type="text/css">
<link href="http://developer.github.com/shared/css/pygments.css" media="screen" rel="stylesheet" type="text/css">
<script src="http://developer.github.com/shared/js/jquery.js" type="text/javascript"></script>
<script src="http://developer.github.com/shared/js/documentation.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
var cache;
setInterval(function() {
$.get('/rendered.html', function(data) {
if(cache === data) {
return;
}
cache = data;
console.log('render');
$('div.content').html(data);
});
$('#remain').load('/remain.txt');
},1000);
});
</script>
<style>
body.api {
margin: 0 10em;
}
h2 {
border-top: 1px solid #ccc;
padding-top: 1em;
}
.content ul {
margin: 0 0 1.5em 1.5em;
}
</style>
</head>
<body class="api">
<div id="remain"></div>
<div id="wrapper">
<div class="content">
</div>
</div>
</body>
</html>
#md = `wget --header='Content-Type: text/plain' --post-file=readme.md https://api.github.com/markdown/raw`
require "net/https"
require "uri"
require 'webrick'
URL = "https://api.github.com/markdown/raw"
def render
uri = URI.parse(URL)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE #本当は良くないが証明書チェック省略
https.start {|h|
path = "/markdown/raw"
data = File.open("readme.md").read;
header = {"content-type" => "text/plain"}
response = h.post(path, data, header)
remain = response.header['X-RateLimit-Remaining']
body = response.body
writeFile("remain.txt", remain)
writeFile("rendered.html", body)
}
end
def writeFile(path, data)
file = File.open(path, "w")
file.puts(data)
file.close
end
server = WEBrick::HTTPServer.new({
:BindAddress => '127.0.0.1',
:Port => 3000,
:Logger => WEBrick::BasicLog.new('/dev/null', WEBrick::BasicLog::DEBUG),
:AccessLog => WEBrick::BasicLog.new('/dev/null', WEBrick::BasicLog::DEBUG),
})
server.mount_proc('/') do |req, res|
res.body = File.read("gfm.html")
end
server.mount_proc('/rendered.html') do |req, res|
res.body = File.read("rendered.html")
end
server.mount_proc('/remain.txt') do |req, res|
res.body = File.read("remain.txt")
end
t = Thread.new do
ctime=nil;
loop {
sleep(2)
c = File.ctime("readme.md")
if c != ctime
ctime = c
render
end
}
end
trap(:INT){
Thread.kill(t)
server.shutdown
}
server.start
@gaurish
Copy link

gaurish commented Oct 21, 2012

Nifty! I cloned the repo & placed my markdown file as readme.md but still
I am getting empty Documents, how does this work? If yes, then what I am I doing wrong? Kindly let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment