commit 30c21d2272b6f5307b2075925f6ec11af22d3ab9 Author: kiritow <1362050620@qq.com> Date: Tue Jun 20 11:44:04 2017 +0800 Initial Commit diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e0274bd --- /dev/null +++ b/Readme.md @@ -0,0 +1,3 @@ +# Markdown2Html + +This simple program is powered by Pandoc. It use pandoc to convert markdown to html. This program just list all markdown files and send them to pandoc. Also, links to markdown files will be changed to links to html files. diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6baa20c --- /dev/null +++ b/main.cpp @@ -0,0 +1,199 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +char buff[1024]; +string basic_markdown_dir; +string basic_html_dir; + +void ReadConfig() +{ + FILE* fp=fopen("config.txt","r"); + if(fp==nullptr) + { + printf("Error: Failed to read config.txt...\n"); + exit(0); + } + shared_ptr sfp(fp,fclose); + while(1) + { + memset(buff,0,1024); + if(fgets(buff,1000,fp)==nullptr) + { + printf("Info: Reach End of file.\n"); + break; + } + else + { + if(buff[1000]) + { + printf("Error: Encounter a long line.\n"); + exit(0); + } + int sz=strlen(buff); + if(buff[sz-1]=='\n') buff[sz-1]=0; + + string s(buff); + size_t idx=s.find('='); + if(idx==string::npos) + { + printf("Warning: Key-Value separator not found in this line, skip.\n"); + continue; + } + string key=s.substr(0,idx); + string value=s.substr(idx+1); + + if(key=="MDDIR") /// Markdown DIR + { + basic_markdown_dir=value; + } + else if(key=="HTMLDIR") + { + basic_html_dir=value; + } + } + } +} +/// Search .md and generate a list. +void SearchMarkdownDir(const string& patternStr,const string& currentDir,vector& vec,vector& DirToMake) +{ + printf("Debug: Searching Pattern [%s] CurrentDir [%s] \n",patternStr.c_str(),currentDir.c_str()); + WIN32_FIND_DATA fnd; + HANDLE hand=FindFirstFile(patternStr.c_str(),&fnd); + if(hand!=INVALID_HANDLE_VALUE) + { + do + { + if(strcmp(fnd.cFileName,".")==0||strcmp(fnd.cFileName,"..")==0) + { + printf("Debug: Found . or ..\n"); + continue; + } + string filefullname=currentDir+"\\"+fnd.cFileName; + if(fnd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + /// It is a subdir. + printf("Debug: Found Dir %s\n",fnd.cFileName); + DirToMake.push_back(filefullname); + SearchMarkdownDir(filefullname+"\\*",filefullname,vec,DirToMake); + } + else + { + int sz=strlen(fnd.cFileName); + if(strcmp(fnd.cFileName+sz-3,".md")==0) + { + printf("Found: %s\n",filefullname.c_str()); + /// Yes, that's what we want. + vec.push_back(filefullname); + } + } + } + while(FindNextFile(hand,&fnd)); + FindClose(hand); + } + else + { + printf("Debug: Invalid Handle.\n"); + } +} + +void _ChangeHtmlLink(const string& htmlfile) +{ + printf("Info: Changing HTML Link [%s]\n",htmlfile.c_str()); + + ifstream ifs(htmlfile); + string text,temp; + while(getline(ifs,temp)) text.append(temp); + ifs.close(); + size_t lastidx=0; + size_t idx=0; + int cnt=0; + while((idx=text.find("",idx)-idx+1); + printf("Info: Found LinkTab %s\n",linktab.c_str()); + size_t tidx; + if((tidx=linktab.find(".md"))!=string::npos) + { + string newtab=linktab; + newtab.replace(tidx,3,".html"); + text.replace(idx,linktab.size(),newtab); + printf("Info: LinkTab --> %s\n",newtab.c_str()); + cnt++; + printf("Info: %d Link Changed.\n",cnt); + } + lastidx=idx+1; + } + if(cnt) + { + printf("Info: Totally %d link changed.\n",cnt); + ofstream ofs(htmlfile); + ofs<& vec,const vector& DirToCreate) +{ + for(auto iter=DirToCreate.begin();iter!=DirToCreate.end();++iter) + { + printf("Info: Creating Directory [%s]\n",iter->c_str()); + string dststr=basic_html_dir+iter->substr(basic_markdown_dir.size(),iter->size()-basic_markdown_dir.size()); + /// Create Directory. + CreateDirectory(dststr.c_str(),NULL); + } + for(auto iter=vec.begin();iter!=vec.end();++iter) + { + string dststr=basic_html_dir+iter->substr(basic_markdown_dir.size(),iter->size()-basic_markdown_dir.size()-3)+".html"; + _ConvertMarkdownToHTML_Real(*iter,dststr); + } +} + +int main() +{ + printf("======================\n" + "MarkdownBlogEngine Started.\n" + "Written by Kiritow.\n" + "Powered by Pandoc.\n" + "HC 2017 | HC TECH 2017 \n" + "=======================\n"); + printf("Reading config.txt...\n"); + ReadConfig(); + if(basic_markdown_dir.empty ()||basic_html_dir.empty()) + { + printf("Error: Markdown/HTML Dir is Empty.\n"); + return 0; + } + else + { + printf("Info: Markdown DIR: %s \nInfo: HTML DIR: %s\n",basic_markdown_dir.c_str(),basic_html_dir.c_str()); + } + vector mdvec; + vector dirvec; + string patternstr=basic_markdown_dir+"\\*"; + SearchMarkdownDir(patternstr,basic_markdown_dir,mdvec,dirvec); + if(mdvec.empty()) + { + printf("Error: No Markdown File found.\n"); + return 0; + } + printf("Info: Start Converting...\n"); + ConvertMarkdown(mdvec,dirvec); + printf("Info: Convert Finished.\n"); + return 0; +}