Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added striptags filter #177

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/linux-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
INPUT_BASE_CONFIG: ${{ matrix.build-config }}
INPUT_EXTRA_FLAGS: ${{ matrix.extra-flags }}
run: |
sudo apt-get update
sudo apt-get install -y cmake build-essential ${INPUT_COMPILER}
- name: Prepare build
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**)
- 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 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)
Expand Down
1 change: 1 addition & 0 deletions src/filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ std::unordered_map<std::string, ExpressionFilter::FilterFactoryFn> s_filters = {
{"selectattr", FilterFactory<filters::Tester>::MakeCreator(filters::Tester::SelectAttrMode)},
{"slice", FilterFactory<filters::Slice>::MakeCreator(filters::Slice::SliceMode)},
{"sort", &FilterFactory<filters::Sort>::Create},
{"striptags", FilterFactory<filters::StringConverter>::MakeCreator(filters::StringConverter::StriptagsMode) },
{"sum", FilterFactory<filters::SequenceAccessor>::MakeCreator(filters::SequenceAccessor::SumItemsMode)},
{"title", FilterFactory<filters::StringConverter>::MakeCreator(filters::StringConverter::TitleMode)},
{"tojson", FilterFactory<filters::Serialize>::MakeCreator(filters::Serialize::JsonMode)},
Expand Down
1 change: 1 addition & 0 deletions src/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class StringConverter : public FilterBase
EscapeHtmlMode,
LowerMode,
ReplaceMode,
StriptagsMode,
TitleMode,
TrimMode,
TruncateMode,
Expand Down
25 changes: 25 additions & 0 deletions src/string_converter_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <numeric>
#include <regex>
#include <sstream>

#include <boost/algorithm/string/trim_all.hpp>
Expand Down Expand Up @@ -343,6 +344,30 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
}
});
break;
case StriptagsMode:
result = ApplyStringConverter(baseVal, [](auto srcStr) -> TargetString {
auto str = sv_to_string(srcStr);
using StringT = decltype(str);
using CharT = typename StringT::value_type;
static const std::basic_regex<CharT> STRIPTAGS_RE(UNIVERSAL_STR("(<!--.*?-->|<[^>]*>)").GetValue<CharT>());
str = std::regex_replace(str, STRIPTAGS_RE, UNIVERSAL_STR("").GetValue<CharT>());
ba::trim_all(str);
static const StringT html_entities [] {
UNIVERSAL_STR("&amp;").GetValue<CharT>(), UNIVERSAL_STR("&").GetValue<CharT>(),
UNIVERSAL_STR("&apos;").GetValue<CharT>(), UNIVERSAL_STR("\'").GetValue<CharT>(),
UNIVERSAL_STR("&gt;").GetValue<CharT>(), UNIVERSAL_STR(">").GetValue<CharT>(),
UNIVERSAL_STR("&lt;").GetValue<CharT>(), UNIVERSAL_STR("<").GetValue<CharT>(),
UNIVERSAL_STR("&quot;").GetValue<CharT>(), UNIVERSAL_STR("\"").GetValue<CharT>(),
UNIVERSAL_STR("&#39;").GetValue<CharT>(), UNIVERSAL_STR("\'").GetValue<CharT>(),
UNIVERSAL_STR("&#34;").GetValue<CharT>(), UNIVERSAL_STR("\"").GetValue<CharT>(),
};
for (auto it = std::begin(html_entities), end = std::end(html_entities); it < end; it += 2)
{
ba::replace_all(str, *it, *(it + 1));
}
return str;
});
break;
default:
break;
}
Expand Down
9 changes: 9 additions & 0 deletions test/filters_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,12 @@ INSTANTIATE_TEST_CASE_P(ListSlice, ListSliceTest, ::testing::Values(
InputOutputPair{"[1, 2, 3, 4, 5, 6, 7] | slice(3) | pprint", "[[1, 2, 3], [4, 5, 6], [7]]"},
InputOutputPair{"[1, 2, 3, 4, 5, 6, 7] | slice(3, 0) | pprint", "[[1, 2, 3], [4, 5, 6], [7, 0, 0]]"}
));

INSTANTIATE_TEST_CASE_P(Striptags, FilterGenericTest, ::testing::Values(
InputOutputPair{ "' Hello World ' | striptags | pprint", "'Hello World'" },
InputOutputPair{ "'foo <bar> baz <qux>' | striptags | pprint", "'foo baz'" },
InputOutputPair{"'ab&cd&amp;&gt;&lt;efgh' | striptags | pprint", "'ab&cd&><efgh'"},
InputOutputPair{"'<em>Foo &amp; Bar</em>' | striptags | pprint", "'Foo & Bar'"},
InputOutputPair{"'&amp;&apos;&gt;&lt;&quot;&#39;\"&#34;' | striptags | pprint", "'&\'><\"\'\"\"'"},
InputOutputPair{"'&#34;&#39;' | striptags | pprint", "'\"\''"}));