Skip to content

Commit

Permalink
code style, update single include
Browse files Browse the repository at this point in the history
  • Loading branch information
pantor committed Oct 5, 2024
1 parent 478d1ae commit cac6ac3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ env.set_line_statement("##"); // Line statements ## (just an opener)
env.set_html_autoescape(true); // Perform HTML escaping on all strings
```
### HTML escaping
Templates are frequently used to creat HTML pages. Source data that contains
characters that have meaning within HTML (like <. >, &) needs to be escaped.
It is often inconvenient to perform such escaping within the JSON data.
With Environment::set_html_autoescape(true), Inja can be configured to
HTML escape each and every string created.
### Variables
Variables are rendered within the `{{ ... }}` expressions.
Expand Down Expand Up @@ -373,6 +365,13 @@ render("{% if neighbour in guests -%} I was there{% endif -%} !", data); //
Stripping behind a statement or expression also removes any newlines.
### HTML escaping
Templates are frequently used to creat HTML pages. Source data that contains
characters that have meaning within HTML (like <. >, &) needs to be escaped.
It is often inconvenient to perform such escaping within the JSON data. With `Environment::set_html_autoescape(true)`, Inja can be configured to
HTML escape each and every string created.
### Comments
Comments can be written with the `{# ... #}` syntax.
Expand Down
33 changes: 16 additions & 17 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,29 @@ class Renderer : public NodeVisitor {
return !data->empty();
}


static std::string htmlescape(const std::string& data)
{
static std::string htmlescape(const std::string& data) {
std::string buffer;
buffer.reserve(1.1*data.size());
for(size_t pos = 0; pos != data.size(); ++pos) {
switch(data[pos]) {
case '&': buffer.append("&amp;"); break;
case '\"': buffer.append("&quot;"); break;
case '\'': buffer.append("&apos;"); break;
case '<': buffer.append("&lt;"); break;
case '>': buffer.append("&gt;"); break;
default: buffer.append(&data[pos], 1); break;
}
buffer.reserve(1.1 * data.size());

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2019-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_benchmark.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2019-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_test.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2019-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_test.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2019-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_benchmark.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2022-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_benchmark.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2022-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_test.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2022-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_test.vcxproj]

Check warning on line 58 in include/inja/renderer.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2022-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\inja_benchmark.vcxproj]
for (size_t pos = 0; pos != data.size(); ++pos) {
switch (data[pos]) {
case '&': buffer.append("&amp;"); break;
case '\"': buffer.append("&quot;"); break;
case '\'': buffer.append("&apos;"); break;
case '<': buffer.append("&lt;"); break;
case '>': buffer.append("&gt;"); break;
default: buffer.append(&data[pos], 1); break;
}
}
return buffer;
}

void print_data(const std::shared_ptr<json> value) {
if (value->is_string()) {
if(config.html_autoescape)
*output_stream << htmlescape(value->get_ref<const json::string_t&>());
else
*output_stream << value->get_ref<const json::string_t&>();
if (config.html_autoescape) {
*output_stream << htmlescape(value->get_ref<const json::string_t&>());
} else {
*output_stream << value->get_ref<const json::string_t&>();
}
} else if (value->is_number_unsigned()) {
*output_stream << value->get<const json::number_unsigned_t>();
} else if (value->is_number_integer()) {
Expand Down
33 changes: 31 additions & 2 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,9 +2125,29 @@ class Renderer : public NodeVisitor {
return !data->empty();
}

static std::string htmlescape(const std::string& data) {
std::string buffer;
buffer.reserve(1.1 * data.size());

Check warning on line 2130 in single_include/inja/inja.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2019-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\single_inja_test.vcxproj]

Check warning on line 2130 in single_include/inja/inja.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2019-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\single_inja_test.vcxproj]

Check warning on line 2130 in single_include/inja/inja.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2022-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\single_inja_test.vcxproj]

Check warning on line 2130 in single_include/inja/inja.hpp

View workflow job for this annotation

GitHub Actions / test-windows-2022-msvc

'argument': conversion from 'double' to 'const unsigned __int64', possible loss of data [D:\a\inja\inja\build\single_inja_test.vcxproj]
for (size_t pos = 0; pos != data.size(); ++pos) {
switch (data[pos]) {
case '&': buffer.append("&amp;"); break;
case '\"': buffer.append("&quot;"); break;
case '\'': buffer.append("&apos;"); break;
case '<': buffer.append("&lt;"); break;
case '>': buffer.append("&gt;"); break;
default: buffer.append(&data[pos], 1); break;
}
}
return buffer;
}

void print_data(const std::shared_ptr<json> value) {
if (value->is_string()) {
*output_stream << value->get_ref<const json::string_t&>();
if (config.html_autoescape) {
*output_stream << htmlescape(value->get_ref<const json::string_t&>());
} else {
*output_stream << value->get_ref<const json::string_t&>();
}
} else if (value->is_number_unsigned()) {
*output_stream << value->get<const json::number_unsigned_t>();
} else if (value->is_number_integer()) {
Expand Down Expand Up @@ -2383,7 +2403,7 @@ class Renderer : public NodeVisitor {
} break;
case Op::Capitalize: {
auto result = get_arguments<1>(node)[0]->get<json::string_t>();
result[0] = std::toupper(result[0]);
result[0] = static_cast<char>(::toupper(result[0]));
std::transform(result.begin() + 1, result.end(), result.begin() + 1, [](char c) { return static_cast<char>(::tolower(c)); });
make_result(std::move(result));
} break;
Expand Down Expand Up @@ -2793,6 +2813,11 @@ class Environment {
render_config.throw_at_missing_includes = will_throw;
}

/// Sets whether we'll automatically perform HTML escape
void set_html_autoescape(bool will_escape) {
render_config.html_autoescape = will_escape;
}

Template parse(std::string_view input) {
Parser parser(parser_config, lexer_config, template_storage, function_storage);
return parser.parse(input, input_path);
Expand Down Expand Up @@ -2855,6 +2880,10 @@ class Environment {
return os;
}

std::ostream& render_to(std::ostream& os, const std::string_view input, const json& data) {
return render_to(os, parse(input), data);
}

std::string load_file(const std::string& filename) {
Parser parser(parser_config, lexer_config, template_storage, function_storage);
return Parser::load_file(input_path + filename);
Expand Down

0 comments on commit cac6ac3

Please sign in to comment.