Skip to content

Instantly share code, notes, and snippets.

@justinj
Created September 8, 2013 21:01
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 justinj/6488381 to your computer and use it in GitHub Desktop.
Save justinj/6488381 to your computer and use it in GitHub Desktop.
require "sinatra"
enable :inline_templates
def get_algs(input)
input
.lines
.map(&:chomp)
.reject(&:empty?)
.each_slice(2)
.map { |name, alg| [name, alg.split(/\s+or\s+/)] }
end
def image_for(alg, template)
%(<img src="#{template.gsub("$", alg.gsub(/\s/,""))}">)
end
get "/" do
erb :input
end
post "/create" do
erb :algpage, locals: { algs: get_algs(params["input"]),
template: params["template"] }
end
__END__
@@layout
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<%= yield %>
</div>
@@input
<form action="/create" method="POST">
<label>Template ($ is where your alg will go)</label>
<input type="text" name="template" class="form-control" value="http://cube.crider.co.uk/visualcube.php?pzl=2&fmt=gif&size=100&case=$">
<br>
<label>Algs, alternating title and alg, algs separated by "or", blank lines ignored</label>
<textarea name="input" class="form-control" rows="20" >
</textarea>
<br>
<input type="submit" value="GO!" class="form-control">
</form>
@@algpage
<table class="table">
<tr>
<th>Case</th>
<th>Image</th>
<th>Alg</th>
</tr>
<% algs.each do |name, alg| %>
<tr>
<td><%= name %></td>
<td><%= image_for(alg.first, template) %></td>
<td><%= alg.join("<br>") %></td>
</tr>
<% end %>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment