From 0a812beb120620ad2bdc8b1504bb7edf6eaac18a Mon Sep 17 00:00:00 2001 From: Michael Orr Date: Wed, 13 Jul 2016 14:09:59 -0600 Subject: [PATCH] tpl: Modify tpl.humanize to ordinalize integer input Add logic to tpl.humanize such that it understands input of int literals or strings which represent an integer. When tpl.humanize sees this type of input, it will use inflect.Ordinalize as opposed to the standard inflect.Humanize. Fixes #1886 --- docs/content/templates/functions.md | 5 ++++- tpl/template_funcs.go | 11 ++++++++++- tpl/template_funcs_test.go | 7 ++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/content/templates/functions.md b/docs/content/templates/functions.md index 84767b224..6ba0a8963 100644 --- a/docs/content/templates/functions.md +++ b/docs/content/templates/functions.md @@ -450,12 +450,15 @@ Used in the [highlight shortcode](/extras/highlighting/). ### humanize -Humanize returns the humanized version of a string with the first letter capitalized. +Humanize returns the humanized version of an argument with the first letter capitalized. +If the input is either an int64 value or the string representation of an integer, humanize returns the number with the proper ordinal appended. e.g. ``` {{humanize "my-first-post"}} → "My first post" {{humanize "myCamelPost"}} → "My camel post" +{{humanize "52"}} → "52nd" +{{humanize 103}} → "103rd" ``` diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go index b34300ab2..b8896a9cd 100644 --- a/tpl/template_funcs.go +++ b/tpl/template_funcs.go @@ -1696,8 +1696,12 @@ func countRunes(content interface{}) (int, error) { return counter, nil } -// humanize returns the humanized form of a single word. +// humanize returns the humanized form of a single parameter. +// If the parameter is either an integer or a string containing an integer +// value, the behavior is to add the appropriate ordinal. // Example: "my-first-post" -> "My first post" +// Example: "103" -> "103rd" +// Example: 52 -> "52nd" func humanize(in interface{}) (string, error) { word, err := cast.ToStringE(in) if err != nil { @@ -1708,6 +1712,11 @@ func humanize(in interface{}) (string, error) { return "", nil } + _, ok := in.(int) // original param was literal int value + _, err = strconv.Atoi(word) // original param was string containing an int value + if ok == true || err == nil { + return inflect.Ordinalize(word), nil + } return inflect.Humanize(word), nil } diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go index f0d123c0f..1dcae066c 100644 --- a/tpl/template_funcs_test.go +++ b/tpl/template_funcs_test.go @@ -1801,11 +1801,16 @@ func TestHighlight(t *testing.T) { func TestInflect(t *testing.T) { for i, this := range []struct { inflectFunc func(i interface{}) (string, error) - in string + in interface{} expected string }{ {humanize, "MyCamel", "My camel"}, {humanize, "", ""}, + {humanize, "103", "103rd"}, + {humanize, "41", "41st"}, + {humanize, 103, "103rd"}, + {humanize, int64(92), "92nd"}, + {humanize, "5.5", "5.5"}, {pluralize, "cat", "cats"}, {pluralize, "", ""}, {singularize, "cats", "cat"},