summaryrefslogtreecommitdiff
path: root/src/markdown.cpp
blob: c542a1fca57956d634e8738161e837d606b33c54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "markdown.hpp"


TexoProducerMarkdown::TexoProducerMarkdown(TexoExporter &exporter):
    TexoProducer(exporter), quoted(false), newline(false), code(false)
{}

void TexoProducerMarkdown::Put(const Texo &piece)
{
    if (piece.c == '\n') {
        if (code) {
            exporter.Put('\n');
        } else if (quoted) {
            exporter.Put("\n>");
        } else if (header || newline) {
            exporter.Put(' ');
        } else {
            exporter.Put('\n');
            newline = true;
        }
    } else {
        exporter.Put(piece.c);
        newline = false;
    }
}

void TexoProducerMarkdown::Put(const TexoHeader &piece)
{
    if (piece.closing) {
        exporter.Put('\n');
        header  = false;
        newline = true;
    } else {
        if (!newline) {
            exporter.Put('\n');
        }
        if (piece.level <= 1) {
            exporter.Put("###### ");
        } else if (piece.level == 2) {
            exporter.Put("##### ");
        } else if (piece.level == 3) {
            exporter.Put("#### ");
        } else if (piece.level == 4) {
            exporter.Put("### ");
        } else if (piece.level == 5) {
            exporter.Put("## ");
        } else {
            exporter.Put("# ");
        }
        header = true;
    }
}

void TexoProducerMarkdown::Put(const TexoParagraph &piece)
{
    if (piece.closing) {
        exporter.Put("\n\n");
        newline = true;
    }
}

void TexoProducerMarkdown::Put(const TexoCode &piece)
{
    exporter.Put("\n```\n");
    code = !code;
}

void TexoProducerMarkdown::Put(const TexoQuote &piece)
{
    if (piece.closing) {
        exporter.Put("\n\n");
        quoted  = false;
        newline = true;
    } else {
        if (!newline) {
            exporter.Put('\n');
        }
        exporter.Put(">");
        quoted = true;
    }
}

void TexoProducerMarkdown::Put(const TexoMono &piece)
{
    exporter.Put("`");
}

void TexoProducerMarkdown::Put(const TexoBold &piece)
{
    exporter.Put("**");
}

void TexoProducerMarkdown::Put(const TexoItalic &piece)
{
    exporter.Put("*");
}

void TexoProducerMarkdown::Put(const TexoUnderline &piece)
{
    exporter.Put("++");
}

void TexoProducerMarkdown::Put(const TexoStrike &piece)
{
    exporter.Put("~~");
}

void TexoProducerMarkdown::Put(const TexoImage &piece)
{
    if (piece.path != "") {
        const bool link = piece.link != "";
        if (link) {
            exporter.Put('[');
        }
        exporter.Put("![");
        exporter.Put(piece.alt);
        exporter.Put("](");
        exporter.Put(piece.path);
        if (piece.title != "") {
            exporter.Put(" \"");
            exporter.Put(piece.title);
            exporter.Put('"');
        }
        exporter.Put(')');
        if (link) {
            exporter.Put("](");
            exporter.Put(piece.link);
            if (piece.title != "") {
                exporter.Put(" \"");
                exporter.Put(piece.title);
                exporter.Put('"');
            }
            exporter.Put(')');
        }
    }
}

void TexoProducerMarkdown::Put(const TexoLink &piece)
{
    if (piece.text != "") {
        exporter.Put('[');
        exporter.Put(piece.text);
        exporter.Put("](");
        exporter.Put(piece.link);
        if (piece.title != "") {
            exporter.Put(" \"");
            exporter.Put(piece.title);
            exporter.Put('"');
        }
        exporter.Put(')');
    }
}

void TexoProducerMarkdown::Put(const TexoLineBreak &piece)
{
    exporter.Put("\\\n");
    newline = true;
}

void TexoProducerMarkdown::Put(const TexoHorizontalRule &piece)
{
    if (!newline) {
        exporter.Put('\n');
    }
    exporter.Put("---\n");
    newline = false;
}