make difficulty_stars a ruby method instead

This commit is contained in:
Lynne Megido 2020-03-06 21:16:12 +10:00
parent 407c6f473b
commit 1d295e27f5
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
4 changed files with 22 additions and 32 deletions

View file

@ -1,30 +0,0 @@
{%- if page.difficulty or recipe.difficulty -%}
{%- if page.difficulty -%}
{%- assign difficulty = page.difficulty -%}
{%- else -%}
{%- assign difficulty = recipe.difficulty -%}
{%- endif -%}
{%- if difficulty < 6 and difficulty > 0 -%}
{%- comment -%} note: these characters are invalid unicode. they will only render properly on the webpage using the bundled "stars" font. {%- endcomment -%}
{%- assign empty_star ="" -%}
{%- assign full_star = "" -%}
{%- assign half_star = "" -%}
{%- comment -%} build a string of empty stars {%- endcomment -%}
{%- assign stars = "" -%}
{%- for i in (1..5) -%}
{%- assign stars = stars | append: empty_star -%}
{%- endfor -%}
{%- comment -%} replace the first difficulty stars will filled stars {%- endcomment -%}
{%- assign page_stars_rounded = difficulty | floor -%}
{%- for i in (1..page_stars_rounded) -%}
{%- assign stars = stars | replace_first: empty_star, full_star -%}
{%- endfor -%}
{%- if difficulty != page_stars_rounded -%}
{%- comment -%} stars ends in .5 (or .1, or .9, or whatever) {%- endcomment -%}
{%- assign stars = stars | replace_first: empty_star, half_star -%}
{%- endif -%}
<span class='stars'>{{ stars }}</span>
{%- endif -%}
{%- endif -%}

View file

@ -4,7 +4,7 @@
<h3>{{ recipe.name }}</h3> <h3>{{ recipe.name }}</h3>
<p> <p>
By {{ recipe.author }} - By {{ recipe.author }} -
{% if recipe.difficulty %}Difficulty: {% include difficulty_stars.html %} - {%- endif %} {% if recipe.difficulty %}Difficulty: <span class='stars'>{{ recipe.difficulty | to_stars }}</span> - {%- endif %}
{{ recipe.method | size }} steps {%- if recipe.time %} - {{ recipe.method | size }} steps {%- if recipe.time %} -
{%- if recipe.time > 59 %} {{ recipe.time | divided_by: 60}}h {% endif -%} {%- if recipe.time > 59 %} {{ recipe.time | divided_by: 60}}h {% endif -%}
{% assign mins = recipe.time | modulo: 60 -%} {% assign mins = recipe.time | modulo: 60 -%}

View file

@ -31,7 +31,7 @@ custom_h1: true
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}
<p class='subtle'>{% if page.difficulty %}Difficulty: {% include difficulty_stars.html %} - {% endif %}{{ page.method.size }} steps - Estimated cooking time: {{ time | default: "Not provided" }} {% if page.author %} - By {{ page.author }} {% endif %}</p> <p class='subtle'>{% if page.difficulty %}Difficulty: <span class='stars'>{{ page.difficulty | to_stars }}</span> - {% endif %}{{ page.method.size }} steps - Estimated cooking time: {{ time | default: "Not provided" }} {% if page.author %} - By {{ page.author }} {% endif %}</p>
{%- if applicable_subrecipes %} {%- if applicable_subrecipes %}
<h2> Subrecipes </h2> <h2> Subrecipes </h2>
{% for subrecipe in site.subrecipes -%} {% for subrecipe in site.subrecipes -%}

View file

@ -35,7 +35,27 @@ module Jekyll
end end
"<span title='#{input}'>#{leading_integer != "0" ? "#{leading_integer} " : ""}<span class='fraction'><span class='numerator'>#{rational.numerator}</span><span class='denominator'>#{rational.denominator}</span></span></span>" "<span title='#{input}'>#{leading_integer != "0" ? "#{leading_integer} " : ""}<span class='fraction'><span class='numerator'>#{rational.numerator}</span><span class='denominator'>#{rational.denominator}</span></span></span>"
end
def to_stars(input)
input = input.to_i
if input <= 0 or input > 5
return "[invalid difficulty value: #{input}. difficulty must be between 0.5 and 5 (inclusive)]"
end
# the "star" characters are invalid unicode but will render properly on the webpage when using the 'stars' font
empty_star, half_star, full_star = "", "", ""
out = full_star * input.floor
if input.floor != input
out += half_star
end
while out.length != 5
out += empty_star
end
out
end end
end end
end end