ferencd@0: #include "r_impl.h" ferencd@0: #include "templater.h" ferencd@0: #include "url_breaker.h" ferencd@0: #include "logger.h" ferencd@0: #include "json.h" ferencd@0: #include ferencd@0: #include "maddy/parser.h" ferencd@0: ferencd@0: #include ferencd@0: #include ferencd@0: ferencd@0: std::string r_impl::getMd(const std::string& key, const std::string &f, const std::string& cCurrentPath) ferencd@0: { ferencd@0: std::string filenameDesc(cCurrentPath); ferencd@0: filenameDesc += RECIPES_ROOT + key + "/" + m_lang + "/" + f; ferencd@0: ferencd@0: std::ifstream finIntro(filenameDesc.c_str(), std::ios::in | std::ios::binary); ferencd@0: if(finIntro) ferencd@0: { ferencd@0: std::ostringstream ossIntro; ferencd@0: ossIntro << finIntro.rdbuf(); ferencd@0: std::string intro(ossIntro.str()); ferencd@0: std::stringstream markdownInput(intro); ferencd@0: ferencd@0: // config is optional ferencd@0: std::shared_ptr config = std::make_shared(); ferencd@0: config->isEmphasizedParserEnabled = true; // default ferencd@0: config->isHTMLWrappedInParagraph = true; // default ferencd@0: ferencd@0: std::shared_ptr parser = std::make_shared(config); ferencd@0: return parser->Parse(markdownInput); ferencd@0: } ferencd@0: ferencd@0: return ""; ferencd@0: } ferencd@0: ferencd@0: r_impl::r_impl(tnt::HttpRequest &request, tnt::HttpReply &reply, const std::string &what) : web_component(request, reply, "") ferencd@0: { ferencd@0: url_breaker r("r/key", boost::to_lower_copy(what)); ferencd@0: m_key = r["key"]; ferencd@0: ferencd@0: // identify the language ferencd@0: auto pars = request.getQueryParams(); ferencd@0: m_lang = pars.arg("l"); ferencd@0: ferencd@0: char cCurrentPath[FILENAME_MAX] = {0}; ferencd@0: ferencd@0: if (!getcwd(cCurrentPath, sizeof(cCurrentPath))) ferencd@0: { ferencd@0: log_err() << "Cannot get current path"; ferencd@0: return; ferencd@0: } ferencd@0: ferencd@0: std::string filename(cCurrentPath); ferencd@0: filename += RECIPES_ROOT + "/" + m_key + "/descriptor.json"; ferencd@0: bool found_lang = false; ferencd@0: ferencd@0: { ferencd@0: std::ifstream fin(filename.c_str(), std::ios::in | std::ios::binary); ferencd@0: if(fin) ferencd@0: { ferencd@0: std::ostringstream oss; ferencd@0: oss << fin.rdbuf(); ferencd@0: std::string fileData(oss.str()); ferencd@0: auto j = nlohmann::json::parse(fileData); ferencd@0: auto langs = j["lang"]; ferencd@0: for(const auto& l : langs) ferencd@0: { ferencd@0: std::string cpp_string; ferencd@0: l.get_to(cpp_string); ferencd@0: if(cpp_string == m_lang) ferencd@0: { ferencd@0: found_lang = true; ferencd@0: break; ferencd@0: } ferencd@0: } ferencd@0: } ferencd@0: } ferencd@0: if(!found_lang) ferencd@0: { ferencd@0: log_err() << "Cannot find the language " << m_lang << " for " << m_key; ferencd@0: return; ferencd@0: } ferencd@0: ferencd@0: m_intro = getMd(m_key, "intro.md", cCurrentPath); ferencd@0: m_body = getMd(m_key, "recipe.md", cCurrentPath); ferencd@0: ferencd@0: // fix the image, if it contains {#rroot} ferencd@0: stringholder sh(m_body); ferencd@0: sh.replace_all("{#rroot}", RECIPES_ROOT + m_key + SLASH); ferencd@0: sh.replace_all("//", "/"); ferencd@0: m_body = sh.get(); ferencd@0: ferencd@0: m_body = "" + m_body; ferencd@0: ferencd@0: } ferencd@0: ferencd@0: unsigned r_impl::send() ferencd@0: { ferencd@0: std::string stype = ""; ferencd@0: try { ferencd@0: ferencd@0: tntdb::Connection conn = tntdb::connect("sqlite:lang.db"); ferencd@0: ferencd@0: std::string v = std::string("select type from food where food_key='") + m_key + "'"; ferencd@0: tntdb::Result result = conn.select(v); ferencd@0: for (tntdb::Result::const_iterator it = result.begin(); it != result.end(); ++it) ferencd@0: { ferencd@0: std::string type; ferencd@0: std::string key; ferencd@0: tntdb::Row row = *it; ferencd@0: ferencd@0: row[0].get(type); ferencd@0: ferencd@0: if(ctg_to_name.count(type)) ferencd@0: { ferencd@0: stype = ctg_to_name[type]; ferencd@0: } ferencd@0: else ferencd@0: { ferencd@0: return HTTP_NOT_FOUND; ferencd@0: } ferencd@0: } ferencd@0: } ferencd@0: catch (std::exception& ex) ferencd@0: { ferencd@0: log_critical() << ex.what(); ferencd@0: return HTTP_NOT_FOUND; ferencd@0: } ferencd@0: ferencd@0: prepareLanguages(); ferencd@0: template_vector_par tvp_languages("languages", m_languageStructs); ferencd@0: ferencd@0: std::string title = dictionary::translate(m_key + "_name", m_lang); ferencd@0: std::string rply = templater().templatize("intro" m_intro, ferencd@0: "body" m_body, ferencd@0: "title" title, ferencd@0: "key" m_key, ferencd@0: "lng" m_lang, ferencd@0: "category" stype).templatize(tvp_languages).get(); ferencd@0: ferencd@0: std::map > translations; ferencd@0: mreply.out() << translator::translate(rply, m_lang, translations); ferencd@0: ferencd@0: ferencd@0: return HTTP_OK; ferencd@0: }