From 96cbebf8b4e10d21339ad09e738f4cc9a52592eb Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Fri, 1 Nov 2019 21:58:20 -0400 Subject: [PATCH 1/2] Add center filter --- README.md | 2 +- src/filters.cpp | 1 + src/filters.h | 3 ++- src/string_converter_filter.cpp | 16 ++++++++++++++++ test/filters_test.cpp | 6 ++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0a274ec1..b4544831 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ More detailed examples and features description can be found in the documentatio ## Current Jinja2 support Currently, Jinja2C++ supports the limited number of Jinja2 features. By the way, Jinja2C++ is planned to be a full [jinja2 specification](http://jinja.pocoo.org/docs/2.10/templates/)-conformant. The current support is limited to: - expressions. You can use almost every expression style: simple, filtered, conditional, and so on. -- the big number of filters (**sort, default, first, last, length, max, min, reverse, unique, sum, attr, map, reject, rejectattr, select, selectattr, pprint, dictsort, abs, float, int, list, round, random, trim, title, upper, wordcount, replace, truncate, groupby, urlencode, capitalize, escape, tojson, striptags**) +- the big number of filters (**sort, default, first, last, length, max, min, reverse, unique, sum, attr, map, reject, rejectattr, select, selectattr, pprint, dictsort, abs, float, int, list, round, random, trim, title, upper, wordcount, replace, truncate, groupby, urlencode, capitalize, escape, tojson, striptags, center**) - the big number of testers (**eq, defined, ge, gt, iterable, le, lt, mapping, ne, number, sequence, string, undefined, in, even, odd, lower, upper**) - the number of functions (**range**, **loop.cycle**) - 'if' statement (with 'elif' and 'else' branches) diff --git a/src/filters.cpp b/src/filters.cpp index 1ec0f4a3..fc594d93 100644 --- a/src/filters.cpp +++ b/src/filters.cpp @@ -41,6 +41,7 @@ std::unordered_map s_filters = { {"batch", FilterFactory::MakeCreator(filters::Slice::BatchMode)}, {"camelize", FilterFactory::MakeCreator(filters::StringConverter::CamelMode)}, {"capitalize", FilterFactory::MakeCreator(filters::StringConverter::CapitalMode)}, + {"center", FilterFactory::MakeCreator(filters::StringConverter::CenterMode)}, {"default", &FilterFactory::Create}, {"d", &FilterFactory::Create}, {"dictsort", &FilterFactory::Create}, diff --git a/src/filters.h b/src/filters.h index 31a5ab0d..c4768ad8 100644 --- a/src/filters.h +++ b/src/filters.h @@ -190,7 +190,8 @@ class StringConverter : public FilterBase WordCountMode, WordWrapMode, UnderscoreMode, - UrlEncodeMode + UrlEncodeMode, + CenterMode }; StringConverter(FilterParams params, Mode mode); diff --git a/src/string_converter_filter.cpp b/src/string_converter_filter.cpp index 18a0701a..634b565c 100644 --- a/src/string_converter_filter.cpp +++ b/src/string_converter_filter.cpp @@ -176,6 +176,9 @@ StringConverter::StringConverter(FilterParams params, StringConverter::Mode mode case TruncateMode: ParseParams({{"length", false, static_cast(255)}, {"killwords", false, false}, {"end", false, std::string("...")}, {"leeway", false}}, params); break; + case CenterMode: + ParseParams({{"width", false, static_cast(80)}}, params); + break; default: break; } } @@ -368,6 +371,19 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex return str; }); break; + case CenterMode: + result = ApplyStringConverter(baseVal, [this, &context](auto srcStr) -> TargetString { + auto width = ConvertToInt(this->GetArgumentValue("width", context)); + auto str = sv_to_string(srcStr); + auto string_length = static_cast(str.size()); + if ( string_length >= width ) + return str; + auto whitespaces = width - string_length; + str.insert(0, (whitespaces + 1) / 2, ' '); + str.append(whitespaces / 2, ' '); + return TargetString(std::move(str)); + }); + break; default: break; } diff --git a/test/filters_test.cpp b/test/filters_test.cpp index e331365f..5c356408 100644 --- a/test/filters_test.cpp +++ b/test/filters_test.cpp @@ -564,3 +564,9 @@ INSTANTIATE_TEST_CASE_P(Striptags, FilterGenericTest, ::testing::Values( InputOutputPair{"'&'><"'\""' | striptags | pprint", "'&\'><\"\'\"\"'"}, InputOutputPair{"'"'' | striptags | pprint", "'\"\''"})); + +INSTANTIATE_TEST_CASE_P(Center, FilterGenericTest, ::testing::Values( + InputOutputPair{" 'x' | center | pprint", "' x '"}, + InputOutputPair{" 'x' | center(width=5) | pprint", "' x '"}, + InputOutputPair{" 'x' | center(width=0) | pprint", "'x'"} + )); From baa4ca0ed66afa57e29d6306901a0dc80381e7ec Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Sat, 2 Nov 2019 10:47:28 -0400 Subject: [PATCH 2/2] Add additional test --- test/filters_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/filters_test.cpp b/test/filters_test.cpp index 5c356408..375fab92 100644 --- a/test/filters_test.cpp +++ b/test/filters_test.cpp @@ -568,5 +568,6 @@ INSTANTIATE_TEST_CASE_P(Striptags, FilterGenericTest, ::testing::Values( INSTANTIATE_TEST_CASE_P(Center, FilterGenericTest, ::testing::Values( InputOutputPair{" 'x' | center | pprint", "' x '"}, InputOutputPair{" 'x' | center(width=5) | pprint", "' x '"}, - InputOutputPair{" 'x' | center(width=0) | pprint", "'x'"} + InputOutputPair{" 'x' | center(width=0) | pprint", "'x'"}, + InputOutputPair{" ' x' | center(width=5) | pprint", "' x '"} ));