| 54 | ~MarkdownConverter() = default; |
| 55 | |
| 56 | QString toHtml(const QString& markdown) |
| 57 | { |
| 58 | const QRegularExpression hRE(QStringLiteral("(#+) (.+)")); |
| 59 | QRegularExpressionMatch match; |
| 60 | |
| 61 | state = EMPTY; |
| 62 | html.clear(); |
| 63 | html += QStringLiteral("<html>"); |
| 64 | |
| 65 | const auto lines = markdown.split(QLatin1Char('\n')); |
| 66 | for (auto line : lines) { |
| 67 | if (line.isEmpty()) { |
| 68 | setState(EMPTY); |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | if (line.startsWith(QLatin1Char('#'))) { |
| 73 | auto match = hRE.match(line); |
| 74 | if (match.hasMatch()) { |
| 75 | setState(HEADING); |
| 76 | html += match.captured(2); |
| 77 | setState(EMPTY); |
| 78 | if (match.capturedLength(1) == 1) { |
| 79 | html += QStringLiteral("<hr>"); |
| 80 | } |
| 81 | } |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | if (line.startsWith(QLatin1String("```"))) { |
| 86 | setState((state == PREFORMATTED) ? EMPTY : PREFORMATTED); |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if (line.startsWith(QLatin1String(" "))) { |
| 91 | if (state == EMPTY) { |
| 92 | setState(PREFORMATTED); |
| 93 | } |
| 94 | } else if ( |
| 95 | line.startsWith(QLatin1String("- ")) || |
| 96 | line.startsWith(QLatin1String("* "))) { |
| 97 | // force close and reopen list - this fixes cases when we don't have |
| 98 | // separator line between items |
| 99 | setState(EMPTY); |
| 100 | setState(LIST); |
| 101 | line.remove(0, 2); |
| 102 | } |
| 103 | |
| 104 | if (state == EMPTY) { |
| 105 | setState(PARAGRAPH); |
| 106 | } |
| 107 | |
| 108 | processLine(line); |
| 109 | } |
| 110 | setState(EMPTY); |
| 111 | |
| 112 | html += QStringLiteral("</html>"); |
| 113 | return html.join(QLatin1Char('\n')); |