Labs 11 & 12. Markdown

Labs 11 & 12. Markdown

Task 1 (Identifying headers)


#include <iostream>
#include <string>
#include <cctype>

using namespace std;

// returns header level 1-6, or 0 if it's not a header
int header_level(string &s);

const int NOT_HEADER = 0;

int main() {

    string s;

    while(getline(cin, s)) {
        int lvl = header_level(s);
        if (lvl != NOT_HEADER) {
            // header
            cout << "<h" << lvl << ">" << s.substr(lvl, s.length() - lvl)
                 << "</h" << lvl << ">" << endl;
        }
        else {
            // not a header
            cout << s << endl;
        }
    }
}

int header_level(string &s) {
    int i = 0;
    while (i < s.length() && s[i] == '#') {
        i++;
        if (i > 6) return 0;
    }
    return i;
}

Task 2 & 3. (Paragraphs and emphasis)

We keep track of the current state of the system using two Boolean variables:


#include <iostream>
#include <string>
#include <cctype>

using namespace std;

// returns true if s contain only whitespace
bool is_empty(string &s);

// returns header level 1-6, or 0 if it's not a header
int header_level(string &s);

const int NOT_HEADER = 0;

int main() {

    string s;

    // Two state variables:
    bool mid_para = false; // true if <p> tag is open

    bool emph = false;     // true if <em> tag is open

    while(getline(cin, s)) {
        int lvl = header_level(s);
        if (lvl != NOT_HEADER) {
            // header
            if (emph) {
                cout << "</em>"; // close <em>
                emph = false;
            }
            if (mid_para) { // close the paragraph
                cout << "</p>" << endl;
                mid_para = false;
            }
            cout << "<h" << lvl << ">" << s.substr(lvl, s.length() - lvl)
                 << "</h" << lvl << ">" << endl;
        }
        else {
            // not a header
            if (is_empty(s)) {
                // empty line
                if (emph) {
                    cout << "</em>";
                    emph = false;
                }
                if (mid_para) {
                    cout << "</p>" << endl;
                    mid_para = false;
                }
            }
            else {
                // non-empty line
                if (!mid_para) {
                    cout << "<p>";
                    mid_para = true;
                }
                // print line
                for (unsigned int i = 0; i < s.length(); i++) {
                    if (s[i] == '*') {
                        emph = !emph;
                        if (emph) {
                            cout << "<em>";
                        }
                        else {
                            cout << "</em>";
                        }
                    }
                    else {
                        cout << s[i];
                    }
                }
                cout << endl;

            }
        }
    }

    // end of file
    if (emph) {
        cout << "</em>";
        emph = false;
    }
    if (mid_para) {
        cout << "</p>";
        mid_para = false;
    }
}

bool is_empty(string &s) {
    for (unsigned int i = 0; i < s.length(); i++) {
        if (!isspace(s[i])) return false;
    }
    return true;
}

int header_level(string &s) {
    unsigned int i = 0;
    while (i < s.length() && s[i] == '#') {
        i++;
        if (i > 6) return 0;
    }
    return i;
}

Input:

# Intro to HTML
We are going to consider only very basic features of HTML. 
For text formatting, you use special tags (*markup elements enclosed
between angular brackets*).

Each paragraph should be enclosed in its own paragraph tag (p). 
A paragraph can span multiple lines: the browser wraps the lines 
automatically. Multiple whitespace characters and new line characters 
are discarded by the browser, so you have to rely on the paragraph 
tags to format the page. 

## Adding emphasis

The tag em can be used to *emphasize certain phrases*, the browser 
shows it with the *italic* typeface.
### Handling errors in formatting
A valid HTML document *must have all open tags closed*. This is
why this implementation closes an open (em) tag by the end of 
the paragraph *even if it does not see a closing asterisk. 

Another *test.

Output:

<h1> Intro to HTML</h1>
<p>We are going to consider only very basic features of HTML. 
For text formatting, you use special tags (<em>markup elements enclosed
between angular brackets</em>).
</p>
<p>Each paragraph should be enclosed in its own paragraph tag (p). 
A paragraph can span multiple lines: the browser wraps the lines 
automatically. Multiple whitespace characters and new line characters 
are discarded by the browser, so you have to rely on the paragraph 
tags to format the page. 
</p>
<h2> Adding emphasis</h2>
<p>The tag em can be used to <em>emphasize certain phrases</em>, the browser 
shows it with the <em>italic</em> typeface.
</p>
<h3> Handling errors in formatting</h3>
<p>A valid HTML document <em>must have all open tags closed</em>. This is
why this implementation closes an open (em) tag by the end of 
the paragraph <em>even if it does not see a closing asterisk. 
</em></p>
<p>Another <em>test.
</em></p>