From e6f2077d9b28153c6dc43d2409feabd698b5f81b Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Fri, 6 Mar 2020 21:32:01 +1000 Subject: [PATCH] ruby function for formatting time --- _includes/recipes.html | 7 ++----- _layouts/recipe.html | 13 +------------ _plugins/custom_stuff.rb | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/_includes/recipes.html b/_includes/recipes.html index 18d3bf3..4ae3258 100644 --- a/_includes/recipes.html +++ b/_includes/recipes.html @@ -5,11 +5,8 @@

By {{ recipe.author }} - {% 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 -%} - {%- if mins > 0 %}{{ mins }}m{% endif %} - {%- endif -%} + {{ recipe.method | size }} steps + {%- if recipe.time %} - {{ recipe.time | hours_and_minutes: false }}{%- endif -%}

    {% for step in recipe.method | limit: 4 %} diff --git a/_layouts/recipe.html b/_layouts/recipe.html index ad079e6..36d1d30 100644 --- a/_layouts/recipe.html +++ b/_layouts/recipe.html @@ -17,18 +17,7 @@ custom_h1: true

    {{ page.name }}

    {{ page.name }}

    {%- if page.time -%} - {%- if page.time > 59 %} - {%- assign hours = page.time | divided_by: 60 -%} - {%- assign minutes = page.time | modulo: 60 -%} - {%- assign time = hours | append: " hour" -%} - {%- if hours > 1 %}{% assign time = time | append: "s" %}{% endif -%} - {%- if minutes > 0 -%} - {%- assign time = time | append: " " | append: minutes | append: " minute" -%} - {%- if minutes > 1 %}{% assign time = time | append: "s" %}{% endif -%} - {%- endif -%} - {%- else -%} - {%- assign time = page.time %} - {%- endif -%} + {%- assign time = page.time | hours_and_minutes -%} {%- 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 %}

    diff --git a/_plugins/custom_stuff.rb b/_plugins/custom_stuff.rb index 8b3439a..71f8dc4 100644 --- a/_plugins/custom_stuff.rb +++ b/_plugins/custom_stuff.rb @@ -57,6 +57,23 @@ module Jekyll out end + + def hours_and_minutes(input, long = true) + time = input.to_i.divmod(60) + hours, minutes = "h", "m" + if long + hours = " hour" + (time[0] > 1 ? "s" : "") + " " + minutes = " minute" + (time[1] > 1 ? "s" : "") + " " + end + + if time[1] == 0 + "#{time[0]}#{hours}" + elsif time[0] == 0 + "#{time[1]}#{minutes}" + else + "#{time[0]}#{hours}#{time[1]}#{minutes}" + end + end end end