1 Star 0 Fork 0

iamdsy / nginx-blog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
blog_home_page.cpp 38.58 KB
一键复制 编辑 原始数据 按行查看 历史
iamdsy 提交于 2017-12-27 22:09 . fix-g++5.4.0-compile
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
extern "C"{
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
}
#include "blog_home_page.h"
#include "lib_markdown_parse.h"
#include <fstream>
#include <sstream>
#include <utility>
string strim_string(string before){
before = before.erase(0,before.find_first_not_of(" "));
before = before.erase(before.find_last_not_of(" ")+1);
return before;
}
bool is_md_file(string name){
name = strim_string(name);
size_t pos = name.find_last_of(".");
string md = string("md");
if(strcasecmp( name.substr(pos+1,string::npos).c_str(),md.c_str()) == 0){
return true;
}
return false;
}
bool create_dir(string dir_name){
//file do not exist ,return true
if(access(dir_name.c_str(),F_OK) == 0){
return true;
}
//0755 is a normal mkdir directory right
if(mkdir(dir_name.c_str(),0755) == 0){
return true;
}
cout << "error:create dir " << dir_name << endl;
return false;
}
blog_home_page::blog_home_page(){
}
blog_home_page::~blog_home_page(){
}
bool blog_home_page::clear_all(){
//clear string
html_header.clear();
html_header.shrink_to_fit();
html_left_sidebar.clear();
html_left_sidebar.shrink_to_fit();
html_item.clear();
html_item.shrink_to_fit();
html_pagination.clear();
html_pagination.shrink_to_fit();
html_md_contents.clear();
html_md_contents.shrink_to_fit();
html_md_catalog.clear();
html_md_catalog.shrink_to_fit();
html_md_header.clear();
html_md_header.shrink_to_fit();
html_right_sidebar.clear();
html_right_sidebar.shrink_to_fit();
html_ending.clear();
html_ending.shrink_to_fit();
//end clear string
//clear vector<string>
blog_files.clear();
blog_files.shrink_to_fit();
blog_files_without_dir.clear();
blog_files_without_dir.shrink_to_fit();
blog_files_subdir.clear();
blog_files_subdir.shrink_to_fit();
html_index_page_v.clear();
html_index_page_v.shrink_to_fit();
html_index_contents_v.clear();
html_index_contents_v.shrink_to_fit();
//end clear vector
//clear map and multimap
html_archive_contents_vv.clear();
html_tags_contents_vv.clear();
html_archive_page_vv.clear();
html_tags_page_vv.clear();
tags_mmap_blog_file_attr.clear();
month_secs_mmap_blog_file_attr.clear();
date_secs_mmap_blog_file_attr.clear();
//end clear map and multimap
return true;
}
bool blog_home_page::get_blog_dir(string blog_dir){
DIR *dir;
struct dirent *ptr;
dir = opendir(blog_dir.c_str());
if(dir == NULL){
cout << "open dir " << blog_dir << " error\n";
return false;
}
while((ptr = readdir(dir)) != NULL){
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
continue;
}
else if((ptr->d_type == DT_REG) && (is_md_file(ptr->d_name))){
blog_files.push_back(blog_dir + ptr->d_name);
blog_files_without_dir.push_back(ptr->d_name);
} //file
else if(ptr->d_type == DT_LNK){
continue;
}//link file
else if(ptr->d_type == DT_DIR){
blog_files_subdir.push_back(ptr->d_name);
continue;
}//dir
}
closedir(dir);
return true;
}
bool blog_home_page::get_blog_subdir(string blog_dir){
DIR *dir;
struct dirent *ptr;
dir = opendir(blog_dir.c_str());
if(dir == NULL){
cout << "open dir " << blog_dir << " error\n";
return false;
}
vector<string> empty_vec_1;
empty_vec_1.push_back(string(""));
blog_files_subdir.clear();
blog_files_subdir.swap(empty_vec_1);
while((ptr = readdir(dir)) != NULL){
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
continue;
}
else if((ptr->d_type == DT_REG)){
blog_files.push_back(blog_dir + ptr->d_name);
blog_files_without_dir.push_back(ptr->d_name);
} //file
else if(ptr->d_type == DT_LNK){
continue;
}//link file
else if(ptr->d_type == DT_DIR){
blog_files_subdir.push_back(ptr->d_name);
continue;
}//dir
}
closedir(dir);
return true;
}
bool blog_home_page::get_blog_files_attr(){
//bfi means blog file iterator
vector<string>::iterator bfi;
vector<string>::iterator bfi_without_dir;
blog_file_attr_t bfa;
size_t pos;
string tag;
string after_tags;
//dbfavt means date_secs_mmap_blog_file_attr value type
typedef multimap<long,blog_file_attr_t,greater<long>>::value_type dbfavt;
//mbfavt means month_secs_mmap_blog_file_attr value type
typedef multimap<long,blog_file_attr_t,greater<long>>::value_type mbfavt;
//tbfavt means tags_mmap_blog_file_attr value type
typedef multimap<string,blog_file_attr_t,less<string>>::value_type tbfavt;
bfi_without_dir = blog_files_without_dir.begin();
for(bfi = blog_files.begin();
bfi != blog_files.end();bfi++){
if(get_a_blog_file_attr(&bfa,(*bfi))){
bfa.blog_file_time.blog_file_name = *bfi_without_dir;
date_secs_mmap_blog_file_attr.insert(dbfavt(bfa.date_secs,bfa));
month_secs_mmap_blog_file_attr.insert(mbfavt(bfa.month_secs,bfa));
tag = bfa.tags;
pos = tag.find_first_of(" ");
if(pos == string::npos){
tags_mmap_blog_file_attr.insert(tbfavt(tag,bfa));
}
else{
after_tags = tag.substr(0,pos);
tags_mmap_blog_file_attr.insert(tbfavt(strim_string(after_tags),bfa));
after_tags = tag.substr(pos+1,string::npos);
do{
pos = after_tags.find_first_of(" ");
if(pos == string::npos){
tags_mmap_blog_file_attr.insert(tbfavt(strim_string(after_tags),bfa));
}
else{
tag = after_tags.substr(0,pos);
tags_mmap_blog_file_attr.insert(tbfavt(strim_string(tag),bfa));
after_tags = after_tags.substr(pos+1,string::npos);
}
}while(pos != string::npos);
}
}
bfi_without_dir++;
}
return true;
}
bool blog_home_page::get_a_blog_file_attr(blog_file_attr_t *blog_file_attr, string blog_file){
ifstream file;
file.open(blog_file,ios::in);
if(!file){
cout << "fail to open the file 1 " << blog_file << endl;
return false;
}
stringstream file_stream;
file_stream << file.rdbuf();
string blog_line;
getline(file_stream,blog_line);
size_t pos;
int max_times;
string temp_str;
string temp_month_str;
tm temp_tm;
if(blog_line == string("<!--")){
max_times = 0;
do{
max_times = max_times + 1;
getline(file_stream,blog_line);
if(blog_line.substr(0,6) == string("author")){
pos = blog_line.find_first_of(":");
blog_file_attr->author = blog_line.substr(pos+1,string::npos);
}
else if(blog_line.substr(0,4) == string("date")){
pos = blog_line.find_first_of(":");
temp_str = blog_line.substr(pos+1,string::npos);
blog_file_attr->date = temp_str ;
strptime(blog_file_attr->date.c_str(),"%Y-%m-%d %H:%M:%S",&temp_tm);
temp_tm.tm_isdst = -1;
blog_file_attr->date_secs = mktime(&temp_tm);
pos = temp_str.find_last_of("-");
temp_month_str.clear();
temp_month_str = temp_str.substr(0,pos) + string("-01 00:00:00");
strptime(temp_month_str.c_str(),"%Y-%m-%d %H:%M:%S",&temp_tm);
temp_tm.tm_isdst = -1;
blog_file_attr->month_secs = mktime(&temp_tm);
temp_str = temp_str.substr(0,pos);
temp_str = temp_str.erase(0,temp_str.find_first_not_of(" "));
temp_str = temp_str.erase(temp_str.find_last_not_of(" ")+1);
blog_file_attr->month = temp_str.substr(0,pos);
}
else if(blog_line.substr(0,5) == string("title")){
pos = blog_line.find_first_of(":");
blog_file_attr->title = blog_line.substr(pos+1,string::npos);
}
else if(blog_line.substr(0,4) == string("tags")){
pos = blog_line.find_first_of(":");
blog_file_attr->tags = blog_line.substr(pos+1,string::npos);
}
else if(blog_line.substr(0,7) == string("summary")){
pos = blog_line.find_first_of((":"));
blog_file_attr->summary = blog_line.substr(pos+1,string::npos);
}
else if(blog_line.substr(0,5) == string("stick")){
pos = blog_line.find_first_of(":");
blog_file_attr->stick = blog_line.substr(pos+1,string::npos);
}
else if(blog_line == string("-->")){
break;
}
}while(max_times < MAX_TIMES);
}
blog_file_attr->blog_file_time.blog_file_name_with_dir = blog_file;
file.close();
return true;
}
bool blog_home_page::create_blog_index_page(){
string html_index_page = string("");
vector<string>::iterator it;
for(it = html_index_contents_v.begin();
it != html_index_contents_v.end();
it++){
html_index_page = html_header + string("<div class=\"main-page-box\">\n") +
(*it) + html_left_sidebar + html_right_sidebar + string("</div>\n")
+ html_ending;
html_index_page_v.push_back(html_index_page);
html_index_page.clear();
}
return true;
}
bool blog_home_page::create_blog_archive_page(){
string html_archive_page = string("");
map<string,string,less<string> >::iterator it;
typedef map<string,string,less<string> >::value_type hacvt;
for(it = html_archive_contents_vv.begin();
it != html_archive_contents_vv.end();
it++){
html_archive_page = html_header + string("<div class=\"main-page-box\">\n") +
it->second + html_left_sidebar + html_right_sidebar + string("</div>\n")
+ html_ending;
html_archive_page_vv.insert(hacvt(it->first,html_archive_page));
html_archive_page.clear();
}
return true;
}
bool blog_home_page::create_blog_tags_page(){
string html_tags_page = string("");
map<string,string,less<string> >::iterator it;
typedef map<string,string,less<string> >::value_type htpvt;
for(it = html_tags_contents_vv.begin();
it != html_tags_contents_vv.end();
it++){
html_tags_page = html_header + string("<div class=\"main-page-box\">\n") +
it->second + html_left_sidebar + html_right_sidebar + string("</div>\n")
+ html_ending;
html_tags_page_vv.insert(htpvt(it->first,html_tags_page));
html_tags_page.clear();
}
return true;
}
bool blog_home_page::create_blog_header(string html_title,
string blog_name,
string blog_subname){
html_header = string("<!DOCTYPE html>\n"
"<html lang=\"zh_CN\">\n"
"<head>\n<meta "
"charset=\"UTF-8\">"
"<meta name=\"viewport\" "
"content=\"width=device-width, initial-scale=1\">\n"
"\n<title>") +
html_title +
string("</title>\n<link rel=\"stylesheet\" "
"type=\"text/css\" href=\"style."
"css\">\n</head>\n<body>\n"
"<div class=\"main-header-box\">\n"
"<h1 class=\"main-header\">\n"
"<a href=\"/\" rel=\"home\">") +
blog_name +
string("</a>\n</h1>\n<h3 class=\"main-header\">")+
blog_subname +
string("</h3>\n</div>\n<div "
"class=\"main-header-line\"></div>\n"
"<div class=\"main-nav-box\" "
"id=\"main-nav\">\n<div "
"class=\"main-nav\">\n<a href=\"/\" "
"rel=\"home\">首页</a>\n</div>\n"
"<div class=\"main-nav\">|</div>\n"
"<div class=\"main-nav\">\n<a "
"href=\"/about_me.html\">关于我</a>\n"
"</div>\n</div>\n");
return true;
}
bool blog_home_page::create_blog_left_sidebar() {
html_left_sidebar = string("<div class=\"left-sidebar\">\n"
"<h3>文章归档(共");
html_left_sidebar = html_left_sidebar +
to_string( blog_files.size()) +
string("篇)</h3>\n");
multimap<long,blog_file_attr_t,greater<long>>::iterator i;
for(i = month_secs_mmap_blog_file_attr.begin();
i != month_secs_mmap_blog_file_attr.end();
){
pair <multimap<long,blog_file_attr_t,greater<long> >::iterator,
multimap<long,blog_file_attr_t,greater<long> >::iterator>
ret;
ret = month_secs_mmap_blog_file_attr.equal_range(i->first);
html_left_sidebar = html_left_sidebar +
string("<h4><a href=\"/archive/") +
ret.first->second.month + string(".html\">") +
ret.first->second.month + string("(") +
to_string(month_secs_mmap_blog_file_attr.count(ret.first->first)) +
string("篇)</a></h4>\n");
for(i = ret.first;i != ret.second;){
i++;
}
}
html_left_sidebar = html_left_sidebar + string("</div>\n");
return true;
}
bool blog_home_page::create_blog_right_sidebar(){
html_right_sidebar = string("<div class=\"right-sidebar\">"
"<h3>标签分类</h3>\n");
multimap<string,blog_file_attr_t,less<string>>::iterator i;
for(i = tags_mmap_blog_file_attr.begin();
i != tags_mmap_blog_file_attr.end();
){
pair<multimap<string,blog_file_attr_t,less<string> >::iterator,
multimap<string,blog_file_attr_t,less<string> >::iterator>
ret;
ret = tags_mmap_blog_file_attr.equal_range(i->first);
if(!ret.first->first.empty()){
html_right_sidebar = html_right_sidebar +
string("<h4><a href=\"/tags/") +
ret.first->first + string(".html\">") +
ret.first->first + string("(") +
to_string(tags_mmap_blog_file_attr.count(ret.first->first)) +
string("篇)</a></h4>\n");
}
for(i = ret.first;i != ret.second;i++){
}
}
html_right_sidebar = html_right_sidebar +
string("</div>\n");
return true;
}
bool blog_home_page::create_blog_ending(){
html_ending = string("<div class=\"footer\">\n"
"<p>Powered by <a href=\""
"https://gitee.com/iamdsy/nginx-blog\" "
"target=\"_blank\">nginx-blog</a></p>\n"
"</div>\n</body>\n</html>");
return true;
}
bool blog_home_page::create_blog_index_contents(int max_items_per_page){
int pages = 0;
string html_index_contents;
multimap<long,blog_file_attr_t,greater<long>>::iterator it;
for(it = date_secs_mmap_blog_file_attr.begin();
it != date_secs_mmap_blog_file_attr.end();
){
html_index_contents = string("<div class=\"contents\">\n");
for(int j = 0;j < max_items_per_page;j++){
if(it != date_secs_mmap_blog_file_attr.end()){
if(create_blog_item(it->second)){
html_index_contents = html_index_contents + html_item;
}
html_item.clear();
it++;
}
}
pages = pages + 1;
if(create_blog_pagination(pages,string("index"),max_items_per_page)){
html_index_contents = html_index_contents + html_pagination;
}
html_index_contents = html_index_contents + string("</div>\n");
html_index_contents_v.push_back(html_index_contents);
html_index_contents.clear();
}
return true;
}
bool blog_home_page::create_blog_archive_contents(int max_items_per_page){
int pages = 0;
string month;
string html_archive_contents;
string page_type = string("/archive/");
multimap<long,blog_file_attr_t,greater<long>>::iterator it;
typedef map<string,string,less<string> >::value_type hacvt;
for(it = month_secs_mmap_blog_file_attr.begin();
it != month_secs_mmap_blog_file_attr.end();
){
pair <multimap<long,blog_file_attr_t,greater<long> >::iterator,
multimap<long,blog_file_attr_t,greater<long> >::iterator>
ret;
ret = month_secs_mmap_blog_file_attr.equal_range(it->first);
for(it = ret.first;it != ret.second;){
html_archive_contents = string("<div class=\"contents\">\n");
for(int j = 0;j < max_items_per_page;j++){
if(create_blog_item(it->second)){
html_archive_contents = html_archive_contents + html_item;
}
html_item.clear();
it++;
if((it == month_secs_mmap_blog_file_attr.end())
|| it == ret.second){
break;
}
}
if(pages == 0){
month = ret.first->second.month;
}
else{
month = ret.first->second.month +
string("_") + to_string(pages+1);
}
pages = pages+1;
int max_item = month_secs_mmap_blog_file_attr.count(ret.first->first);
int max_page = max_item / max_items_per_page;
if((max_item % max_items_per_page) > 0){
max_page = max_page + 1;
}
if(create_archive_tags_blog_pagination(pages,string("/archive/"),
max_page,ret.first->second.month)){
html_archive_contents = html_archive_contents + html_pagination;
}
html_archive_contents = html_archive_contents + string("</div>\n");
html_archive_contents_vv.insert(hacvt(month,html_archive_contents));
if((max_page == pages) && (max_page > 1)){
month = ret.first->second.month +
string("_last");
html_archive_contents_vv.insert(hacvt(month,html_archive_contents));
}
month.clear();
html_archive_contents.clear();
if(it == month_secs_mmap_blog_file_attr.end()){
break;
}
}
pages = 0;
}
return true;
}
bool blog_home_page::create_blog_tags_contents(int max_items_per_page){
int pages = 0;
string html_tags_contents;
string tags;
string page_type = string("/tags/");
multimap<string,blog_file_attr_t,less<string> >::iterator it;
typedef map<string,string,less<string> >::value_type hacvt;
for(it = tags_mmap_blog_file_attr.begin();
it != tags_mmap_blog_file_attr.end();
){
pair<multimap<string,blog_file_attr_t,less<string> >::iterator,
multimap<string,blog_file_attr_t,less<string> >::iterator>
ret;
ret = tags_mmap_blog_file_attr.equal_range(it->first);
for(it = ret.first;it != ret.second;){
html_tags_contents = string("<div class=\"contents\">\n");
for(int j = 0;j < max_items_per_page;j++){
if(create_blog_item(it->second)){
html_tags_contents = html_tags_contents + html_item;
}
html_item.clear();
it++;
if((it == tags_mmap_blog_file_attr.end())
|| (it == ret.second)){
break;
}
}
//for some reason do not clearly
if(!ret.first->first.empty()){
if(pages == 0){
tags = ret.first->first;
}
else{
tags = ret.first->first +
string("_") + to_string(pages+1);
}
}
pages = pages+1;
int max_item =tags_mmap_blog_file_attr.count(ret.first->first);
int max_page = max_item / max_items_per_page;
if((max_item % max_items_per_page) > 0){
max_page = max_page + 1;
}
if(create_archive_tags_blog_pagination(pages,string("/tags/"),
max_page,ret.first->first)){
html_tags_contents = html_tags_contents + html_pagination;
}
html_tags_contents = html_tags_contents + string("</div>\n");
html_tags_contents_vv.insert(hacvt(tags,html_tags_contents));
if((max_page == pages) && (max_page > 1) && (!ret.first->first.empty())){
tags = ret.first->first +
string("_last");
html_tags_contents_vv.insert(hacvt(tags,html_tags_contents));
}
tags.clear();
html_tags_contents.clear();
if(it == tags_mmap_blog_file_attr.end()){
break;
}
}
pages = 0;
}
return true;
}
bool blog_home_page::create_blog_item(blog_file_attr_t bfa){
html_item = string("<div class=\"contents-item\">\n<h1>");
html_item = html_item + bfa.title + string("</h1>");
html_item = html_item + string("</div>\n<div "
"class=\"contents-item\">\n"
"<div class=\"time"
"-tags-box\">\n<div class=\"time\">");
html_item = html_item + bfa.date + string(" / ");
html_item = html_item + bfa.author + string("</div>");
size_t pos;
if(!bfa.tags.empty()){
string tag;
string after_tag;
html_item = html_item + string("<div class=\"tags\">标签:\n");
pos = bfa.tags.find_first_of(" ");
if(pos == string::npos){
html_item = html_item + string("<span>\n") +
string("<a href=\"/tags/") +
bfa.tags + string(".html\" rel = \"tag\">") +
bfa.tags + string("</a>\n</span>\n");
}
else{
tag = bfa.tags.substr(0,pos);
after_tag = bfa.tags.substr(pos+1,string::npos);
html_item = html_item + string("<span>\n") +
string("<a href=\"/tags/") +
tag + string(".html\" rel = \"tag\">") +
tag + string("</a>\n</span>\n");
do{
pos = after_tag.find_first_of(" ");
if(pos == string::npos){
html_item = html_item + string("<span>\n") +
string("<a href=\"/tags/") +
after_tag + string(".html\" rel = \"tag\">") +
after_tag + string("</a>\n</span>\n");
}
else{
tag = after_tag.substr(0,pos);
after_tag = after_tag.substr(pos+1,string::npos);
html_item = html_item + string("<span>\n") +
string("<a href=\"/tags/") +
tag + string(".html\" rel = \"tag\">") +
tag + string("</a>\n</span>\n");
}
}while(pos != string::npos);
}
html_item = html_item + string("</div>\n");
}
html_item = html_item + string("</div>\n</div>\n");
html_item = html_item + string("<div class=\"contents-item summary\">\n");
html_item = html_item + bfa.summary + string("</div>\n");
html_item = html_item + string("<div class=\"contents-item\">\n");
html_item = html_item + string("<a href=\"/blog/");
pos = bfa.blog_file_time.blog_file_name.find_last_of(".");
if(pos == string::npos){
html_item = html_item + bfa.blog_file_time.blog_file_name;
}
else{
html_item = html_item + bfa.blog_file_time.blog_file_name.substr(0,pos);
}
html_item = html_item + string(".html\">阅读全文</a>\n</div>");
return true;
}
bool blog_home_page::create_blog_pagination(int pages, string page_type,
int max_item_per_page){
int max_page = blog_files.size() / max_item_per_page;
if((blog_files.size() % max_item_per_page) > 0 ){
max_page = max_page + 1;
}
if(max_page == 1){
html_pagination = string("");
return true;
}
html_pagination = string("<div class=\"contents-item\">\n"
"<h4 class=\"pagination\">\n"
"<span class=\"pagination\"><a href=\"");
html_pagination = html_pagination + page_type +
string(".html\"><<</a></span>\n");
if(max_page - pages >= 5){
for (int i = pages;i <= pages+5;i++){
if(i == pages){
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + string("_") + to_string(i) + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
}
else{
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + string("_") + to_string(i) + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
}
}
}
else{
int i;
if(max_page >= 7){
i = max_page - 5;
}
else{
i = 1;
}
for (;i <= max_page;i++){
if(i == pages){
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + string("_") + to_string(i) + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
}
else{
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + string("_") + to_string(i) + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
}
}
}
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + string("_last.html\">>></a></span>\n");
html_pagination = html_pagination + string("</h4>\n</div>\n");
return true;
}
bool blog_home_page::create_archive_tags_blog_pagination(int pages, string page_type,
int max_pages,string arch_tags){
if(max_pages == 1){
html_pagination = string("");
return true;
}
html_pagination = string("<div class=\"contents-item\">\n"
"<h4 class=\"pagination\">\n"
"<span class=\"pagination\"><a href=\"");
html_pagination = html_pagination + page_type +
arch_tags + string(".html\"><<</a></span>\n");
if(max_pages - pages >= 5){
for (int i = pages;i <= pages+5;i++){
if(i == pages){
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + arch_tags + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + arch_tags + string("_") + to_string(i) +
string(".html\">") + to_string(i) + string("</a></span>\n");
}
}
else{
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + arch_tags + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + arch_tags + string("_") + to_string(i)
+ string(".html\">") + to_string(i) + string("</a></span>\n");
}
}
}
}
else{
int i;
if(max_pages >= 7){
i = max_pages - 5;
}
else{
i = 1;
}
for (;i <= max_pages;i++){
if(i == pages){
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + arch_tags + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination active\"><a href=\"")+
page_type + arch_tags + string("_") + to_string(i) +
string(".html\">") + to_string(i) + string("</a></span>\n");
}
}
else{
if(i == 1){
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + arch_tags + string(".html\">") +
to_string(i) + string("</a></span>\n");
}
else{
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + arch_tags + string("_") + to_string(i)
+ string(".html\">") + to_string(i) + string("</a></span>\n");
}
}
}
}
html_pagination = html_pagination +
string("<span class=\"pagination\"><a href=\"")+
page_type + arch_tags + string("_last.html\">>></a></span>\n");
html_pagination = html_pagination + string("</h4>\n</div>\n");
return true;
}
bool blog_home_page::create_md_header(string html_title,
string blog_name,
string blog_subname,
blog_file_attr_t bfa){
html_md_header = string("<!DOCTYPE html>\n"
"<html lang=\"zh_CN\">\n"
"<head>\n<meta "
"charset=\"UTF-8\">"
"<meta name=\"viewport\" "
"content=\"width=device-width, initial-scale=1\">\n"
"\n<title>") + bfa.title + string(" | ") +
html_title +
string("</title>\n"
"<meta name=\"keyword\" contents=\"") +
bfa.tags +
string("\">\n<meta name=\"description\" contents\"") +
bfa.summary +
string("\">\n<link rel=\"stylesheet\" "
"type=\"text/css\" href=\"style."
"css\">\n</head>\n<body>\n"
"<div class=\"main-header-box\">\n"
"<h1 class=\"main-header\">\n"
"<a href=\"/\" rel=\"home\">") +
blog_name +
string("</a>\n</h1>\n<h3 class=\"main-header\">")+
blog_subname +
string("</h3>\n</div>\n<div "
"class=\"main-header-line\"></div>\n"
"<div class=\"main-nav-box\" "
"id=\"main-nav\">\n<div "
"class=\"main-nav\">\n<a href=\"/\" "
"rel=\"home\">首页</a>\n</div>\n"
"<div class=\"main-nav\">|</div>\n"
"<div class=\"main-nav\">\n<a "
"href=\"/about_me.html\">关于我</a>\n"
"</div>\n</div>\n");
return true;
}
bool blog_home_page::create_md_contens(blog_file_attr_t bfa){
string about_me = string("about_me.md");
if(strcasecmp(about_me.c_str(),bfa.blog_file_time.blog_file_name.c_str()) != 0){
html_md_contents = string("<div class=\"contents\">\n"
"<div class=\"contents-item\">\n"
"<h1>\n<a href=\"/blog/");
//get blog html file name
string temp_str = bfa.blog_file_time.blog_file_name;
int dot = temp_str.find_last_of(".");
temp_str = temp_str.substr(0,dot) + string(".html\">");
html_md_contents = html_md_contents + temp_str +
bfa.title + string("</a>\n");
//end html file name
}
else{
html_md_contents = string("<div class=\"contents\">\n");
//get blog html file name
}
if(strcasecmp(about_me.c_str(),bfa.blog_file_time.blog_file_name.c_str()) != 0){
//get blog time author
html_md_contents = html_md_contents + string("</h1>\n</div>\n<div class=\"contents-item\">\n"
"<div class=\"time-tags-box\">\n<div class=\"time\">");
html_md_contents = html_md_contents + bfa.date + string(" / ") +
bfa.author + string(" </div>");
//end time author
size_t pos;
//get blog tags
string html_md_tag;
if(!bfa.tags.empty()){
string tag;
string after_tag;
html_md_tag = html_md_tag + string("<div class=\"tags\">标签:\n");
pos = bfa.tags.find_first_of(" ");
if(pos == string::npos){
html_md_tag = html_md_tag + string("<span>\n") +
string("<a href=\"/tags/") +
bfa.tags + string(".html\" rel = \"tag\">") +
bfa.tags + string("</a>\n</span>\n");
}
else{
tag = bfa.tags.substr(0,pos);
after_tag = bfa.tags.substr(pos+1,string::npos);
html_md_tag = html_md_tag + string("<span>\n") +
string("<a href=\"/tags/") +
tag + string(".html\" rel = \"tag\">") +
tag + string("</a>\n</span>\n");
do{
pos = after_tag.find_first_of(" ");
if(pos == string::npos){
html_md_tag = html_md_tag + string("<span>\n") +
string("<a href=\"/tags/") +
after_tag + string(".html\" rel = \"tag\">") +
after_tag + string("</a>\n</span>\n");
}
else{
tag = after_tag.substr(0,pos);
after_tag = after_tag.substr(pos+1,string::npos);
html_md_tag = html_md_tag + string("<span>\n") +
string("<a href=\"/tags/") +
tag + string(".html\" rel = \"tag\">") +
tag + string("</a>\n</span>\n");
}
}while(pos != string::npos);
}
html_md_tag = html_md_tag + string("</div>\n");
}
html_md_contents = html_md_contents + html_md_tag;
html_md_contents = html_md_contents + string("</div>\n</div>\n");
//end blog tags
}
//get blog contents
ifstream file;
file.open(bfa.blog_file_time.blog_file_name_with_dir,
ios::in);//open the input file
if(!file){
cout << "fail to open the file 2 " << bfa.blog_file_time.blog_file_name_with_dir
<< endl;
return false;
}
stringstream str_stream;
str_stream << file.rdbuf();//read the file
markdown_parse hehe;
bool ret = hehe.parse_file(&str_stream);
if(ret == false){
cout << "some thing wrong happened\n";
file.close();
return false;
}
file.close();
html_md_contents = html_md_contents + string("<div class=\"contents-item\">\n");
html_md_contents = html_md_contents + hehe.parsed_all_string + string("</div>\n</div>\n");
if(hehe.catalog.empty()){
html_md_catalog = string("<div class=\"left-sidebar\">\n") +
string("<h3> </h3>\n");
html_md_catalog = html_md_catalog+ hehe.catalog + string("</div>");
}
else{
if(strcasecmp(about_me.c_str(),bfa.blog_file_time.blog_file_name.c_str()) != 0){
html_md_catalog = string("<div class=\"left-sidebar\">\n") +
string("<h3>目录</h3>\n");
html_md_catalog = html_md_catalog+ hehe.catalog + string("</div>");
}
else{
html_md_catalog = string("<div class=\"left-sidebar\">\n") +
string("<h3> </h3>\n");
html_md_catalog = html_md_catalog+ hehe.catalog + string("</div>");
}
}
return true;
}
C++
1
https://gitee.com/iamdsy/nginx-blog.git
git@gitee.com:iamdsy/nginx-blog.git
iamdsy
nginx-blog
nginx-blog
master

搜索帮助