mirror of
https://github.com/pantor/inja.git
synced 2026-08-03 06:12:16 +00:00
code style (cpplint)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#ifndef PANTOR_INJA_BYTECODE_HPP
|
||||
#define PANTOR_INJA_BYTECODE_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_BYTECODE_HPP_
|
||||
#define INCLUDE_INJA_BYTECODE_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
@@ -10,7 +13,7 @@
|
||||
|
||||
namespace inja {
|
||||
|
||||
using namespace nlohmann;
|
||||
using json = nlohmann::json;
|
||||
|
||||
|
||||
struct Bytecode {
|
||||
@@ -128,4 +131,4 @@ struct Bytecode {
|
||||
|
||||
} // namespace inja
|
||||
|
||||
#endif // PANTOR_INJA_BYTECODE_HPP
|
||||
#endif // INCLUDE_INJA_BYTECODE_HPP_
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef PANTOR_INJA_CONFIG_HPP
|
||||
#define PANTOR_INJA_CONFIG_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_CONFIG_HPP_
|
||||
#define INCLUDE_INJA_CONFIG_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
@@ -54,6 +56,6 @@ struct ParserConfig {
|
||||
ElementNotation notation {ElementNotation::Dot};
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace inja
|
||||
|
||||
#endif // PANTOR_INJA_CONFIG_HPP
|
||||
#endif // INCLUDE_INJA_CONFIG_HPP_
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef PANTOR_INJA_ENVIRONMENT_HPP
|
||||
#define PANTOR_INJA_ENVIRONMENT_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_ENVIRONMENT_HPP_
|
||||
#define INCLUDE_INJA_ENVIRONMENT_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
@@ -20,7 +22,7 @@
|
||||
|
||||
namespace inja {
|
||||
|
||||
using namespace nlohmann;
|
||||
using json = nlohmann::json;
|
||||
|
||||
/*!
|
||||
* \brief Class for changing the configuration.
|
||||
@@ -103,8 +105,8 @@ class Environment {
|
||||
|
||||
Template parse_template(const std::string& filename) {
|
||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
||||
return parser.parse_template(m_impl->input_path + static_cast<std::string>(filename));
|
||||
}
|
||||
return parser.parse_template(m_impl->input_path + static_cast<std::string>(filename));
|
||||
}
|
||||
|
||||
std::string render(nonstd::string_view input, const json& data) {
|
||||
return render(parse(input), data);
|
||||
@@ -117,35 +119,35 @@ class Environment {
|
||||
}
|
||||
|
||||
std::string render_file(const std::string& filename, const json& data) {
|
||||
return render(parse_template(filename), data);
|
||||
}
|
||||
return render(parse_template(filename), data);
|
||||
}
|
||||
|
||||
std::string render_file_with_json_file(const std::string& filename, const std::string& filename_data) {
|
||||
const json data = load_json(filename_data);
|
||||
return render_file(filename, data);
|
||||
}
|
||||
const json data = load_json(filename_data);
|
||||
return render_file(filename, data);
|
||||
}
|
||||
|
||||
void write(const std::string& filename, const json& data, const std::string& filename_out) {
|
||||
std::ofstream file(m_impl->output_path + filename_out);
|
||||
file << render_file(filename, data);
|
||||
file.close();
|
||||
}
|
||||
std::ofstream file(m_impl->output_path + filename_out);
|
||||
file << render_file(filename, data);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void write(const Template& temp, const json& data, const std::string& filename_out) {
|
||||
std::ofstream file(m_impl->output_path + filename_out);
|
||||
file << render(temp, data);
|
||||
file.close();
|
||||
}
|
||||
std::ofstream file(m_impl->output_path + filename_out);
|
||||
file << render(temp, data);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void write_with_json_file(const std::string& filename, const std::string& filename_data, const std::string& filename_out) {
|
||||
const json data = load_json(filename_data);
|
||||
write(filename, data, filename_out);
|
||||
}
|
||||
void write_with_json_file(const std::string& filename, const std::string& filename_data, const std::string& filename_out) {
|
||||
const json data = load_json(filename_data);
|
||||
write(filename, data, filename_out);
|
||||
}
|
||||
|
||||
void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) {
|
||||
const json data = load_json(filename_data);
|
||||
write(temp, data, filename_out);
|
||||
}
|
||||
void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) {
|
||||
const json data = load_json(filename_data);
|
||||
write(temp, data, filename_out);
|
||||
}
|
||||
|
||||
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
|
||||
Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data);
|
||||
@@ -154,15 +156,15 @@ class Environment {
|
||||
|
||||
std::string load_file(const std::string& filename) {
|
||||
Parser parser(m_impl->parser_config, m_impl->lexer_config, m_impl->included_templates);
|
||||
return parser.load_file(m_impl->input_path + filename);
|
||||
}
|
||||
return parser.load_file(m_impl->input_path + filename);
|
||||
}
|
||||
|
||||
json load_json(const std::string& filename) {
|
||||
std::ifstream file = open_file_or_throw(m_impl->input_path + filename);
|
||||
json j;
|
||||
file >> j;
|
||||
return j;
|
||||
}
|
||||
std::ifstream file = open_file_or_throw(m_impl->input_path + filename);
|
||||
json j;
|
||||
file >> j;
|
||||
return j;
|
||||
}
|
||||
|
||||
void add_callback(const std::string& name, unsigned int numArgs, const CallbackFunction& callback) {
|
||||
m_impl->callbacks.add_callback(name, numArgs, callback);
|
||||
@@ -194,4 +196,4 @@ inline void render_to(std::ostream& os, nonstd::string_view input, const json& d
|
||||
|
||||
}
|
||||
|
||||
#endif // PANTOR_INJA_ENVIRONMENT_HPP
|
||||
#endif // INCLUDE_INJA_ENVIRONMENT_HPP_
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#ifndef PANTOR_INJA_FUNCTION_STORAGE_HPP
|
||||
#define PANTOR_INJA_FUNCTION_STORAGE_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||
#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "bytecode.hpp"
|
||||
#include "string_view.hpp"
|
||||
@@ -7,7 +11,7 @@
|
||||
|
||||
namespace inja {
|
||||
|
||||
using namespace nlohmann;
|
||||
using json = nlohmann::json;
|
||||
|
||||
using Arguments = std::vector<const json*>;
|
||||
using CallbackFunction = std::function<json(Arguments& args)>;
|
||||
@@ -72,4 +76,4 @@ class FunctionStorage {
|
||||
|
||||
}
|
||||
|
||||
#endif // PANTOR_INJA_FUNCTION_STORAGE_HPP
|
||||
#endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef PANTOR_INJA_HPP
|
||||
#define PANTOR_INJA_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_INJA_HPP_
|
||||
#define INCLUDE_INJA_INJA_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
@@ -18,4 +20,4 @@
|
||||
#include "renderer.hpp"
|
||||
|
||||
|
||||
#endif // PANTOR_INJA_HPP
|
||||
#endif // INCLUDE_INJA_INJA_HPP_
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#ifndef PANTOR_INJA_LEXER_HPP
|
||||
#define PANTOR_INJA_LEXER_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_LEXER_HPP_
|
||||
#define INCLUDE_INJA_LEXER_HPP_
|
||||
|
||||
#include <cctype>
|
||||
#include <locale>
|
||||
@@ -75,7 +77,7 @@ class Lexer {
|
||||
inja::string_view::starts_with(open_str, m_config.line_statement)) {
|
||||
m_state = State::LineStart;
|
||||
} else {
|
||||
m_pos += 1; // wasn't actually an opening sequence
|
||||
m_pos += 1; // wasn't actually an opening sequence
|
||||
goto again;
|
||||
}
|
||||
|
||||
@@ -303,4 +305,4 @@ class Lexer {
|
||||
|
||||
}
|
||||
|
||||
#endif // PANTOR_INJA_LEXER_HPP
|
||||
#endif // INCLUDE_INJA_LEXER_HPP_
|
||||
|
||||
+12
-7
@@ -1,7 +1,12 @@
|
||||
#ifndef PANTOR_INJA_PARSER_HPP
|
||||
#define PANTOR_INJA_PARSER_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_PARSER_HPP_
|
||||
#define INCLUDE_INJA_PARSER_HPP_
|
||||
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "bytecode.hpp"
|
||||
#include "config.hpp"
|
||||
@@ -510,10 +515,10 @@ class Parser {
|
||||
}
|
||||
|
||||
std::string load_file(nonstd::string_view filename) {
|
||||
std::ifstream file = open_file_or_throw(static_cast<std::string>(filename));
|
||||
std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
return text;
|
||||
}
|
||||
std::ifstream file = open_file_or_throw(static_cast<std::string>(filename));
|
||||
std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
return text;
|
||||
}
|
||||
|
||||
private:
|
||||
const ParserConfig& m_config;
|
||||
@@ -553,4 +558,4 @@ class Parser {
|
||||
|
||||
} // namespace inja
|
||||
|
||||
#endif // PANTOR_INJA_PARSER_HPP
|
||||
#endif // INCLUDE_INJA_PARSER_HPP_
|
||||
|
||||
+33
-29
@@ -1,50 +1,54 @@
|
||||
#ifndef PANTOR_INJA_POLYFILL_HPP
|
||||
#define PANTOR_INJA_POLYFILL_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_POLYFILL_HPP_
|
||||
#define INCLUDE_INJA_POLYFILL_HPP_
|
||||
|
||||
|
||||
#if __cplusplus < 201402L
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace stdinja {
|
||||
template<class T> struct _Unique_if {
|
||||
typedef std::unique_ptr<T> _Single_object;
|
||||
};
|
||||
|
||||
template<class T> struct _Unique_if<T[]> {
|
||||
typedef std::unique_ptr<T[]> _Unknown_bound;
|
||||
};
|
||||
template<class T> struct _Unique_if {
|
||||
typedef std::unique_ptr<T> _Single_object;
|
||||
};
|
||||
|
||||
template<class T, size_t N> struct _Unique_if<T[N]> {
|
||||
typedef void _Known_bound;
|
||||
};
|
||||
template<class T> struct _Unique_if<T[]> {
|
||||
typedef std::unique_ptr<T[]> _Unknown_bound;
|
||||
};
|
||||
|
||||
template<class T, class... Args>
|
||||
typename _Unique_if<T>::_Single_object
|
||||
make_unique(Args&&... args) {
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
template<class T, size_t N> struct _Unique_if<T[N]> {
|
||||
typedef void _Known_bound;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
typename _Unique_if<T>::_Unknown_bound
|
||||
make_unique(size_t n) {
|
||||
typedef typename std::remove_extent<T>::type U;
|
||||
return std::unique_ptr<T>(new U[n]());
|
||||
}
|
||||
|
||||
template<class T, class... Args>
|
||||
typename _Unique_if<T>::_Known_bound
|
||||
make_unique(Args&&...) = delete;
|
||||
template<class T, class... Args>
|
||||
typename _Unique_if<T>::_Single_object
|
||||
make_unique(Args&&... args) {
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename _Unique_if<T>::_Unknown_bound
|
||||
make_unique(size_t n) {
|
||||
typedef typename std::remove_extent<T>::type U;
|
||||
return std::unique_ptr<T>(new U[n]());
|
||||
}
|
||||
|
||||
template<class T, class... Args>
|
||||
typename _Unique_if<T>::_Known_bound
|
||||
make_unique(Args&&...) = delete;
|
||||
|
||||
} // namespace stdinja
|
||||
|
||||
#else
|
||||
|
||||
namespace stdinja = std;
|
||||
|
||||
#endif // memory */
|
||||
#endif // memory */
|
||||
|
||||
|
||||
#endif // PANTOR_INJA_POLYFILL_HPP
|
||||
#endif // INCLUDE_INJA_POLYFILL_HPP_
|
||||
|
||||
+17
-13
@@ -1,8 +1,13 @@
|
||||
#ifndef PANTOR_INJA_RENDERER_HPP
|
||||
#define PANTOR_INJA_RENDERER_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_RENDERER_HPP_
|
||||
#define INCLUDE_INJA_RENDERER_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
@@ -113,7 +118,7 @@ class Renderer {
|
||||
LoopLevel& level = m_loop_stack.back();
|
||||
|
||||
if (level.loop_type == LoopLevel::Type::Array) {
|
||||
level.data[static_cast<std::string>(level.value_name)] = level.values.at(level.index); // *level.it;
|
||||
level.data[static_cast<std::string>(level.value_name)] = level.values.at(level.index); // *level.it;
|
||||
auto& loopData = level.data["loop"];
|
||||
loopData["index"] = level.index;
|
||||
loopData["index1"] = level.index + 1;
|
||||
@@ -135,22 +140,21 @@ class Renderer {
|
||||
enum class Type { Map, Array };
|
||||
|
||||
Type loop_type;
|
||||
nonstd::string_view key_name; // variable name for keys
|
||||
nonstd::string_view value_name; // variable name for values
|
||||
json data; // data with loop info added
|
||||
nonstd::string_view key_name; // variable name for keys
|
||||
nonstd::string_view value_name; // variable name for values
|
||||
json data; // data with loop info added
|
||||
|
||||
json values; // values to iterate over
|
||||
json values; // values to iterate over
|
||||
|
||||
// loop over list
|
||||
size_t index; // current list index
|
||||
size_t size; // length of list
|
||||
size_t index; // current list index
|
||||
size_t size; // length of list
|
||||
|
||||
// loop over map
|
||||
using KeyValue = std::pair<nonstd::string_view, json*>;
|
||||
using MapValues = std::vector<KeyValue>;
|
||||
MapValues map_values; // values to iterate over
|
||||
MapValues::iterator map_it; // iterator over values
|
||||
|
||||
MapValues map_values; // values to iterate over
|
||||
MapValues::iterator map_it; // iterator over values
|
||||
};
|
||||
|
||||
std::vector<LoopLevel> m_loop_stack;
|
||||
@@ -573,4 +577,4 @@ class Renderer {
|
||||
|
||||
} // namespace inja
|
||||
|
||||
#endif // PANTOR_INJA_RENDERER_HPP
|
||||
#endif // INCLUDE_INJA_RENDERER_HPP_
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#ifndef PANTOR_INJA_TEMPLATE_HPP
|
||||
#define PANTOR_INJA_TEMPLATE_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_TEMPLATE_HPP_
|
||||
#define INCLUDE_INJA_TEMPLATE_HPP_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -19,6 +22,6 @@ struct Template {
|
||||
|
||||
using TemplateStorage = std::map<std::string, Template>;
|
||||
|
||||
}
|
||||
} // namespace inja
|
||||
|
||||
#endif // PANTOR_INJA_TEMPLATE_HPP
|
||||
#endif // INCLUDE_INJA_TEMPLATE_HPP_
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#ifndef PANTOR_INJA_TOKEN_HPP
|
||||
#define PANTOR_INJA_TOKEN_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_TOKEN_HPP_
|
||||
#define INCLUDE_INJA_TOKEN_HPP_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "string_view.hpp"
|
||||
|
||||
@@ -62,4 +66,4 @@ struct Token {
|
||||
|
||||
}
|
||||
|
||||
#endif // PANTOR_INJA_TOKEN_HPP
|
||||
#endif // INCLUDE_INJA_TOKEN_HPP_
|
||||
|
||||
+10
-5
@@ -1,8 +1,13 @@
|
||||
#ifndef PANTOR_INJA_UTILS_HPP
|
||||
#define PANTOR_INJA_UTILS_HPP
|
||||
// Copyright (c) 2019 Pantor. All rights reserved.
|
||||
|
||||
#ifndef INCLUDE_INJA_UTILS_HPP_
|
||||
#define INCLUDE_INJA_UTILS_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "string_view.hpp"
|
||||
|
||||
@@ -28,7 +33,7 @@ namespace string_view {
|
||||
inline nonstd::string_view slice(nonstd::string_view view, size_t start, size_t end) {
|
||||
start = std::min(start, view.size());
|
||||
end = std::min(std::max(start, end), view.size());
|
||||
return view.substr(start, end - start); // StringRef(Data + Start, End - Start);
|
||||
return view.substr(start, end - start); // StringRef(Data + Start, End - Start);
|
||||
}
|
||||
|
||||
inline std::pair<nonstd::string_view, nonstd::string_view> split(nonstd::string_view view, char Separator) {
|
||||
@@ -42,8 +47,8 @@ namespace string_view {
|
||||
inline bool starts_with(nonstd::string_view view, nonstd::string_view prefix) {
|
||||
return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0);
|
||||
}
|
||||
} // namespace string
|
||||
} // namespace string_view
|
||||
|
||||
} // namespace inja
|
||||
|
||||
#endif // PANTOR_INJA_UTILS_HPP
|
||||
#endif // INCLUDE_INJA_UTILS_HPP_
|
||||
|
||||
Reference in New Issue
Block a user