Enhancements

inja: re-implement exists() to work with JSON pointer for quick usage.
inja: move include scope limit config to ParserConfig.
Optimize codes.
This commit is contained in:
Tindy X 2020-04-04 00:33:44 +08:00
parent bcc4422e87
commit f63420be12
No known key found for this signature in database
GPG Key ID: C6AD413169968D58
2 changed files with 19 additions and 10 deletions

View File

@ -1594,6 +1594,9 @@ struct LexerConfig {
*/ */
struct ParserConfig { struct ParserConfig {
ElementNotation notation {ElementNotation::Dot}; ElementNotation notation {ElementNotation::Dot};
/// Extra options for limiting included files' scope
bool include_scope_limit = false;
std::string include_scope = "templates";
}; };
} // namespace inja } // namespace inja
@ -2684,7 +2687,7 @@ class Parser {
} }
// sys::path::remove_dots(pathname, true, sys::path::Style::posix); // sys::path::remove_dots(pathname, true, sys::path::Style::posix);
if(include_scope_limit) if(m_config.include_scope_limit)
{ {
#ifdef _WIN32 #ifdef _WIN32
if(pathname.find(":\\") != pathname.npos || pathname.find("..") != pathname.npos) if(pathname.find(":\\") != pathname.npos || pathname.find("..") != pathname.npos)
@ -2693,7 +2696,7 @@ class Parser {
if(pathname.find("/") == 0 || pathname.find("..") != pathname.npos) if(pathname.find("/") == 0 || pathname.find("..") != pathname.npos)
inja_throw("file_error", "access denied when trying to include '" + pathname + "': out of scope"); inja_throw("file_error", "access denied when trying to include '" + pathname + "': out of scope");
#endif // _WIN32 #endif // _WIN32
if(include_scope.size() && pathname.find(include_scope) != 0) if(m_config.include_scope.size() && pathname.find(m_config.include_scope) != 0)
inja_throw("file_error", "access denied when trying to include '" + pathname + "': out of scope"); inja_throw("file_error", "access denied when trying to include '" + pathname + "': out of scope");
} }
@ -2825,11 +2828,6 @@ class Parser {
return text; return text;
} }
public:
/// Extra options for limiting included files' scope
bool include_scope_limit = false;
std::string include_scope = "templates";
private: private:
const ParserConfig& m_config; const ParserConfig& m_config;
Lexer m_lexer; Lexer m_lexer;
@ -3324,7 +3322,18 @@ class Renderer {
} }
case Bytecode::Op::Exists: { case Bytecode::Op::Exists: {
auto&& name = get_args(bc)[0]->get_ref<const std::string&>(); auto&& name = get_args(bc)[0]->get_ref<const std::string&>();
bool result = (data.find(name) != data.end()); //bool result = (data.find(name) != data.end());
bool result = false;
try
{
const std::string pointer = "/" + [](std::string str){std::string new_value = "/", old_value = ".";for(std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()){if((pos = str.find(old_value, pos)) != std::string::npos)str.replace(pos, old_value.length(), new_value);else break;}return str;}(name);
data.at(json::json_pointer(pointer));
result = true;
}
catch (json::out_of_range &e)
{
result = false;
}
pop_args(bc); pop_args(bc);
m_stack.emplace_back(result); m_stack.emplace_back(result);
break; break;

View File

@ -48,10 +48,10 @@ int render_template(const std::string &content, const template_args &vars, std::
std::string data = args.at(0)->get<std::string>(); std::string data = args.at(0)->get<std::string>();
return trim(data); return trim(data);
}); });
m_parser_config.include_scope_limit = true;
m_parser_config.include_scope = include_scope;
inja::Parser parser(m_parser_config, m_lexer_config, m_included_templates); inja::Parser parser(m_parser_config, m_lexer_config, m_included_templates);
parser.include_scope_limit = true;
parser.include_scope = include_scope;
inja::Renderer renderer(m_included_templates, m_callbacks); inja::Renderer renderer(m_included_templates, m_callbacks);
try try