201703-3 markdown

实现

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

string line;
int line_index = 0;

void handle_link() {
    ++line_index;

    char text[255]; int text_index = 0;
    while (line[line_index] != ']') {
        text[text_index++] = line[line_index++];
    }

    line_index+=2;

    char link[255]; int link_index = 0;
    while (line[line_index] != ')') {
        link[link_index++] = line[line_index++];
    }
    ++line_index;


    cout << "<a href="" ; 
    for (int i = 0;i < link_index;++i) {
        if (link[i] != '_') {
            cout << link[i];
        } else {
            cout << "<em>";
            ++i;
            while (link[i] != '_') {
                cout << link[i++];
            }
            cout << "</em>";
        }
    }
    
    cout << "">";

    for (int i = 0;i < text_index;++i) {
        if (text[i] != '_') {
            cout << text[i];
        } else {
            cout << "<em>";
            ++i;
            while (text[i] != '_') {
                cout << text[i++];
            }
            cout << "</em>";
        }
    }
    cout << "</a>";
}

void handle_emp() {
    ++line_index;
    cout << "<em>";

    while (line_index < line.size()) {
        switch (line[line_index])
        {
            case '_':
                ++line_index;
                cout << "</em>";
                return;
            case '[':
                handle_link();
                break;
            default:
                cout << line[line_index++];
                break;
        }
    }
}

void handle_line() {
	while (line_index < line.size()) {
	    switch (line[line_index])
	    {
	        case '_':
	            handle_emp();
	            break;
	        case '[':
	            handle_link();
	            break;
	        default:
	            if (line_index == line.size()) {
	                return;
	            } else {
	                cout << line[line_index++];
	            }
	            break;
	        if (line_index == line.size()) { 
	            return;
	        }
	    }
	
	}
}

int main() {
    
    while (getline(cin,line)){ 

        line_index = 0;

        handle_begin:;  

        if (line.empty()) {
            continue;
        } else if (line[0] == '#') {
            int sharp_cnt = 0;
            while(line[line_index++] == '#') {
                sharp_cnt+=1;
            }
            cout << "<h" << sharp_cnt << ">";

            while (isspace(line[line_index])) {
                ++line_index;
            }
            handle_line();
            cout << "</h" << sharp_cnt << ">" << endl;
        } else if (line[0] == '*') {
            cout << "<ul>" << endl;

            handle_list:;

            ++line_index;
            while(isspace(line[line_index])) {
                ++line_index;
            }

            cout << "<li>";
            handle_line();
            cout << "</li>" << endl;

            if (getline(cin,line)) {
                line_index = 0;
                if (line.empty()||line[0] != '*') {
                    cout << "</ul>" << endl;
                    goto handle_begin;
                } else {
                    goto handle_list;
                }
            }
            cout << "</ul>";
            break;
        } else {
            cout << "<p>";
            
            handle_content:;

            handle_line();

            if (getline(cin,line)) {
                line_index = 0;
                if (line.empty() || line[0] == '*' || line[0] == '#') {
                    cout << "</p>" << endl;
                    goto handle_begin;
                } else {
                    cout << endl;
                    goto handle_content;
                }
            }
            cout << "</p>";
            break;
        }

    }
}

原文地址:https://www.cnblogs.com/amonqsq/p/13610991.html