Pro3
原始文件为 CPP 代码,本文是转换后的 Markdown 文件。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
void readline(string line);
void dealwith_header(string line);
void dealwith_order(string line);
bool dealwith_order_sub(string line);
void dealwith_text(string line);
bool dealwith_text_sub(string line);
void dealwith_content(string line);
int main()
{
freopen("in.txt","r",stdin);
string line;
while(getline(cin,line))
{
readline(line);
}
}
void readline(string line)
{
char c = line[0];
if (c == '#')
dealwith_header(line);
else if (c == '*')
dealwith_order(line);
else if (c == 0)
return;
else
dealwith_text(line);
}
void dealwith_text(string line)
{
printf("<p>");
dealwith_content(line);
do
{
if(!getline(cin,line))
{
line[0] = 0;
break;
}
}while(dealwith_text_sub(line));
printf("</p>\n");
//cout << line << endl;
readline(line);
}
bool dealwith_text_sub(string line)
{
if (line[0] == 0 || line[0] == '*' || line[0] == '#') return false;
printf("\n");
dealwith_content(line);
return true;
}
void dealwith_header(string line)
{
int headnum = 0; while(line[++headnum] == '#');
printf("<h%d>",headnum);
int pos = headnum; while(line[pos++] == ' ');
line = line.substr(pos-1,line.size());
dealwith_content(line);
printf("</h%d>\n",headnum);
if(getline(cin,line)) readline(line);
}
void dealwith_order(string line)
{
printf("<ul>\n");
while(dealwith_order_sub(line))
if(!getline(cin,line)) line[0]=0;
printf("</ul>\n");
readline(line);
}
bool dealwith_order_sub(string line)
{
if(line[0] != '*') return false;
int pos = 1; while(line[pos++] == ' ');
line = line.substr(pos-1,line.size());
printf("<li>");
dealwith_content(line);
printf("</li>\n");
return true;
}
void dealwith_content(string line)
{
int size = line.size();
bool impLeft = true;
for (int i = 0; i < size; ++i)
{
if (line[i] == '_')
{
if (impLeft == true)
printf("<em>");
else
printf("</em>");
impLeft = !impLeft;
}
else if (line[i] == '[')
{
int pos = line.find(']',i)-1;
string text = line.substr(i+1,pos-i);
int pos1 = line.find('(',pos)+1;
int pos2 = line.find(')',pos);
string link = line.substr(pos1,pos2-pos1);
cout << "<a href=\"";
dealwith_content(link);
cout << "\">";
dealwith_content(text);
cout << "</a>";
i = pos2;
}
else
printf("%c",line[i]);
}
}