Initial Commit
This commit is contained in:
commit
30c21d2272
3
Readme.md
Normal file
3
Readme.md
Normal file
|
@ -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.
|
199
main.cpp
Normal file
199
main.cpp
Normal file
|
@ -0,0 +1,199 @@
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <stack>
|
||||||
|
#include <fstream>
|
||||||
|
#include <windows.h>
|
||||||
|
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<FILE> 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<string>& vec,vector<string>& 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("<a href=",lastidx))!=string::npos)
|
||||||
|
{
|
||||||
|
string linktab=text.substr(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<<text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _ConvertMarkdownToHTML_Real(const string& SourceMarkdown,const string& TargetHTML)
|
||||||
|
{
|
||||||
|
string command=string("D:\\pandoc.exe -f markdown -t html ")+SourceMarkdown+" -o "+TargetHTML;
|
||||||
|
printf("Executing...: %s\n",command.c_str());
|
||||||
|
system(command.c_str());
|
||||||
|
_ChangeHtmlLink(TargetHTML);
|
||||||
|
}
|
||||||
|
/// Convert Markdown to HTML
|
||||||
|
void ConvertMarkdown(const vector<string>& vec,const vector<string>& 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<string> mdvec;
|
||||||
|
vector<string> 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;
|
||||||
|
}
|
Reference in New Issue
Block a user