Skip to content

Commit 9cc0981

Browse files
Added striptags filter
1 parent 2e7453b commit 9cc0981

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ More detailed examples and features description can be found in the documentatio
9696
## Current Jinja2 support
9797
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:
9898
- expressions. You can use almost every expression style: simple, filtered, conditional, and so on.
99-
- 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**)
99+
- 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**)
100100
- the big number of testers (**eq, defined, ge, gt, iterable, le, lt, mapping, ne, number, sequence, string, undefined, in, even, odd, lower, upper**)
101101
- the number of functions (**range**, **loop.cycle**)
102102
- 'if' statement (with 'elif' and 'else' branches)

src/filters.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ std::unordered_map<std::string, ExpressionFilter::FilterFactoryFn> s_filters = {
7070
{"selectattr", FilterFactory<filters::Tester>::MakeCreator(filters::Tester::SelectAttrMode)},
7171
{"slice", FilterFactory<filters::Slice>::MakeCreator(filters::Slice::SliceMode)},
7272
{"sort", &FilterFactory<filters::Sort>::Create},
73+
{"striptags", FilterFactory<filters::StringConverter>::MakeCreator(filters::StringConverter::StriptagsMode) },
7374
{"sum", FilterFactory<filters::SequenceAccessor>::MakeCreator(filters::SequenceAccessor::SumItemsMode)},
7475
{"title", FilterFactory<filters::StringConverter>::MakeCreator(filters::StringConverter::TitleMode)},
7576
{"tojson", FilterFactory<filters::Serialize>::MakeCreator(filters::Serialize::JsonMode)},

src/filters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class StringConverter : public FilterBase
182182
EscapeHtmlMode,
183183
LowerMode,
184184
ReplaceMode,
185+
StriptagsMode,
185186
TitleMode,
186187
TrimMode,
187188
TruncateMode,

src/string_converter_filter.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <algorithm>
77
#include <numeric>
8+
#include <regex>
89
#include <sstream>
910

1011
#include <boost/algorithm/string/trim_all.hpp>
@@ -343,6 +344,30 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
343344
}
344345
});
345346
break;
347+
case StriptagsMode:
348+
result = ApplyStringConverter(baseVal, [](auto srcStr) -> TargetString {
349+
auto str = sv_to_string(srcStr);
350+
using StringT = decltype(str);
351+
using CharT = typename StringT::value_type;
352+
static const std::basic_regex<CharT> STRIPTAGS_RE(UNIVERSAL_STR("(<!--.*?-->|<[^>]*>)").GetValue<CharT>());
353+
str = std::regex_replace(str, STRIPTAGS_RE, UNIVERSAL_STR("").GetValue<CharT>());
354+
ba::trim_all(str);
355+
static const StringT html_entities [] {
356+
UNIVERSAL_STR("&amp;").GetValue<CharT>(), UNIVERSAL_STR("&").GetValue<CharT>(),
357+
UNIVERSAL_STR("&apos;").GetValue<CharT>(), UNIVERSAL_STR("\'").GetValue<CharT>(),
358+
UNIVERSAL_STR("&gt;").GetValue<CharT>(), UNIVERSAL_STR(">").GetValue<CharT>(),
359+
UNIVERSAL_STR("&lt;").GetValue<CharT>(), UNIVERSAL_STR("<").GetValue<CharT>(),
360+
UNIVERSAL_STR("&quot;").GetValue<CharT>(), UNIVERSAL_STR("\"").GetValue<CharT>(),
361+
UNIVERSAL_STR("&#39;").GetValue<CharT>(), UNIVERSAL_STR("\'").GetValue<CharT>(),
362+
UNIVERSAL_STR("&#34;").GetValue<CharT>(), UNIVERSAL_STR("\"").GetValue<CharT>(),
363+
};
364+
for (auto it = std::begin(html_entities), end = std::end(html_entities); it < end; it += 2)
365+
{
366+
ba::replace_all(str, *it, *(it + 1));
367+
}
368+
return str;
369+
});
370+
break;
346371
default:
347372
break;
348373
}

test/filters_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,12 @@ INSTANTIATE_TEST_CASE_P(ListSlice, ListSliceTest, ::testing::Values(
555555
InputOutputPair{"[1, 2, 3, 4, 5, 6, 7] | slice(3) | pprint", "[[1, 2, 3], [4, 5, 6], [7]]"},
556556
InputOutputPair{"[1, 2, 3, 4, 5, 6, 7] | slice(3, 0) | pprint", "[[1, 2, 3], [4, 5, 6], [7, 0, 0]]"}
557557
));
558+
559+
INSTANTIATE_TEST_CASE_P(Striptags, FilterGenericTest, ::testing::Values(
560+
InputOutputPair{ "' Hello World ' | striptags | pprint", "'Hello World'" },
561+
InputOutputPair{ "'foo <bar> baz <qux>' | striptags | pprint", "'foo baz'" },
562+
InputOutputPair{"'ab&cd&amp;&gt;&lt;efgh' | striptags | pprint", "'ab&cd&><efgh'"},
563+
InputOutputPair{"'<em>Foo &amp; Bar</em>' | striptags | pprint", "'Foo & Bar'"},
564+
InputOutputPair{"'&amp;&apos;&gt;&lt;&quot;&#39;\"&#34;' | striptags | pprint", "'&\'><\"\'\"\"'"},
565+
InputOutputPair{"'&#34;&#39;' | striptags | pprint", "'\"\''"}));
566+

0 commit comments

Comments
 (0)