| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "network-web/gemini/geminiparser.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | #include "miscellaneous/iofactory.h" |
| 7 | |
| 8 | QString GeminiParser::geminiToHtml(const QByteArray& gemini_data) { |
| 9 | QString html; |
| 10 | QString gemini_hypertext = |
| 11 | QString::fromUtf8(gemini_data).replace(QSL("\r\n"), QSL("\n")).replace(QSL("\r"), QSL("\n")); |
| 12 | QStringList lines = gemini_hypertext.split(QL1C('\n')); |
| 13 | mode = State::Normal; |
| 14 | |
| 15 | static QRegularExpression exp_link(R"(^=>\s+([^\s]+)(?:\s+(\S.+))?$)"); |
| 16 | static QRegularExpression exp_heading(R"(^(#{1,6})\s+(.+)$)"); |
| 17 | static QRegularExpression exp_list(R"(^\*\s(.+)$)"); |
| 18 | static QRegularExpression exp_quote(R"((?:^>$|^>\s?(.+)$))"); |
| 19 | static QRegularExpression exp_pre(R"(^```.*$)"); |
| 20 | |
| 21 | static QString rich_style = QString::fromUtf8(IOFactory::readFile(QSL(":/scripts/gemini/style.css"))); |
| 22 | |
| 23 | QRegularExpressionMatch mtch; |
| 24 | QString title; |
| 25 | |
| 26 | for (const QString& line : lines) { |
| 27 | if ((mtch = exp_pre.match(line)).hasMatch()) { |
| 28 | // Begin or end PRE block. |
| 29 | switch (mode) { |
| 30 | case State::Pre: |
| 31 | // Ending of a PRE block. |
| 32 | html += endBlock(State::Normal); |
| 33 | break; |
| 34 | |
| 35 | default: |
| 36 | // Beginning of a PRE block. |
| 37 | html += endBlock(State::Normal); |
| 38 | html += beginBlock(State::Pre); |
| 39 | break; |
| 40 | } |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | if (mode != State::Pre) { |
| 45 | if ((mtch = exp_link.match(line)).hasMatch()) { |
| 46 | html += endBlock(State::Normal); |
| 47 | html += parseLink(mtch); |
| 48 | } |
| 49 | else if ((mtch = exp_heading.match(line)).hasMatch()) { |
| 50 | html += endBlock(State::Normal); |
| 51 | html += parseHeading(mtch, title.isEmpty() ? &title : nullptr); |
| 52 | } |
| 53 | else if ((mtch = exp_list.match(line)).hasMatch()) { |
| 54 | html += beginBlock(State::List); |
| 55 | html += parseList(mtch); |
| 56 | } |
| 57 | else if ((mtch = exp_quote.match(line)).hasMatch()) { |
| 58 | html += beginBlock(State::Quote); |
| 59 | html += parseQuote(mtch); |
| 60 | } |
| 61 | else { |
| 62 | html += endBlock(State::Normal); |
| 63 | html += parseTextInNormalMode(line); |
| 64 | } |
| 65 | } |
| 66 | else { |
| 67 | // Add new line in PRE mode. |
| 68 | html += parseInPreMode(line); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | html += endBlock(State::Normal); |
| 73 | |
| 74 | return QSL("<!DOCTYPE html>" |
| 75 | "<html>" |
| 76 | "<head>" |
| 77 | "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" |
| 78 | "<meta charset=utf-8>" |
| 79 | "<title>%1</title>" |
| 80 | "<style>%3</style>" |
| 81 | "</head>" |
| 82 | "<body>%2</body>" |
| 83 | "</html>") |
| 84 | .arg(title, html, m_richHtml ? rich_style : QString()); |
| 85 | } |
| 86 | |
| 87 | QString GeminiParser::beginBlock(State new_mode) { |
| 88 | if (new_mode != mode) { |
| 89 | mode = new_mode; |
| 90 | |
| 91 | switch (new_mode) { |
| 92 | case State::List: |
| 93 | return "<ul>\n"; |
| 94 | |
| 95 | case State::Quote: |
| 96 | return QSL("<%1 style=\"" |
| 97 | "background-color: #E1E5EE;" |
| 98 | "font-style: italic;" |
| 99 | "margin-left: 20px;" |
| 100 | "margin-right: 20px;\">\n") |
| 101 | .arg(m_richHtml ? QSL("blockquote") : QSL("div")); |
| 102 | |
| 103 | case State::Pre: |
| 104 | return "<pre style=\"background-color: #E1E5EE;\">\n"; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return QString(); |
| 109 | } |
| 110 | |
| 111 | QString GeminiParser::endBlock(State new_mode) { |
| 112 | QString to_return; |
| 113 | |
| 114 | if (new_mode != mode) { |
| 115 | switch (mode) { |
| 116 | case State::List: |
| 117 | to_return = "</ul>\n"; |
| 118 | break; |
| 119 | |
| 120 | case State::Quote: |
| 121 | to_return = QSL("</%1>\n").arg(m_richHtml ? QSL("blockquote") : QSL("div")); |
| 122 | break; |
| 123 | |
| 124 | case State::Pre: |
| 125 | to_return = "</pre>\n"; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | mode = new_mode; |
| 130 | } |
| 131 | |
| 132 | return to_return; |
| 133 | } |
| 134 | |
| 135 | GeminiParser::GeminiParser(bool rich_html) : m_richHtml(rich_html) {} |
| 136 | |
| 137 | QString GeminiParser::parseLink(const QRegularExpressionMatch& mtch) const { |
| 138 | QString link = mtch.captured(1); |
| 139 | QString name = mtch.captured(2); |
| 140 | |
| 141 | return QSL("<p>🔗 <a href=\"%1\">%2</a></p>\n").arg(link, name.isEmpty() ? link : name); |
| 142 | } |
| 143 | |
| 144 | QString GeminiParser::parseHeading(const QRegularExpressionMatch& mtch, QString* clean_header) const { |
| 145 | int level = mtch.captured(1).size(); |
| 146 | QString header = mtch.captured(2); |
| 147 | |
| 148 | if (!header.isEmpty() && clean_header != nullptr) { |
| 149 | clean_header->clear(); |
| 150 | clean_header->append(header); |
| 151 | } |
| 152 | |
| 153 | return QSL("<h%1>%2</h%1>\n").arg(QString::number(level), header); |
| 154 | } |
| 155 | |
| 156 | QString GeminiParser::parseQuote(const QRegularExpressionMatch& mtch) const { |
| 157 | QString text = mtch.captured(1); |
| 158 | QString element = m_richHtml ? QSL("p") : QSL("div"); |
| 159 | |
| 160 | return QSL("<%2>%1</%2>\n") |
| 161 | .arg(text.simplified().isEmpty() ? QString() : (m_richHtml ? text : QSL("“%1”").arg(text)), element); |
| 162 | } |
| 163 | |
| 164 | QString GeminiParser::parseList(const QRegularExpressionMatch& mtch) const { |
| 165 | QString text = mtch.captured(1); |
| 166 | |
| 167 | return QSL("<li>%1</li>\n").arg(text); |
| 168 | } |
| 169 | |
| 170 | QString GeminiParser::parseTextInNormalMode(const QString& line) const { |
| 171 | return QSL("<p>%1</p>\n").arg(line); |
| 172 | } |
| 173 | |
| 174 | QString GeminiParser::parseInPreMode(const QString& line) const { |
| 175 | return QSL("%1\n").arg(line.toHtmlEscaped()); |
| 176 | } |