-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1389 from sneakers-the-rat/badges
Reviewer Badges :)
- Loading branch information
Showing
10 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Based on | ||
# - https://github.com/metabolize/anafanafo/tree/main | ||
# - https://github.com/badges/shields/blob/master/badge-maker/lib/badge-renderers.js | ||
class BadgesController < ApplicationController | ||
|
||
PADDING = 5 | ||
SCALE_UP_FACTOR = 10 | ||
SCALE_DOWN_FACTOR = 0.1 | ||
|
||
COLORS = { | ||
gray: "#555", | ||
blue: "#007ec6" | ||
} | ||
|
||
def reviewed_by | ||
n_reviews = Paper.where(":reviewer = ANY (reviewers)", reviewer: params[:reviewer]).count | ||
@key = string_item("JOSS Reviews", COLORS[:gray]) | ||
@value = string_item(n_reviews.to_s, COLORS[:blue], @key[:outer_width]) | ||
@badge_width = @key[:outer_width] + @value[:outer_width] | ||
|
||
render template: 'badges/badge', locals: { | ||
scale_up_factor: SCALE_UP_FACTOR, | ||
scale_down_factor: SCALE_DOWN_FACTOR | ||
} | ||
end | ||
|
||
private | ||
|
||
def string_item(str, color, offset=0) | ||
# Object that contains templating information for rendering a string within a badge | ||
width = string_width(str) | ||
{ | ||
text_width: width, | ||
outer_width: width + PADDING * 2, | ||
x: text_x_position(width, offset), | ||
text: str, | ||
color: color | ||
} | ||
end | ||
|
||
def string_width(data) | ||
data.to_s.codepoints.reduce(0) do |total, point| | ||
char_width = point < VERDANA_WIDTHS.length ? VERDANA_WIDTHS[point] : em_width | ||
total + char_width * SCALE_DOWN_FACTOR | ||
end | ||
end | ||
|
||
def text_x_position(width, offset=0) | ||
SCALE_UP_FACTOR * (offset + PADDING + width * 0.5) | ||
end | ||
|
||
def em_width | ||
VERDANA_WIDTHS["m".codepoints[0]] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="<%= @badge_width %>" height="20" role="img" aria-label="<%= @key[:text] %>: <%= @value[:text] %>"> | ||
<title><%= @key[:text] %>: <%= @value[:text] %></title> | ||
<linearGradient id="s" x2="0" y2="100%"> | ||
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/> | ||
<stop offset="1" stop-opacity=".1"/> | ||
</linearGradient> | ||
<clipPath id="r"> | ||
<rect width="<%= @badge_width %>" height="20" rx="3" fill="#fff"/> | ||
</clipPath> | ||
<g clip-path="url(#r)"> | ||
<rect width="<%= @key[:outer_width] %>" height="20" fill="<%= @key[:color] %>"/> | ||
<rect x="<%= @key[:outer_width] %>" width="<%= @value[:outer_width] %>" height="20" fill="<%= @value[:color] %>"/> | ||
<rect width="<%= @badge_width %>" height="20" fill="url(#s)"/> | ||
</g> | ||
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"> | ||
<text aria-hidden="true" x="<%= @key[:x] %>" y="150" fill="#010101" fill-opacity=".3" transform="scale(<%= scale_down_factor %>)" textLength="<%= @key[:text_width] * scale_up_factor %>"><%= @key[:text] %></text> | ||
<text x="<%= @key[:x] %>" y="140" transform="scale(<%= scale_down_factor %>)" fill="#fff" textLength="<%= @key[:text_width] * scale_up_factor %>"><%= @key[:text] %></text> | ||
<text aria-hidden="true" x="<%= @value[:x] %>" y="150" fill="#010101" fill-opacity=".3" transform="scale(<%= scale_down_factor %>)" textLength="<%= @value[:text_width] * scale_up_factor %>"><%= @value[:text] %></text> | ||
<text x="<%= @value[:x] %>" y="140" transform="scale(.1)" fill="#fff" textLength="<%= @value[:text_width] * scale_up_factor %>"><%= @value[:text] %></text> | ||
</g> | ||
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<% if badge[:type] == 'reviewer' %> | ||
<% badge_url = badges_by_reviewer_url(reviewer: badge[:value]) %> | ||
<% badge_markdown = "[![JOSS Reviews](#{badge_url})](#{papers_by_reviewer_url(reviewer: badge[:value])})" %> | ||
<% else %> | ||
<% badge_url = "" %> | ||
<% badge_markdown = "" %> | ||
<% end %> | ||
<div class="badge-container" id="markdown-badge"> | ||
<img id="reviewer-badge" src="<%= badge_url %>"> | ||
<button | ||
id="badge-copy-button" | ||
class="btn btn-outline-secondary btn-sm" | ||
data-toggle="popover" | ||
title="Copied!" | ||
data-content="<pre><%= badge_markdown %></pre>" | ||
data-sanitize="false" | ||
data-html="true" | ||
onclick="window.copyText('<%= badge_markdown %>')" | ||
> | ||
Copy Markdown | ||
</button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters