(ccf)201703-3markdown

#include<iostream>  
#include<memory.h>
#include<stack>
#include<string>
#include<cmath>
#include<map>
#include<algorithm> 
#include<sstream>
#include<set>
#include<queue>

using namespace std;

//處理超連接部分 
string processA(string text)
{
    int begin,end;
    for(int i=0;i<text.length();i++)
    {
        if(text[i]=='[')
        {
            string ch,link;
            bool chend=false;
            bool linkend=true;;
            begin=i;
            for(int j=i+1;j<text.length();j++)
            {
                if(text[j]==']')
                {
                    chend=true;
                    continue;
                }
                if(text[j]=='(')
                {
                    linkend=false;
                    continue;
                }
                if(text[j]==')')
                {
                    end=j;
                    break;
                }
                
                if(!chend)
                    ch+=text[j];
                if(!linkend)
                    link+=text[j];
            }
            string newstr="<a href=\""+link+"\">"+ch+"</a>";
            text.replace(begin,end-begin+1,newstr);
        }
    }
    return text;    
}

//處理着重號部分 
string processK(string text)
{
    int begin,end;
    for(int i=0;i<text.length();i++)
    {
        if(text[i]=='_')
        {
            begin=i;
            string k="";
            for(int j=i+1;j<text.length();j++)
            {
                if(text[j]=='_')
                {
                    end=j;
                    break;
                }
                k+=text[j];
            }
        string newstr="<em>"+k+"</em>";
        text.replace(begin,end-begin+1,newstr);
        }
    }
    return text;
}

//處理三種區塊部分 
void processQ(queue<string> &q,queue<string> &r)
{
    string front=q.front();
    if(front[0]=='*')
    {
        string newstr="<ul>\n";
        while(!q.empty())
        {
            string t=q.front();
            q.pop();
            int count=0;
            for(int i=0;i<t.length();i++)
            {
                if(t[i]==' '||t[i]=='*')
                    count++;
                else
                    break;
            }
            t.erase(0,count);
            t=processA(t);
            t=processK(t);
            t="<li>"+t+"</li>\n";
            newstr+=t;
        }
        newstr+="</ul>";
        r.push(newstr);
    }
    else if(front[0]=='#')
    {
        while(!q.empty())
        {
            string head=q.front();
        q.pop();
        head=processA(head);
        head=processK(head);
        int h=0;
        int space=0;
        for(int i=0;i<head.length();i++)
        {
            if(head[i]=='#')
                h++;
            else if(head[i]==' ')
                space++;
            else
                break;              
        }
        head.erase(0,h+space);
        ostringstream s;
        s<<h;
        string H=s.str();
        head="<h"+H+">"+head+"</h"+H+">";
        r.push(head);
        }
        
    }
    else
    {
        string newstr="<p>";
        while(!q.empty())
        {
            string p = q.front();
            q.pop();
            p = processA(p);
            p = processK(p);
            if(!q.empty())
                p+="\n";
            newstr+=p;
        }
        newstr=newstr+"</p>";
        r.push(newstr);
    }
}

//讀取和輸出 
int main()
{
    string line;
    queue<string> q;
    queue<string> r;
    while(getline(cin,line))
    {
        if(line=="")
        {
            if(q.empty())
                continue;
            else
                processQ(q,r);
            continue;   
        }
        q.push(line);
    }
    if(!q.empty())
        processQ(q,r);
    while(!r.empty())
    {
        cout<<r.front()<<endl;;
        r.pop();
    }
    return 0;
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息