WikiGraph::WikiPage& make_wiki_page(string path_to_html, string path_to_txt) { auto toRet = new WikiGraph::WikiPage; WikiGraph::WikiPage& new_page = *toRet; //get reference to the newly created object ifstream html_file(path_to_html.c_str()); //open html file new_page.title = getPageTitle(html_file); // use already existing method to get title html_file.close(); new_page.txt_location = path_to_txt; //save the path to the txt file new_page.html_location = path_to_html; //save the path to the html file return new_page; }
unsigned PageAsJson::operator() (tnt::HttpRequest& request, tnt::HttpReply& reply, tnt::QueryParams& qparam) { std::string content = request.getArg("content"); std::string contentOutput; unsigned ret = HTTP_INTERNAL_SERVER_ERROR; // handle all pages that mustn't be called by url here if(content == "page" || content == "page.json" || content == "404" || content == "500") { contentOutput = scallComp("404", request); ret = HTTP_NOT_FOUND; } else { try { // call content component contentOutput = scallComp(content, request, qparam); ret = HTTP_OK; } catch(tnt::NotFoundException&) { contentOutput = scallComp("404", request); ret = HTTP_NOT_FOUND; } catch(std::runtime_error& e) { tnt::QueryParams errorParam; errorParam.add("errorMessage", e.what()); contentOutput = scallComp("500", request, errorParam); ret = HTTP_INTERNAL_SERVER_ERROR; } } Json::Value replyContent; Json::FastWriter jsonWriter; replyContent["content"] = contentOutput; replyContent["title"] = getPageTitle(request); reply.out() << jsonWriter.write(replyContent); reply.setContentType("application/json"); return ret; }
//Creates a new WikiPage from HTML and text source files WikiGraph::WikiPage& make_wiki_page(string path_to_html, string path_to_txt){ //Dynamically allocate memory for the WikiPage. Remember to delete in destructor WikiGraph::WikiPage *page = new WikiGraph::WikiPage; WikiGraph::WikiPage &pageRef = *page; page->ID = 0; //Open a file stream to read the page title from the HTML source ifstream ffi; ffi.open(path_to_html); //Find the title of the page using the input stream page->title = getPageTitle(ffi); ffi.close(); //Set the file paths page->html_location = path_to_html; page->txt_location = path_to_txt; return pageRef; }