From 1d295e27f54ce982a323218014c9314bb1448680 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Fri, 6 Mar 2020 21:16:12 +1000 Subject: [PATCH] make difficulty_stars a ruby method instead --- _includes/difficulty_stars.html | 30 ------------------------------ _includes/recipes.html | 2 +- _layouts/recipe.html | 2 +- _plugins/custom_stuff.rb | 20 ++++++++++++++++++++ 4 files changed, 22 insertions(+), 32 deletions(-) delete mode 100644 _includes/difficulty_stars.html diff --git a/_includes/difficulty_stars.html b/_includes/difficulty_stars.html deleted file mode 100644 index 5595514..0000000 --- a/_includes/difficulty_stars.html +++ /dev/null @@ -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 -%} - {{ stars }} - {%- endif -%} -{%- endif -%} diff --git a/_includes/recipes.html b/_includes/recipes.html index 8202efc..18d3bf3 100644 --- a/_includes/recipes.html +++ b/_includes/recipes.html @@ -4,7 +4,7 @@

{{ recipe.name }}

By {{ recipe.author }} - - {% if recipe.difficulty %}Difficulty: {% include difficulty_stars.html %} - {%- endif %} + {% if recipe.difficulty %}Difficulty: {{ recipe.difficulty | to_stars }} - {%- endif %} {{ recipe.method | size }} steps {%- if recipe.time %} - {%- if recipe.time > 59 %} {{ recipe.time | divided_by: 60}}h {% endif -%} {% assign mins = recipe.time | modulo: 60 -%} diff --git a/_layouts/recipe.html b/_layouts/recipe.html index 0fd7c6e..ad079e6 100644 --- a/_layouts/recipe.html +++ b/_layouts/recipe.html @@ -31,7 +31,7 @@ custom_h1: true {%- endif -%} {%- endif -%} -

{% 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 %}

+

{% if page.difficulty %}Difficulty: {{ page.difficulty | to_stars }} - {% endif %}{{ page.method.size }} steps - Estimated cooking time: {{ time | default: "Not provided" }} {% if page.author %} - By {{ page.author }} {% endif %}

{%- if applicable_subrecipes %}

Subrecipes

{% for subrecipe in site.subrecipes -%} diff --git a/_plugins/custom_stuff.rb b/_plugins/custom_stuff.rb index a5f932b..8b3439a 100644 --- a/_plugins/custom_stuff.rb +++ b/_plugins/custom_stuff.rb @@ -35,7 +35,27 @@ module Jekyll end "#{leading_integer != "0" ? "#{leading_integer} " : ""}#{rational.numerator}#{rational.denominator}" + 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