使用C++编写一个网页修改辅助程序
因为本网站是纯静态的,而且本人编写网页也是很不专业的。当需要更新网页文章的时候,需要自己手动添加很多重复的代码内容。考虑到这些工作是重复 的,所以打算写一个辅助我自己更新网站的程序。刚好学习的C++也有很久没用过了,就用C++来编写这个程序。用C++来生成HTML代码的难点就是拼接字符串,但是C++有关于string的库,所以这个工作就简单了:基本就是输出文字,大量的cout。逻辑上需要分类文章 、用程序拼接出文章的网址链接,最后生成HTML的代码。 分段程序及思路:
int main()
{
string path;
int n,end=0;
while(end==0)
{
cout << "**********博客更新辅助程序*************" << endl;
//cin >> digit;
cout << "--> 请输入文章标题:";
cin >> title;
cout << "--> 请输入源文件名称(去掉“.html”):";
cin >> name;
cout << "--> 请输入文章时间“年”:";
cin >> year;
cout << "--> 请输入文章时间“月”:";
cin >> month;
cout << "--> 请输入文章时间“日”:";
cin >> day;
cout << "文章分类代码:" << endl;
cout << "1:STC89C" << endl;
cout << "2:32位单片机" << endl;
cout << "3:FPGA" << endl;
cout << "4:模块" << endl;
cout << "5:原理图与PCB" << endl;
cout << "6:服务器" << endl;
cout << "7:应用和程序" << endl;
cout << "8:项目" << endl;
cout << "9:其他随笔" << endl;
cout << "--> 请输入文章分类:";
cin >> type;
cout << "--> 请输入文章简介:";
cin >> summary;
cout << " " << endl;
cout << "***********修改内容,共三步。************" << endl;
cout << "----> 【一】插入到对应LCPageX的内容:" << endl;
learning_content(title,name,year,month,day,type);
cout << " " << endl;
cout << "----> 【二】插入到“归档”代码段的内容。应当归纳于:";
cout << place << endl;
classify(title,name,type);
cout << "----> 【三】加入“推荐”代码段中。" << endl;
suggest(title,name,type);
cout << "****************************************" << endl;
path = judgment(name,type);
cout << "----> 上传文章所需图片到对应图片存放目录。" << endl;
cout << "----> 最后上传文章到:";
cout << path << endl;
cout << "***********目录设计***************" << endl;
cout << "--> 请输入目录中有几项:";
cin >> n;
if(n!=0) catalog(n);
cout << "是否结束本进程?(结束输入1,重启输入0)";
cin >> end;
}
}
//生成文章链接,并分类
string judgment(string name1,int type1)
{
string slink;
switch(type1)
{
case 1:
{
slink = "\"https://lytnib.top/article/STC89C/";
slink.append(name1);
slink.append(".html\"");
place = "STC89C";
break;
}
case 2:
{
slink = "\"https://lytnib.top/article/32bit/";
slink.append(name1);
slink.append(".html\"");
place = "32位单片机";
break;
}
//case 3:... case 4:...
default: break;
}
return slink;
}
//生成用于主页展示的HTML代码
void learning_content(string title1,string name1,int year1,int month1,int day1,int type1)
{
string s1;
cout << "" << endl;
cout << " " << endl;
cout << " " << endl;
s1 = judgment(name1,type1);
cout << " ";
cout << title1;
cout << "" << endl;
cout << "
" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ";
cout << summary;
cout << "Read more
" << endl;
cout << " " << endl;
}
//生成用于“归档”的HTML代码
void classify(string title1,string name1,int type1)
{
string s1;
s1 = judgment(name1,type1);
cout << "";
cout << title1;
cout << "
" << endl;
}
//生成用于“推荐”的HTML代码
void suggest(string title1,string name1,int type1)
{
string s1;
s1 = judgment(name1,type1);
cout << ""; cout << title1; cout << "
" << endl; cout << "
//生成文章的目录
void catalog(int n)
{
int i;
string cata1[n],cata2[n];
for(i=0;i<n;i++)
{
cout << "--> 输入项" << i+1 << "的标记文字:";
cin >> cata1[i];
cout << "--> 输入对应目录上显示的文字:";
cin >> cata2[i];
}
cout << "***************目录*****************" << endl;
for(i=0;i<n;i++)
{
cout << "***********修改内容,共三步。************ ----> 【一】插入到对应LCPageX的内容: <article class="format-standard type-post hentry clearfix"> <header class="clearfix"> <h3 class="post-title"> <a href="https://lytnib.top/article/32bit/musicwater.html">基于STM32的音乐喷泉</a> </h3> <div class="post-meta clearfix"> <span class="date">2023/3/5</span> <span class="category"><a href="https://lytnib.top/article/32bit/musicwater.html" title="位置">位置:32位单片机</a></span> </div> </header> <p>【具体的简介内容】<a class="readmore-link" href="https://lytnib.top/article/32bit/musicwater.html">Read more</a></p> </article> ----> 【二】插入到“归档”代码段的内容。应当归纳于:32位单片机 <a href="https://lytnib.top/article/32bit/musicwater.html">基于STM32的音乐喷泉</a><br> ----> 【三】加入“推荐”代码段中。 <li class="article-entry standard"> <h4><a href="https://lytnib.top/article/32bit/musicwater.html">基于STM32的音乐喷泉</a></h4> </li> **************************************** ----> 上传文章所需图片到对应图片存放目录。 ----> 最后上传文章到:"https://lytnib.top/article/32bit/musicwater.html" ***************目录***************** <li><a href="#1">yi</a></li> <li><a href="#2">er</a></li> <li><a href="#3">san</a></li>这样的工具就帮助我节省了很多时间。写完文章只要填入必要的信息后,直接粘贴生成的代码即可。