1 Star 0 Fork 0

iamdsy / nginx-blog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ngx_blog.cpp 41.54 KB
一键复制 编辑 原始数据 按行查看 历史
iamdsy 提交于 2017-12-27 23:37 . test_about_me
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
extern "C" {
#include <sys/inotify.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h> //for stat
#include <sys/types.h>
#include <dirent.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_event.h>
#include <ngx_event_connect.h>
}
#include "blog_home_page.h"
#include "ngx_blog.h"
#include "lib_markdown_parse.h"
#include <fstream>
using namespace std;
static void* ngx_http_blog_create_main_conf(ngx_conf_t* cf);
static ngx_int_t ngx_http_blog_all(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_blog_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_blog_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_blog_get_wd_str(ngx_array_t* wd_dir_array,ngx_int_t key,
ngx_str_t* ret_str,ngx_http_request_t *r);
static string strim_last_slash(string name);
static bool is_directory(string name);
static bool delete_html_file(string name);
static bool copy_file(string src_dir,string src_file,
string dst_dir);
static bool copy_directory(string src_dir,string dst_src);
static bool get_all_subdir(string dir_with_path,vector<string>* subdir_without_path);
static bool create_all_output_dir(string output_pre_with_path,
string output_blog_without_path,
vector<string> output_blog_sub_without_path,
string output_archive_without_path,
string output_tags_without_path);
static bool create_nginx_blog_without_md(string md_file_dir_with_path,
string html_title,
string blog_name,
string blog_subname,
int max_item_per_page,
blog_home_page* blog_home);
static bool output_index_html(string output_dir_with_path,
int max_item_per_page,
blog_home_page* blog_home);
static bool output_archive_html(string output_dir_with_path,
int max_item_per_page,
blog_home_page* blog_home);
static bool output_tags_html(string output_dir_with_path,
blog_home_page* blog_home);
static bool create_output_all_md_blog_html(string output_dir_with_path,
string html_title,
string blog_name,
string blog_subname,
blog_home_page* blog_home);
static bool create_out_a_md_blog_html(string src_dir_with_path,
string md_name_with_post,
string html_title,
string blog_name,
string blog_subname,
string dst_dir_with_path,
blog_home_page* blog_home);
static bool create_out_about_me_html(string src_dir_with_path,
string md_name_with_post,
string html_title,
string blog_name,
string blog_subname,
string dst_dir_with_path,
blog_home_page* blog_home);
static ngx_command_t ngx_http_blog_commands[] = {
{ ngx_string("src_root_dir"),
NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_blog_main_conf_t,src_root_dir),
NULL },
{ ngx_string("dst_root_dir"),
NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_blog_main_conf_t,dst_root_dir),
NULL },
{ ngx_string("html_title"),
NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_blog_main_conf_t,html_title),
NULL },
{ ngx_string("blog_name"),
NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_blog_main_conf_t,blog_name),
NULL },
{ ngx_string("blog_subname"),
NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_blog_main_conf_t,blog_subname),
NULL },
{ ngx_string("max_item_per_page"),
NGX_HTTP_MAIN_CONF| NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_blog_main_conf_t,max_item_per_page),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_blog_module_ctx = {
NULL, /* preconfiguration */
ngx_http_blog_init, /* postconfiguration */
ngx_http_blog_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_blog_module = {
NGX_MODULE_V1,
&ngx_http_blog_module_ctx, /* module context */
ngx_http_blog_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
ngx_http_blog_all, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void* ngx_http_blog_create_main_conf(ngx_conf_t *cf){
ngx_http_blog_main_conf_t *conf;
conf = (ngx_http_blog_main_conf_t*)ngx_palloc(cf->pool,sizeof(ngx_http_blog_main_conf_t));
if(conf == NULL){
return NULL;
}
conf->max_item_per_page = NGX_CONF_UNSET_UINT;
conf->inotify_fd = NGX_CONF_UNSET_UINT;
conf->wd_dir_key_val = NULL;
//create the space to store the wd and the directory
ngx_array_t* a;
a = (ngx_array_t*)ngx_array_create(cf->pool,4,sizeof(wd_dir_key_val_t));
if(a == NULL){
return NULL;
}
conf->wd_dir_key_val = a;
return conf;
}
static ngx_int_t ngx_http_blog_all(ngx_cycle_t *cycle){
ngx_http_blog_main_conf_t *bmcf;
bmcf = (ngx_http_blog_main_conf_t*)ngx_http_cycle_get_module_main_conf(cycle,ngx_http_blog_module);
string src_root_dir = string(reinterpret_cast<char*>(bmcf->src_root_dir.data));
string dst_root_dir = string(reinterpret_cast<char*>(bmcf->dst_root_dir.data));
string html_title = string(reinterpret_cast<char*>(bmcf->html_title.data));
string blog_name = string(reinterpret_cast<char*>(bmcf->blog_name.data));
string blog_subname = string(reinterpret_cast<char*>(bmcf->blog_subname.data));
int max_item_per_page = bmcf->max_item_per_page;
string src_blog_dir = string("blog/");
string dst_blog_dir = string("blog/");
string dst_archive_dir = string("archive/");
string dst_tags_dir = string("tags/");
vector<string> src_blog_subdir_without_path;
if(!get_all_subdir(src_root_dir + src_blog_dir,&src_blog_subdir_without_path)){
return NGX_ERROR;
}
if(!create_all_output_dir(dst_root_dir,
dst_blog_dir,
src_blog_subdir_without_path,
dst_archive_dir,
dst_tags_dir)){
return NGX_ERROR;
}
blog_home_page blog_home;
if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
html_title,
blog_name,
blog_subname,
max_item_per_page,
&blog_home)){
return NGX_ERROR;
}
if(!output_index_html(dst_root_dir,
max_item_per_page,
&blog_home)){
return NGX_ERROR;
}
if(!output_archive_html(dst_root_dir + dst_archive_dir,
max_item_per_page,
&blog_home)){
return NGX_ERROR;
}
if(!output_tags_html(dst_root_dir + dst_tags_dir,
&blog_home)){
return NGX_ERROR;
}
if(!create_output_all_md_blog_html(dst_root_dir + dst_blog_dir,
html_title,
blog_name,
blog_subname,
&blog_home)){
return NGX_ERROR;
}
//create about_me.html
if(!create_out_about_me_html(src_root_dir,
string("about_me.md"),
html_title,
blog_name,
blog_subname,
dst_root_dir,
&blog_home)){
return NGX_OK;
}
vector<string>::iterator it;
for(it = src_blog_subdir_without_path.begin();
it != src_blog_subdir_without_path.end();
it++){
if(!copy_directory(src_root_dir+src_blog_dir + (*it),
dst_root_dir + dst_blog_dir + (*it))){
return false;
}
}
blog_home.clear_all();
int inotify_fd = inotify_init1(IN_NONBLOCK);
if(inotify_fd < 0){
cout << "init inotify error\n";
return NGX_ERROR;
}
bmcf->inotify_fd = inotify_fd;
int op = IN_CREATE | IN_MODIFY | IN_DELETE;
wd_dir_key_val_t* wd_dir_key_val;
if((bmcf->wd[0] = inotify_add_watch(inotify_fd,
(strim_last_slash(src_root_dir + src_blog_dir)).c_str()
,op)) == -1){
cout << "can not add inotify watch " << (src_root_dir + src_blog_dir) << endl;
close(inotify_fd);
return NGX_ERROR;
}
else{
wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
wd_dir_key_val->wd = bmcf->wd[0];
wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(cycle->pool,64);
strcpy((char*)wd_dir_key_val->dir_or_file.data,(src_blog_dir).c_str());
wd_dir_key_val->dir_or_file.len = src_blog_dir.length()+1;
}
if((bmcf->wd[1] = inotify_add_watch(inotify_fd,
(strim_last_slash(src_root_dir)).c_str()
,op)) == -1){
cout << "can not add inotify watch " << (src_root_dir) << endl;
close(inotify_fd);
return NGX_ERROR;
}
else{
wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
wd_dir_key_val->wd = bmcf->wd[1];
wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(cycle->pool,64);
strcpy((char*)wd_dir_key_val->dir_or_file.data,(string("")).c_str());
wd_dir_key_val->dir_or_file.len = 1;
}
int i = 1;
int wr;
for(it = src_blog_subdir_without_path.begin();
it != src_blog_subdir_without_path.end();
it++){
if((wr = inotify_add_watch(inotify_fd,
(strim_last_slash(src_root_dir + src_blog_dir + (*it))).c_str(),op)) == -1){
cout << "can not add inotify watch " << src_root_dir + src_blog_dir + (*it) << endl;
close(inotify_fd);
return NGX_ERROR;
}
else{
wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
wd_dir_key_val->wd = wr;
wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(cycle->pool,64);
strcpy((char*)wd_dir_key_val->dir_or_file.data,(*it).c_str());
wd_dir_key_val->dir_or_file.len = (*it).length()+1;
ngx_str_set(&(wd_dir_key_val->dir_or_file),const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(((*it)).c_str())));
}
i++;
}
//end to add to inotify
return NGX_OK;
}
static ngx_int_t ngx_http_blog_init(ngx_conf_t *cf){
ngx_http_handler_pt* h;
ngx_http_core_main_conf_t* cmcf;
cmcf = (ngx_http_core_main_conf_t*)ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = (ngx_http_handler_pt*)ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_blog_handler;
return NGX_OK;
}
static ngx_int_t ngx_http_blog_handler(ngx_http_request_t *r){
ngx_http_blog_main_conf_t *bmcf;
bmcf = (ngx_http_blog_main_conf_t*)ngx_http_get_module_main_conf(r,ngx_http_blog_module);
string src_root_dir = string(reinterpret_cast<char*>(bmcf->src_root_dir.data));
string dst_root_dir = string(reinterpret_cast<char*>(bmcf->dst_root_dir.data));
string html_title = string(reinterpret_cast<char*>(bmcf->html_title.data));
string blog_name = string(reinterpret_cast<char*>(bmcf->blog_name.data));
string blog_subname = string(reinterpret_cast<char*>(bmcf->blog_subname.data));
int max_item_per_page = bmcf->max_item_per_page;
string src_blog_dir = string("blog/");
string dst_blog_dir = string("blog/");
string dst_archive_dir = string("archive/");
string dst_tags_dir = string("tags/");
string about_me = string("about_me.md");
blog_home_page blog_home;
string src_dir;
string src_file;
string dst_dir;
string sub_dir;
int op = IN_CREATE | IN_MODIFY | IN_DELETE;
wd_dir_key_val_t* wd_dir_key_val;
int length;
char inotify_event_buf[1024];
while((length = read(bmcf->inotify_fd,inotify_event_buf,1024)) > 0){
blog_home.clear_all();
struct inotify_event* iev = (struct inotify_event*)inotify_event_buf;
//blog dir create directory,add the diretory into the inotify_fd to watch
//blog dir create md file,call nginx_blog_md and nginx_blog_other
//blog dir modify or close write md file,call nginx_blog_md and nginx_blog_other
//else blog subdir create modify or close write file,copy changed files into the html directory
if(iev->wd == bmcf->wd[0]){
if(iev->mask & IN_CREATE){
if(is_directory(src_root_dir + src_blog_dir + string(iev->name))){
int new_subdir = inotify_add_watch(bmcf->inotify_fd,
(src_root_dir + src_blog_dir + string(iev->name)).c_str(),op);
if(new_subdir == -1){
ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"can not add the new subdir into the inotify\n") ;
}
else{
wd_dir_key_val = (wd_dir_key_val_t*)ngx_array_push(bmcf->wd_dir_key_val);
wd_dir_key_val->wd = new_subdir;
wd_dir_key_val->dir_or_file.data = (u_char*)ngx_palloc(r->pool,64);
strcpy((char*)wd_dir_key_val->dir_or_file.data,(string(iev->name) + string("/")).c_str());
wd_dir_key_val->dir_or_file.len = (string(iev->name) + string("/")).length();
// wd_dir_key_val->dir_or_file.data = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>((string(iev->name) + string("/")).c_str()));
// wd_dir_key_val->dir_or_file.len = (string(iev->name) + string("/")).length();
if(!create_dir(dst_root_dir + dst_blog_dir + string(iev->name) + string("/"))){
cout << "can not create dir:" << dst_root_dir + dst_blog_dir + string(iev->name) << endl;
return NGX_OK;
}
}
}
if(is_md_file(string(iev->name))){
if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
html_title,
blog_name,
blog_subname,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_index_html(dst_root_dir,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_archive_html(dst_root_dir + dst_archive_dir,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_tags_html(dst_root_dir + dst_tags_dir,
&blog_home)){
return NGX_OK;
}
if(!create_out_a_md_blog_html(src_root_dir + src_blog_dir,
string(iev->name),
html_title,
blog_name,
blog_subname,
dst_root_dir + dst_blog_dir,
&blog_home)){
return NGX_OK;
}
}
}
if(iev->mask & IN_MODIFY){
if(is_md_file(string(iev->name))){
if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
html_title,
blog_name,
blog_subname,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_index_html(dst_root_dir,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_archive_html(dst_root_dir + dst_archive_dir,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_tags_html(dst_root_dir + dst_tags_dir,
&blog_home)){
return NGX_OK;
}
if(!create_out_a_md_blog_html(src_root_dir + src_blog_dir,
string(iev->name),
html_title,
blog_name,
blog_subname,
dst_root_dir + dst_blog_dir,
&blog_home)){
return NGX_OK;
}
}
}
if(iev->mask & IN_DELETE){
string iev_name = string(iev->name);
int pos = iev_name.find_last_of(".");
string html_file = iev_name.substr(0,pos) + string(".html");
if(!delete_html_file(dst_root_dir + dst_blog_dir + html_file)){
cout << "can not delete file:" << iev_name << endl;
}
vector<string> src_blog_subdir_without_path;
if(!get_all_subdir(src_root_dir + src_blog_dir,&src_blog_subdir_without_path)){
return NGX_OK;
}
if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
html_title,
blog_name,
blog_subname,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_index_html(dst_root_dir,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_archive_html(dst_root_dir + dst_archive_dir,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
if(!output_tags_html(dst_root_dir + dst_tags_dir,
&blog_home)){
return NGX_OK;
}
//for md files managed by git
if(!create_out_a_md_blog_html(src_root_dir + src_blog_dir,
string(iev->name),
html_title,
blog_name,
blog_subname,
dst_root_dir + dst_blog_dir,
&blog_home)){
return NGX_OK;
}
}
}
else if(iev->wd == bmcf->wd[1]){
if((iev->mask & IN_CREATE ) || (iev->mask & IN_MODIFY)){
if(strcasecmp(about_me.c_str(),iev->name) == 0){
if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
html_title,
blog_name,
blog_subname,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
//create about_me.html
if(!create_out_about_me_html(src_root_dir,
string("about_me.md"),
html_title,
blog_name,
blog_subname,
dst_root_dir,
&blog_home)){
ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"false");
return NGX_OK;
}
ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"true");
}
}
//for md files managed by git
else{
if(strcasecmp(about_me.c_str(),iev->name) == 0){
if(!create_nginx_blog_without_md(src_root_dir + src_blog_dir,
html_title,
blog_name,
blog_subname,
max_item_per_page,
&blog_home)){
return NGX_OK;
}
//create about_me.html
if(!create_out_about_me_html(src_root_dir,
string("about_me.md"),
html_title,
blog_name,
blog_subname,
dst_root_dir,
&blog_home)){
ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"false");
return NGX_OK;
}
ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"true");
}
}
}
else{
if(iev->mask & IN_CREATE){
ngx_str_t val;
if(ngx_http_blog_get_wd_str(bmcf->wd_dir_key_val,iev->wd,&val,r) == NGX_ERROR){
return NGX_OK;
}
if(val.data != NULL){
sub_dir = string(reinterpret_cast<char*>(val.data));
}
if(!is_directory(src_root_dir + src_blog_dir + sub_dir + string(iev->name))){
src_dir.clear();
src_dir.shrink_to_fit();
src_file.clear();
src_file.shrink_to_fit();
dst_dir.clear();
dst_dir.shrink_to_fit();
src_dir = src_root_dir + src_blog_dir + sub_dir;
src_file = string(iev->name);
dst_dir = dst_root_dir + dst_blog_dir + sub_dir;
if(!copy_file(src_dir,src_file,dst_dir)){
cout << "copy changed file error\n"
<< "the src dir is:" << src_dir
<< "the src file is:" << src_file
<< "the dst dir is:" << dst_dir;
}
}
}
}
}
if(length == -1 && errno != EAGAIN){
cout << "some error happened when read from the inotify fd\n";
}
return NGX_OK;
}
static ngx_int_t ngx_http_blog_get_wd_str(ngx_array_t* wd_dir_array,ngx_int_t key,
ngx_str_t* ret_str,ngx_http_request_t *r){
for(ngx_uint_t i = 0;i < wd_dir_array->nelts;i++){
wd_dir_key_val_t* wd_dir_key_val = (wd_dir_key_val_t*)((char*)wd_dir_array->elts + sizeof(wd_dir_key_val_t)*i);
if(wd_dir_key_val->wd == key){
ret_str->data = wd_dir_key_val->dir_or_file.data;
ret_str->len = wd_dir_key_val->dir_or_file.len;
return NGX_OK;
}
}
return NGX_ERROR;
}
static string strim_last_slash(string name){
int pos = name.find_last_of("/");
name = name.substr(0,pos);
return name;
}
static bool is_directory(string name){
struct stat stat_buf;
if(stat(name.c_str(),&stat_buf) == -1){
cout << "get stat of " << name << " wrong\n";
return false;
}
if((stat_buf.st_mode & S_IFMT) == S_IFDIR){
return true;
}
return false;
}
static bool delete_html_file(string name){
//file do not exist ,return true
if(access(name.c_str(),F_OK) == -1){
return true;
}
if(remove(name.c_str()) == 0){
return true;
}
return false;
}
static bool copy_file(string src_dir,string src_file,
string dst_dir){
//copy file
ifstream file_read;
ofstream file_write;
file_read.open(src_dir+src_file,fstream::binary);
if(!file_read){
cout << " 1 can not open file " << src_dir + src_file << endl;
return false;
}
string file_name = dst_dir + src_file;
file_write.open(file_name,fstream::binary);
if(!file_write){
cout << " 2 can not open img file " << file_name << endl;
return false;
}
if (file_read) {
// get length of file:
file_read.seekg (0, file_read.end);
int length = file_read.tellg();
file_read.seekg (0, file_read.beg);
char * buffer = new char [length];
// read data as a block:
file_read.read (buffer,length);
if (!file_read){
cout << "error: only " << file_read.gcount() << " could be read";
return false;
}
file_write.write(buffer,length);
delete []buffer;
}
file_read.close();
file_write.close();
return true;
//end copy file
}
static bool copy_directory(string src_dir,string dst_src){
DIR *dir;
dir = opendir(src_dir.c_str());
if(dir == NULL){
cout << "error open dir " << src_dir << endl;
return false;
}
struct dirent* ptr;
while((ptr = readdir(dir)) != NULL){
if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0){
continue;
}
else if(ptr->d_type == DT_REG){
if(!copy_file(src_dir,string(ptr->d_name),dst_src)){
cout << "copy file " << ptr->d_name << " from directory "
<< src_dir << " to directory " << dst_src << endl;
}
}
else{
continue;
}
}
return true;
}
static bool get_all_subdir(string dir_with_path,vector<string>* subdir_without_path){
DIR* dir;
struct dirent* ptr;
dir = opendir(dir_with_path.c_str());
if(dir == NULL){
cout << "error:get all subdir of " << dir_with_path << endl;
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_DIR){
subdir_without_path->push_back(string(ptr->d_name) + string("/"));
}
else{
continue;
}
}
return true;
}
static bool create_all_output_dir(string output_pre_with_path,
string output_blog_without_path,
vector<string> output_blog_sub_without_path,
string output_archive_without_path,
string output_tags_without_path){
if(!create_dir(output_pre_with_path)){
return false;
}
if(!create_dir(output_pre_with_path + output_blog_without_path)){
return false;
}
vector<string>::iterator it;
for(it = output_blog_sub_without_path.begin();
it != output_blog_sub_without_path.end();
it++){
if(!create_dir(output_pre_with_path + output_blog_without_path + (*it))){
return false;
}
}
if(!create_dir(output_pre_with_path + output_archive_without_path)){
return false;
}
if(!create_dir(output_pre_with_path + output_tags_without_path)){
return false;
}
return true;
}
static bool create_nginx_blog_without_md(string md_file_dir_with_path,
string html_title,
string blog_name,
string blog_subname,
int max_item_per_page,
blog_home_page* blog_home){
bool ret = blog_home->get_blog_dir(md_file_dir_with_path);
if(!ret){
cout << "can not get blog dir " << md_file_dir_with_path << endl;
return false;
}
ret = blog_home->get_blog_files_attr();
if(!ret){
cout << "can not get all blog files attr\n";
return false;
}
ret = blog_home->create_blog_header(html_title,blog_name,blog_subname);
if(!ret){
cout << "can not create blog header\n";
return false;
}
ret = blog_home->create_blog_left_sidebar();
if(!ret){
cout << "can not create blog left sidebar\n";
return false;
}
ret = blog_home->create_blog_right_sidebar();
if(!ret){
cout << "can not create blog right sidebar\n";
return false;
}
ret = blog_home->create_blog_ending();
if(!ret){
cout << "can not create blog ending\n";
return false;
}
ret = blog_home->create_blog_index_contents(max_item_per_page);
if(!ret){
cout << "can not create index contents\n";
return false;
}
ret = blog_home->create_blog_archive_contents(max_item_per_page);
if(!ret){
cout << "can not create archive contents\n";
return false;
}
ret = blog_home->create_blog_tags_contents(max_item_per_page);
if(!ret){
cout << "can not create tags contents\n";
return false;
}
ret = blog_home->create_blog_index_page();
if(!ret){
cout << "can not create blog index page\n";
return false;
}
ret = blog_home->create_blog_archive_page();
if(!ret){
cout << "can not create blog archive page\n";
return false;
}
ret = blog_home->create_blog_tags_page();
if(!ret){
cout << "can not create blog tags page\n";
return false;
}
return true;
}
static bool output_index_html(string output_dir_with_path,
int max_item_per_page,
blog_home_page* blog_home){
//create index html
ofstream out_file;
string out_file_name;
string index = string("index");
string post = string(".html");
vector<string>::iterator it;
int i = 0;
int max_page = blog_home->blog_files.size() / max_item_per_page;
if((blog_home->blog_files.size() % max_item_per_page) > 0 ){
max_page = max_page + 1;
}
for(it = blog_home->html_index_page_v.begin();
it != blog_home->html_index_page_v.end();
it++){
if(i == 0){
out_file_name = output_dir_with_path + index + post;
out_file.open(out_file_name);
}
else{
out_file_name = output_dir_with_path + index + string("_")
+ to_string(i + 1) + post;
out_file.open(out_file_name);
}
out_file << (*it);
out_file.close();
if((i+1) == max_page){
out_file_name = output_dir_with_path + index + string("_last") + post;
out_file.open(out_file_name);
out_file << (*it);
out_file.close();
}
i = i + 1;
}
return true;
//end create index html
}
static bool output_archive_html(string output_dir_with_path,
int max_item_per_page,
blog_home_page* blog_home){
//create archive html
ofstream out_file;
string out_file_name;
string post = string(".html");
map<string,string,less<string> >::iterator itm;
int max_page = blog_home->blog_files.size() / max_item_per_page;
if((blog_home->blog_files.size() % max_item_per_page) > 0 ){
max_page = max_page + 1;
}
for(itm = blog_home->html_archive_page_vv.begin();
itm != blog_home->html_archive_page_vv.end();
itm++){
out_file_name = output_dir_with_path + itm->first + post;
out_file.open(out_file_name);
out_file << itm->second;
out_file.close();
}
return true;
//end create archive html
}
static bool output_tags_html(string output_dir_with_path,
blog_home_page* blog_home){
//create tag html
ofstream out_file;
string out_file_name;
string post = string(".html");
map<string,string,less<string> >::iterator itm;
for(itm = blog_home->html_tags_page_vv.begin();
itm != blog_home->html_tags_page_vv.end();
itm++){
out_file_name = output_dir_with_path + itm->first + post;
out_file.open(out_file_name);
out_file << itm->second;
out_file.close();
}
return true;
//end create tag html
}
static bool create_output_all_md_blog_html(string output_dir_with_path,
string html_title,
string blog_name,
string blog_subname,
blog_home_page* blog_home){
//create all html
ofstream out_file;
string out_file_name;
string post = string(".html");
string html_name;
string html_contents;
multimap<string,blog_file_attr_t,less<string> >::iterator itd;
int pos = 0;
for(itd = blog_home->tags_mmap_blog_file_attr.begin();
itd != blog_home->tags_mmap_blog_file_attr.end();
itd++){
html_name = itd->second.blog_file_time.blog_file_name;
pos = html_name.find_last_of(".");
out_file_name = output_dir_with_path + html_name.substr(0,pos) + post;
out_file.open(out_file_name);
if(blog_home->create_md_header(html_title,
blog_name,
blog_subname,
itd->second)){
html_contents = blog_home->html_md_header;
}
else{
cout << "error: create md header " << itd->second.blog_file_time.blog_file_name << endl;
}
if(blog_home->create_md_contens(itd->second)){
html_contents = html_contents +
string("<div class=\"main-page-box\">\n") +
blog_home->html_md_contents +
blog_home->html_md_catalog +
blog_home->html_right_sidebar + string("</div>\n")
+ blog_home->html_ending;
}
else{
cout << "error 1: create md contents " << itd->second.blog_file_time.blog_file_name << endl;
}
out_file << html_contents;
out_file.close();
}
return true;
//end create all html
}
static bool create_out_a_md_blog_html(string src_dir_with_path,
string md_name_with_post,
string html_title,
string blog_name,
string blog_subname,
string dst_dir_with_path,
blog_home_page* blog_home){
//create md html
if(md_name_with_post.empty()){
return true;
}
string post = string(".html");
string html_name;
string html_contents;
int pos = 0;
html_name = md_name_with_post;
pos = html_name.find_last_of(".");
string out_file_name = dst_dir_with_path + html_name.substr(0,pos) + post;
ofstream out_file;
out_file.open(out_file_name);
blog_file_attr_t bfa;
if(!blog_home->get_a_blog_file_attr(&bfa,(src_dir_with_path + md_name_with_post))){
cout << "can not get the attr of " << (src_dir_with_path + md_name_with_post) << endl;
out_file.close();
return false;
}
if(blog_home->create_md_header(html_title,
blog_name,
blog_subname,
bfa)){
html_contents = blog_home->html_md_header;
}
else{
cout << "error: when create md header of " << src_dir_with_path + md_name_with_post << endl;
out_file.close();
return false;
}
if(blog_home->create_md_contens(bfa)){
html_contents = html_contents +
string("<div class=\"main-page-box\">\n") +
blog_home->html_md_contents +
blog_home->html_md_catalog +
blog_home->html_right_sidebar + string("</div>\n")
+ blog_home->html_ending;
}
else{
cout << "error : when create md contents of" << src_dir_with_path + md_name_with_post << endl;
out_file.close();
return false;
}
out_file << html_contents;
out_file.close();
return true;
//end create md html
}
static bool create_out_about_me_html(string src_dir_with_path,
string md_name_with_post,
string html_title,
string blog_name,
string blog_subname,
string dst_dir_with_path,
blog_home_page* blog_home){
//create md html
string post = string(".html");
string html_name;
string html_contents;
int pos = 0;
html_name = md_name_with_post;
html_title = "关于我";
pos = html_name.find_last_of(".");
string out_file_name = dst_dir_with_path + html_name.substr(0,pos) + post;
ofstream out_file;
out_file.open(out_file_name);
blog_file_attr_t bfa;
if(!blog_home->get_a_blog_file_attr(&bfa,(src_dir_with_path + md_name_with_post))){
cout << "can not get the attr of " << (src_dir_with_path + md_name_with_post) << endl;
out_file.close();
return false;
}
bfa.blog_file_time.blog_file_name = string("about_me.md");
if(blog_home->create_md_header(html_title,
blog_name,
blog_subname,
bfa)){
html_contents = blog_home->html_md_header;
}
else{
cout << "error: when create md header of " << src_dir_with_path + md_name_with_post << endl;
out_file.close();
return false;
}
if(blog_home->create_md_contens(bfa)){
blog_home->html_right_sidebar = string("<div class=\"right-sidebar\">"
"<h3> </h3>\n</div>");
blog_home->html_md_catalog = string("<div class=\"left-sidebar\">"
"<h3> </h3>\n</div>");
html_contents = html_contents +
string("<div class=\"main-page-box\">\n") +
blog_home->html_md_contents +
blog_home->html_md_catalog +
blog_home->html_right_sidebar + string("</div>\n")
+ blog_home->html_ending;
}
else{
cout << "error : when create md contents of" << src_dir_with_path + md_name_with_post << endl;
out_file.close();
return false;
}
out_file << html_contents;
out_file.close();
return true;
//end create md html
}
C++
1
https://gitee.com/iamdsy/nginx-blog.git
git@gitee.com:iamdsy/nginx-blog.git
iamdsy
nginx-blog
nginx-blog
master

搜索帮助