Skip to content

Instantly share code, notes, and snippets.

@kubicek
Last active May 17, 2017 01:13
Show Gist options
  • Save kubicek/415374767cfd8cba9a4872effbee619b to your computer and use it in GitHub Desktop.
Save kubicek/415374767cfd8cba9a4872effbee619b to your computer and use it in GitHub Desktop.
git repo browser
require 'sinatra'
require 'rugged'
require 'pandoc-ruby'
require 'yaml'
set :port, ENV["PORT"] || 5000
before do
@repo = Rugged::Repository.new('.')
@branch = @repo.branches["builds"]
end
get '/' do
@tree = @branch.target.tree
erb :index
end
get '/*' do
path = params['splat'].join('/')
item = @branch.target.tree.path(path)
if item[:type] == :tree
erb :tree, locals: { tree: @repo.lookup(item[:oid]), path: path}
else
blob = @repo.lookup(item[:oid])
ext = File.extname(path)
if ext==".md"
data = blob.content.force_encoding("utf-8")
blank, header, content = data.split("---\n",3)
"<pre>#{header}</pre>"+
PandocRuby.new(data, from: :markdown, to: (params[:format] || "html")).convert.force_encoding("utf-8")
else
content_type ext
blob.content
end
end
end
__END__
@@ layout
<html>
<%= yield %>
</html>
@@ index
<div>Hello world</div>
<%= erb :tree, locals: {tree: @tree, path: '/'} %>
@@ tree
<h1>tree</h1>
<% tree.each do |item| %>
<li>
<%= "<a href=\"#{File.join(path, item[:name])}\">#{item[:name]}</a>" %>
</li>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment