Mercurial > thymian
comparison templates/templater.h @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 #ifndef TEMPLATER_H | |
| 2 #define TEMPLATER_H | |
| 3 | |
| 4 #include <common.h> | |
| 5 #include <named_operator.hpp> | |
| 6 #include <json.h> | |
| 7 #include "template_struct.h" | |
| 8 #include <boost/noncopyable.hpp> | |
| 9 | |
| 10 #include <string> | |
| 11 #include <iostream> | |
| 12 #include <vector> | |
| 13 #include <set> | |
| 14 #include <map> | |
| 15 #include <sstream> | |
| 16 #include <string> | |
| 17 #include <map> | |
| 18 #include <fstream> | |
| 19 #include <memory> | |
| 20 | |
| 21 #include <sys/types.h> | |
| 22 #include <sys/stat.h> | |
| 23 | |
| 24 #ifndef _WIN32 | |
| 25 #include <unistd.h> | |
| 26 #endif | |
| 27 | |
| 28 static const std::string TEMPLATES_DIRECTORY = "theme/current/"; | |
| 29 | |
| 30 static const std::string TEMPLATE_VARIABLE_START_DELIMITER = "{#"; | |
| 31 static const std::string TEMPLATE_VARIABLE_END_DELIMITER = "}"; | |
| 32 | |
| 33 static const std::string INCLUDE_TAG = "<!--#include"; | |
| 34 static const std::string TRANSLATE_TAG = "<!--#translate"; | |
| 35 static const std::string IF_TAG = "<!--#if"; | |
| 36 static const std::string IFEQ_TAG = "<!--#eq"; | |
| 37 static const std::string ELSE_TAG = "<!--#else"; | |
| 38 static const std::string ENDIF_TAG = "<!--#endif"; | |
| 39 static const std::string ENDEQ_TAG = "<!--#endeq"; | |
| 40 static const std::string STRUCT_TAG = "<!--#struct"; | |
| 41 static const std::string PARAMETERS_TAG = "<!--#parameters"; | |
| 42 static const std::string LOOP_TAG = "<!--#loop"; | |
| 43 static const std::string ENDLOOP_TAG = "<!--#endloop"; | |
| 44 static const std::string ENDTRANSLATE_TAG = "<!--#endtranslate"; | |
| 45 static const std::string DEFINE_TAG = "<!--#define"; | |
| 46 | |
| 47 // difference between script and inline script: inline-script is execute BEFORE the variable replacements, thus | |
| 48 // it has the chance to modify the variables | |
| 49 static const std::string SCRIPT_TAG = "<!--#script"; | |
| 50 static const std::string INIT_SCRIPT_TAG = "<!--#init-script"; | |
| 51 static const std::string ENDSCRIPT_TAG = "<!--#endscript"; | |
| 52 | |
| 53 /** | |
| 54 * @brief The template_base class is the basic class for all the templates | |
| 55 */ | |
| 56 class template_base | |
| 57 { | |
| 58 public: | |
| 59 virtual ~template_base() = default; | |
| 60 virtual std::string content() const = 0; | |
| 61 virtual std::string base_dir() const = 0; | |
| 62 virtual bool check() const = 0; | |
| 63 }; | |
| 64 | |
| 65 /** | |
| 66 * @brief The file_template class is a class for a template that resides in a file | |
| 67 */ | |
| 68 class file_template : public template_base | |
| 69 { | |
| 70 public: | |
| 71 file_template() = default; | |
| 72 | |
| 73 virtual std::string fileName() const = 0; | |
| 74 std::string base_dir() const override; | |
| 75 virtual std::string content() const; | |
| 76 virtual bool check() const; | |
| 77 }; | |
| 78 | |
| 79 /** | |
| 80 * @brief The template_warehouse class is the class that keeps track of the templates that were registered | |
| 81 */ | |
| 82 class template_warehouse : public boost::noncopyable | |
| 83 { | |
| 84 public: | |
| 85 static template_warehouse& instance(); | |
| 86 | |
| 87 std::string getTemplateContent(const std::string& templateName); | |
| 88 bool registerTemplate(const std::string& name, template_base *templateClass); | |
| 89 bool checkTemplates(); | |
| 90 | |
| 91 private: | |
| 92 template_warehouse() = default; | |
| 93 std::map <std::string, std::shared_ptr<template_base>> templates; | |
| 94 }; | |
| 95 | |
| 96 #define GENERIC_TEMPLATE(Class, Type) \ | |
| 97 class Class : public file_template \ | |
| 98 { \ | |
| 99 public: \ | |
| 100 Class() = default; \ | |
| 101 static std::string name() {return std::string(#Class); } \ | |
| 102 virtual std::string fileName() const {return std::string(#Class) + "." + #Type; } \ | |
| 103 }; \ | |
| 104 [[maybe_unused]] static bool dummy##Class##Type = template_warehouse::instance().registerTemplate(#Class, new Class) | |
| 105 | |
| 106 #define GENERIC_FILE_TEMPLATE(Class, Filename) \ | |
| 107 class Class : public file_template \ | |
| 108 { \ | |
| 109 public: \ | |
| 110 Class() = default; \ | |
| 111 static std::string name() {return std::string(#Class); } \ | |
| 112 virtual std::string fileName() const {return Filename ; } \ | |
| 113 }; \ | |
| 114 [[maybe_unused]] static bool dummy##Class##Type = template_warehouse::instance().registerTemplate(#Class, new Class) | |
| 115 | |
| 116 | |
| 117 #define CSS_TEMPLATE(Class) GENERIC_TEMPLATE(Class, css) | |
| 118 #define JS_TEMPLATE(Class) GENERIC_TEMPLATE(Class, js) | |
| 119 #define TXT_TEMPLATE(Class) GENERIC_TEMPLATE(Class, txt) | |
| 120 #define HTML_TEMPLATE(Class) GENERIC_TEMPLATE(Class, html) | |
| 121 | |
| 122 #if (defined(__GNUC__) || defined(__GNUG__)) && !(defined(__clang__)) | |
| 123 #define REG_VAR(Class) [[maybe_unused]] static bool dummy##Class##Type __attribute__((unused)) = template_warehouse::instance().registerTemplate(#Class, new Class) | |
| 124 #else | |
| 125 #define REG_VAR(Class) [[gnu::unused]] static bool dummy##Class##Type = template_warehouse::instance().registerTemplate(#Class, new Class) | |
| 126 #endif | |
| 127 | |
| 128 #define STRING_TEMPLATE(Class, s) \ | |
| 129 class Class : public template_base \ | |
| 130 { \ | |
| 131 public: \ | |
| 132 Class() : m_content(s) {} \ | |
| 133 virtual ~Class() = default; \ | |
| 134 static std::string name() {return std::string(#Class); } \ | |
| 135 std::string base_dir() const override { return TEMPLATES_DIRECTORY; } \ | |
| 136 virtual std::string content() const override \ | |
| 137 { \ | |
| 138 return m_content; \ | |
| 139 } \ | |
| 140 virtual bool check() const override { return true; } \ | |
| 141 private: \ | |
| 142 std::string m_content; \ | |
| 143 }; \ | |
| 144 REG_VAR(Class) | |
| 145 | |
| 146 #define STRING_VAR_TEMPLATE(Class, s) \ | |
| 147 static std::string _TS_ ## _Class = s; \ | |
| 148 class Class : public template_base \ | |
| 149 { \ | |
| 150 public: \ | |
| 151 Class() : m_content(_TS_ ## _Class) {} \ | |
| 152 virtual ~Class() = default; \ | |
| 153 static std::string name() {return std::string(#Class); } \ | |
| 154 std::string base_dir() const override { return TEMPLATES_DIRECTORY; } \ | |
| 155 virtual std::string content() const \ | |
| 156 { \ | |
| 157 return m_content; \ | |
| 158 } \ | |
| 159 virtual bool check() const { return true; } \ | |
| 160 private: \ | |
| 161 std::string m_content; \ | |
| 162 }; \ | |
| 163 REG_VAR(Class) | |
| 164 | |
| 165 #define GENERIC_STRING_TEMPLATE(Class) \ | |
| 166 class Class : public template_base \ | |
| 167 { \ | |
| 168 public: \ | |
| 169 Class() : template_base() {} \ | |
| 170 virtual ~Class() = default; \ | |
| 171 static std::string name() {return std::string(#Class); } \ | |
| 172 std::string base_dir() const override { return TEMPLATES_DIRECTORY; } \ | |
| 173 virtual std::string content() const override \ | |
| 174 { \ | |
| 175 return m_content; \ | |
| 176 } \ | |
| 177 virtual bool check() const override { return true; } \ | |
| 178 private: \ | |
| 179 std::string m_content; \ | |
| 180 }; \ | |
| 181 REG_VAR(Class) | |
| 182 | |
| 183 | |
| 184 /** | |
| 185 * This class is the base for the template pairs of (key, value) that can be inserted in the tmeplate | |
| 186 **/ | |
| 187 class template_pair_base | |
| 188 { | |
| 189 public: | |
| 190 virtual ~template_pair_base() = default; | |
| 191 }; | |
| 192 | |
| 193 class template_special_parameter | |
| 194 { | |
| 195 public: | |
| 196 template_special_parameter() = default; | |
| 197 virtual ~template_special_parameter() = default; | |
| 198 | |
| 199 template_special_parameter(const std::string& pname, | |
| 200 const std::string& ptype, | |
| 201 bool piterable) : | |
| 202 iterable(piterable), name(pname), type(ptype) {} | |
| 203 | |
| 204 private: | |
| 205 bool iterable; | |
| 206 std::string name; | |
| 207 std::string type; | |
| 208 }; | |
| 209 | |
| 210 | |
| 211 /** | |
| 212 * This is a wrapper class for a string that is used to do the actual replacing of strings | |
| 213 * in the context of a template | |
| 214 **/ | |
| 215 class stringholder | |
| 216 { | |
| 217 public: | |
| 218 | |
| 219 stringholder(const std::string& d) : str(d) | |
| 220 {} | |
| 221 | |
| 222 void replace_all(const std::string& from, const std::string& to); | |
| 223 | |
| 224 std::string get() const | |
| 225 { | |
| 226 return str; | |
| 227 } | |
| 228 | |
| 229 stringholder& templatize(const std::map<std::string, std::string>& m); | |
| 230 | |
| 231 private: | |
| 232 | |
| 233 std::string str; | |
| 234 }; | |
| 235 | |
| 236 /** | |
| 237 * The template parameter class, holds a classical template parameter | |
| 238 **/ | |
| 239 template <class T> | |
| 240 class template_par : public template_pair_base | |
| 241 { | |
| 242 public: | |
| 243 | |
| 244 template_par(const std::string& name, const T& value) : m_key(name), m_value(unafrog::utils::to_string(value)) | |
| 245 { | |
| 246 } | |
| 247 | |
| 248 template_par(const char* name, const T& value) : template_par(std::string(name), value) | |
| 249 { | |
| 250 } | |
| 251 | |
| 252 std::string value() const | |
| 253 { | |
| 254 return m_value; | |
| 255 } | |
| 256 | |
| 257 std::string key() const | |
| 258 { | |
| 259 return m_key; | |
| 260 } | |
| 261 | |
| 262 void set_value(const std::string& v) | |
| 263 { | |
| 264 m_value = v; | |
| 265 } | |
| 266 | |
| 267 private: | |
| 268 template_pair_base* child; | |
| 269 std::string m_key; | |
| 270 std::string m_value; | |
| 271 }; | |
| 272 | |
| 273 class template_vector_par : public template_pair_base | |
| 274 { | |
| 275 public: | |
| 276 template_vector_par() = default; | |
| 277 | |
| 278 template_vector_par(const char* name, const std::vector<template_struct>& value) : mname(name), mvalue(value) | |
| 279 { | |
| 280 } | |
| 281 | |
| 282 template_vector_par(const std::string& name, const std::vector<template_struct>& value) : mname(name), mvalue(value) | |
| 283 { | |
| 284 } | |
| 285 | |
| 286 const std::vector<template_struct>& value() const | |
| 287 { | |
| 288 return mvalue; | |
| 289 } | |
| 290 | |
| 291 std::string name() const | |
| 292 { | |
| 293 return mname; | |
| 294 } | |
| 295 | |
| 296 auto begin() { return mvalue.begin(); } | |
| 297 auto end() { return mvalue.end(); } | |
| 298 std::vector<template_struct>::const_iterator begin() const { return mvalue.begin(); } | |
| 299 std::vector<template_struct>::const_iterator end() const { return mvalue.end(); } | |
| 300 | |
| 301 private: | |
| 302 std::string mname = ""; | |
| 303 std::vector<template_struct> mvalue = {}; | |
| 304 }; | |
| 305 | |
| 306 namespace op | |
| 307 { | |
| 308 struct is | |
| 309 { | |
| 310 template <typename T> | |
| 311 template_par<T> operator ()(std::string const& vs, T const& v) const | |
| 312 { | |
| 313 return template_par<T>(vs, v); | |
| 314 } | |
| 315 | |
| 316 }; | |
| 317 } | |
| 318 [[maybe_unused]] static auto is = base::make_named_operator(op::is()); | |
| 319 | |
| 320 /** | |
| 321 * @brief The templater_base class is the basic class for a templater engine | |
| 322 */ | |
| 323 class templater_base | |
| 324 { | |
| 325 | |
| 326 public: | |
| 327 virtual ~templater_base() = default; | |
| 328 | |
| 329 std::map<std::string, std::string> pairs() const | |
| 330 { | |
| 331 return kps; | |
| 332 } | |
| 333 | |
| 334 void add_structure_decl(const std::string& name, const std::string& type) | |
| 335 { | |
| 336 structures[name] = template_struct(name, type); | |
| 337 } | |
| 338 | |
| 339 template_struct& get_structure(const std::string& name) | |
| 340 { | |
| 341 return structures[name]; | |
| 342 } | |
| 343 | |
| 344 void add_parameter(const std::string& name, const std::string& type, bool iterable = false) | |
| 345 { | |
| 346 m_parameters[name] = {name, type, iterable}; | |
| 347 } | |
| 348 | |
| 349 templater_base& templatize(const template_struct& s); | |
| 350 | |
| 351 templater_base& templatize(const template_vector_par& v); | |
| 352 | |
| 353 virtual std::string get() = 0; | |
| 354 virtual void do_not_resolve_in_get() = 0; | |
| 355 virtual void do_resolve_in_get() = 0; | |
| 356 virtual std::string name() const = 0; | |
| 357 | |
| 358 void skip_whitespace(const std::string &templatized, size_t &i); | |
| 359 std::string extract_identifier_word(const std::string &input, size_t &i, std::vector<char> separators = {}, | |
| 360 std::set<char> extra_allowed_chars = {}, char &&c = 0); | |
| 361 bool check_opening_parenthesis(const std::string &input, size_t &i); | |
| 362 bool check_closing_comment(const std::string &templatized, size_t &i, std::size_t &include_tag_end_position); | |
| 363 | |
| 364 template <typename... Args> | |
| 365 void set_error(Args&&... args) | |
| 366 { | |
| 367 std::string terr = unafrog::utils::join_string(' ', std::forward<Args>(args)...); | |
| 368 error += unafrog::utils::trim(terr) + "\n"; | |
| 369 | |
| 370 } | |
| 371 | |
| 372 std::string get_error() const; | |
| 373 | |
| 374 /** | |
| 375 * @brief variables will return all the variables that can be replaced in the template | |
| 376 * @return | |
| 377 */ | |
| 378 std::vector<std::string> variables(bool resolve_includes_too); | |
| 379 | |
| 380 | |
| 381 protected: | |
| 382 | |
| 383 std::string get(const std::string& template_name); | |
| 384 void resolve_all_includes(std::string &templatized, bool do_replace = true); | |
| 385 | |
| 386 /** | |
| 387 * @brief resolve_includes Resolves the template including other templates. | |
| 388 * | |
| 389 * The included template name can have parameters too: <!--#include IncludeableTemplate(str="blaa")#-->" meaning | |
| 390 * that when the IncludeableTemplate is included its variables (specified in the list only) are exchhanged to the | |
| 391 * ones with values from the parameters. | |
| 392 * | |
| 393 * @param templatized - the actual content of the template string | |
| 394 * @param inc_pos - the start position | |
| 395 * @param do_variable_replacement - whether wer should replace the variables after including | |
| 396 * | |
| 397 * @return the templated string, where the includes were resolved and eventually the variables were replaced | |
| 398 */ | |
| 399 std::string resolve_includes(std::string templatized, size_t inc_pos, bool do_variable_replacement = true); | |
| 400 | |
| 401 std::string resolve_structure_declaration(size_t struct_pos, std::string templatized); | |
| 402 std::string resolve_parameters(size_t parameters_pos, std::string templatized); | |
| 403 std::string resolve_ifs(size_t if_pos, std::string templatized); | |
| 404 std::string resolve_ifeq(size_t if_pos, std::string templatized); | |
| 405 std::string resolve_defines(std::string content); | |
| 406 std::string resolve_script(size_t pos, const std::string &content, const std::string& tag); | |
| 407 std::string resolve_scripts(std::string templatized, const std::string& tag); | |
| 408 std::string resolve_loops(std::string templatized, const template_vector_par &v); | |
| 409 std::string resolve_ifeqs(std::string templatized); | |
| 410 | |
| 411 /** | |
| 412 * @brief resolve_translations resolves all the translations | |
| 413 * @param templatized | |
| 414 * @param target_language | |
| 415 * @param generate_span - whether to generate HTML spans for the translated elements | |
| 416 * @param translations - a map with the span ID's and their corresponding translations | |
| 417 * @return | |
| 418 */ | |
| 419 std::string resolve_translations(std::string templatized, const std::string& target_language, bool generate_span, std::map<std::string, std::map<std::string, std::string> > &translations); | |
| 420 | |
| 421 /** | |
| 422 * @brief resolve_translation resolves one translation | |
| 423 * @param if_pos | |
| 424 * @param templatized | |
| 425 * @param target_language | |
| 426 * @param generate_span - whether to generate a span HTML element for this translation | |
| 427 * @param span_id - this will be the ID of the span, generated for this translation, used by the javascript at a later stage to set the language upon change | |
| 428 * @param translations - a map of translations for this given translation, for each language (key) the corresponding translated text (value) | |
| 429 * @return | |
| 430 */ | |
| 431 std::string resolve_translation(size_t if_pos, std::string templatized, const std::string& target_language, bool generate_span, std::string& span_id, std::map<std::string, std::string> &translations); | |
| 432 std::string resolve_dynamic_section(std::string templatized, const template_vector_par &v); | |
| 433 | |
| 434 protected: | |
| 435 | |
| 436 // the string representation of the key/value pairs of the template that will be worked with | |
| 437 std::map<std::string, std::string> kps; | |
| 438 | |
| 439 // the structures that are defined in this template are populated from this map of key/structure paris | |
| 440 std::map<std::string, template_struct> structures; | |
| 441 | |
| 442 // the parameters of te template | |
| 443 std::map<std::string, template_special_parameter> m_parameters; | |
| 444 | |
| 445 // working data | |
| 446 std::string precalculated = ""; | |
| 447 std::string error = ""; | |
| 448 }; | |
| 449 | |
| 450 template<class T> | |
| 451 class templater : public templater_base | |
| 452 { | |
| 453 protected: | |
| 454 bool resolve_in_get = true; | |
| 455 bool external_content_set = false; | |
| 456 std::string external_content = ""; | |
| 457 std::map<std::string, std::map<std::string, std::string> > m_translation_map; | |
| 458 public: | |
| 459 | |
| 460 const std::map<std::string, std::map<std::string, std::string> >& get_translations() | |
| 461 { | |
| 462 return m_translation_map; | |
| 463 } | |
| 464 | |
| 465 templater& templatize() | |
| 466 { | |
| 467 return *this; | |
| 468 } | |
| 469 | |
| 470 templater& templatize(const template_vector_par& v) | |
| 471 { | |
| 472 return dynamic_cast<templater&>(templater_base::templatize(v)); | |
| 473 } | |
| 474 | |
| 475 template <typename T1> | |
| 476 templater& templatize(const template_par<T1>& b) | |
| 477 { | |
| 478 precalculated = ""; | |
| 479 kps.insert(make_pair(b.key(), unafrog::utils::to_string(b.value()))); | |
| 480 | |
| 481 return *this; | |
| 482 } | |
| 483 | |
| 484 templater& templatize(const nlohmann::json& j) | |
| 485 { | |
| 486 precalculated = ""; | |
| 487 for (const auto& [k, v] : j.items()) | |
| 488 { | |
| 489 if(v.is_string()) | |
| 490 { | |
| 491 std::string s = unafrog::utils::to_string(v); | |
| 492 remove_quotes(s); | |
| 493 kps.insert(make_pair(unafrog::utils::to_string(k), s)); | |
| 494 } | |
| 495 } | |
| 496 return *this; | |
| 497 } | |
| 498 | |
| 499 templater& templatize(const template_struct& s) | |
| 500 { | |
| 501 return dynamic_cast<templater&>(templater_base::templatize(s)); | |
| 502 } | |
| 503 | |
| 504 template<typename... Args1> | |
| 505 templater& templatize(const template_struct& s, Args1... args) | |
| 506 { | |
| 507 precalculated = ""; | |
| 508 templatize(s); | |
| 509 return templatize(args...); | |
| 510 } | |
| 511 | |
| 512 template<typename... Args1> | |
| 513 templater& templatize(const template_vector_par& v, Args1... args) | |
| 514 { | |
| 515 dynamic_cast<templater&>(templater_base::templatize(v)); | |
| 516 return templatize(args...); | |
| 517 } | |
| 518 | |
| 519 template<typename T1, typename... Args1> | |
| 520 templater& templatize(const template_par<T1>& first, Args1... args) | |
| 521 { | |
| 522 precalculated = ""; | |
| 523 kps.insert(make_pair(first.key(), unafrog::utils::to_string(first.value()))); | |
| 524 return templatize(args...); | |
| 525 } | |
| 526 | |
| 527 void do_not_resolve_in_get() override | |
| 528 { | |
| 529 resolve_in_get = false; | |
| 530 } | |
| 531 void do_resolve_in_get() override | |
| 532 { | |
| 533 resolve_in_get = true; | |
| 534 } | |
| 535 | |
| 536 templater& set(const std::string& ec) | |
| 537 { | |
| 538 external_content = ec; | |
| 539 external_content_set = true; | |
| 540 return *this; | |
| 541 } | |
| 542 | |
| 543 std::string get() override | |
| 544 { | |
| 545 if(precalculated.empty()) | |
| 546 { | |
| 547 std::string templatized = (external_content_set && external_content.size() > 0) ? external_content : templater_base::get(name()); | |
| 548 if(resolve_in_get) | |
| 549 { | |
| 550 templatized = resolve_ifeqs(templatized); | |
| 551 templatized = resolve_scripts(templatized, SCRIPT_TAG); | |
| 552 precalculated = templatized; | |
| 553 } | |
| 554 else | |
| 555 { | |
| 556 precalculated = templatized; | |
| 557 } | |
| 558 | |
| 559 if(kps.count("target_language")) | |
| 560 { | |
| 561 precalculated = resolve_translations(precalculated, kps["target_language"], true, m_translation_map); | |
| 562 } | |
| 563 | |
| 564 return precalculated; | |
| 565 } | |
| 566 else | |
| 567 { | |
| 568 return precalculated; | |
| 569 } | |
| 570 } | |
| 571 | |
| 572 std::string name() const override | |
| 573 { | |
| 574 return T::name(); | |
| 575 } | |
| 576 | |
| 577 | |
| 578 }; | |
| 579 | |
| 580 template <class T> | |
| 581 std::ostream& operator << (std::ostream& os, const templater<T>& t) | |
| 582 { | |
| 583 os << t.get(); | |
| 584 return os; | |
| 585 } | |
| 586 | |
| 587 | |
| 588 template<class T> | |
| 589 struct translator : public templater<T> | |
| 590 { | |
| 591 translator& templatize() | |
| 592 { | |
| 593 return *this; | |
| 594 } | |
| 595 | |
| 596 translator& templatize(const template_vector_par& v) | |
| 597 { | |
| 598 return dynamic_cast<translator&>(templater_base::templatize(v)); | |
| 599 } | |
| 600 | |
| 601 template <typename T1> | |
| 602 translator& templatize(const template_par<T1>& b) | |
| 603 { | |
| 604 templater<T>::precalculated = ""; | |
| 605 templater<T>::kps.insert(make_pair(b.key(), unafrog::utils::to_string(b.value()))); | |
| 606 | |
| 607 return *this; | |
| 608 } | |
| 609 | |
| 610 translator& templatize(const nlohmann::json& j) | |
| 611 { | |
| 612 templater<T>::precalculated = ""; | |
| 613 for (const auto& [k, v] : j.items()) | |
| 614 { | |
| 615 if(v.is_string()) | |
| 616 { | |
| 617 std::string s = unafrog::utils::to_string(v); | |
| 618 remove_quotes(s); | |
| 619 templater<T>::kps.insert(make_pair(unafrog::utils::to_string(k), s)); | |
| 620 } | |
| 621 } | |
| 622 return *this; | |
| 623 } | |
| 624 | |
| 625 translator& templatize(const template_struct& s) | |
| 626 { | |
| 627 return dynamic_cast<translator&>(templater_base::templatize(s)); | |
| 628 } | |
| 629 | |
| 630 template<typename... Args1> | |
| 631 translator& templatize(const template_struct& s, Args1... args) | |
| 632 { | |
| 633 templater<T>::precalculated = ""; | |
| 634 templatize(s); | |
| 635 return templatize(args...); | |
| 636 } | |
| 637 | |
| 638 template<typename T1, typename... Args1> | |
| 639 translator& templatize(const template_par<T1>& first, Args1... args) | |
| 640 { | |
| 641 templater<T>::precalculated = ""; | |
| 642 templater<T>::kps.insert(make_pair(first.key(), unafrog::utils::to_string(first.value()))); | |
| 643 return templatize(args...); | |
| 644 } | |
| 645 | |
| 646 translator& set() | |
| 647 { | |
| 648 templater<T>::get(); | |
| 649 templater<T>::external_content = templater<T>::precalculated; | |
| 650 templater<T>::external_content_set = true; | |
| 651 return *this; | |
| 652 } | |
| 653 | |
| 654 | |
| 655 std::string translate(const std::string& target_language) | |
| 656 { | |
| 657 if(templater<T>::external_content_set && templater<T>::external_content.length() > 0) | |
| 658 { | |
| 659 return translate(templater<T>::external_content, target_language, this->m_translation_map); | |
| 660 } | |
| 661 else | |
| 662 { | |
| 663 std::string s = templater<T>().templatize().get(); | |
| 664 return translate(s, target_language, this->m_translation_map); | |
| 665 } | |
| 666 } | |
| 667 | |
| 668 static std::string translate(const std::string& in, const std::string& target_language, std::map<std::string, std::map<std::string, std::string> >& translation_map) | |
| 669 { | |
| 670 //TODO mutex | |
| 671 GENERIC_STRING_TEMPLATE(Translateable); | |
| 672 auto t = templater<Translateable>(); | |
| 673 std::string res = t.set(in).templatize("target_language" <is> target_language).get(); | |
| 674 translation_map = t.get_translations(); | |
| 675 return res; | |
| 676 } | |
| 677 }; | |
| 678 | |
| 679 HTML_TEMPLATE(mainpage); | |
| 680 HTML_TEMPLATE(category_list); | |
| 681 HTML_TEMPLATE(recipe); | |
| 682 HTML_TEMPLATE(footer); | |
| 683 HTML_TEMPLATE(languages); | |
| 684 | |
| 685 #endif // TEMPLATER_H |
