Skip to content

Commit

Permalink
Support formatting result
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jul 10, 2017
1 parent 1dea51b commit 19ce0db
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
23 changes: 23 additions & 0 deletions app/assets/stylesheets/application.scss
Expand Up @@ -35,3 +35,26 @@ form {
}
}
}

table {
@extend .table;
@extend .table-bordered;
@extend .table-striped;
@extend .table-hover;
@extend .table-responsive;
}

section.text {
img {
float: left;
margin-right: 10px;
}

pre {
white-space: pre-wrap;
}

table {
clear: both;
}
}
3 changes: 2 additions & 1 deletion app/controllers/extractions_controller.rb
Expand Up @@ -11,8 +11,9 @@ def create

respond_to do |format|
if @extraction.valid?
@extracted = @extraction.extract
format.html { render :create }
format.json { render json: @extraction.extract }
format.json { render json: @extract }
else
format.html { render :show }
format.json { render json: @extraction.errors, status: :unprocessable_entity }
Expand Down
17 changes: 17 additions & 0 deletions app/helpers/extractions_helper.rb
@@ -1,2 +1,19 @@
module ExtractionsHelper
def format_screenshot(screenshot)
return "" if screenshot.nil?

data = screenshot["data"]
mime_type = screenshot["mime-type"]
encoding = screenshot["encoding"]
src = "data:#{mime_type}"
if encoding
src << ";#{encoding}"
src << ","
src << data
else
src << ";base64,"
src << [data].pack("m*")
end
tag.img(src: src)
end
end
30 changes: 30 additions & 0 deletions app/views/extractions/_extracted.html.erb
@@ -0,0 +1,30 @@
<section class="metadata">
<h2>Metadata</h2>
<table>
<% @extracted.each do |key, value| -%>
<% next if key == "texts" -%>
<tr>
<th scope="row"><%= key %></th>
<td><%= value %></td>
</tr>
<% end -%>
</table>
</section>

<% (@extracted["texts"] || []).each_with_index do |text, i| -%>
<section class="text">
<h2>Text #<%= i + 1 %></h2>
<%= format_screenshot(text["screenshot"]) %>
<pre><%= text["body"] %></pre>
<table>
<% text.each do |key, value| -%>
<% next if key == "body" -%>
<% next if key == "screenshot" -%>
<tr>
<th scope="row"><%= key %></th>
<td><%= value %></td>
</tr>
<% end -%>
</table>
</section>
<% end -%>
2 changes: 1 addition & 1 deletion app/views/extractions/create.html.erb
Expand Up @@ -2,4 +2,4 @@

<%= render 'form', extraction: @extraction %>
<pre><code><%= JSON.pretty_generate(@extraction.extract) %></code></pre>
<%= render 'extracted', extracted: @extracted %>

0 comments on commit 19ce0db

Please sign in to comment.