diff --git a/src/librssguard/network-web/gemini/geminiparser.cpp b/src/librssguard/network-web/gemini/geminiparser.cpp index af2c2940b..df8649e04 100644 --- a/src/librssguard/network-web/gemini/geminiparser.cpp +++ b/src/librssguard/network-web/gemini/geminiparser.cpp @@ -10,50 +10,119 @@ QString GeminiParser::geminiToHtml(const QByteArray& gemini_data) { QString gemini_hypertext = QString::fromUtf8(gemini_data).replace(QSL("\r\n"), QSL("\n")).replace(QSL("\r"), QSL("\n")); QStringList lines = gemini_hypertext.split(QL1C('\n')); - bool normal_mode = true; + mode = State::Normal; static QRegularExpression exp_link(R"(^=>\s+([^\s]+)(?:\s+(\w.+))?$)"); static QRegularExpression exp_heading(R"(^(#{1,6})\s+(.+)$)"); static QRegularExpression exp_list(R"(^\*\s(.+)$)"); static QRegularExpression exp_quote(R"((?:^>$|^>\s?(.+)$))"); static QRegularExpression exp_pre(R"(^```.*$)"); - static QRegularExpression exp_text(R"()"); QRegularExpressionMatch mtch; QString title; for (const QString& line : lines) { if ((mtch = exp_pre.match(line)).hasMatch()) { - normal_mode = !normal_mode; + // Begin or end PRE block. + switch (mode) { + case State::Pre: + // Ending of a PRE block. + html += endBlock(State::Normal); + break; + + default: + // Beginning of a PRE block. + html += endBlock(State::Normal); + html += beginBlock(State::Pre); + break; + } continue; } - if (normal_mode) { + if (mode != State::Pre) { if ((mtch = exp_link.match(line)).hasMatch()) { + html += endBlock(State::Normal); html += parseLink(mtch); } else if ((mtch = exp_heading.match(line)).hasMatch()) { + html += endBlock(State::Normal); html += parseHeading(mtch, title.isEmpty() ? &title : nullptr); } else if ((mtch = exp_list.match(line)).hasMatch()) { + html += beginBlock(State::List); html += parseList(mtch); } else if ((mtch = exp_quote.match(line)).hasMatch()) { + html += beginBlock(State::Quote); html += parseQuote(mtch); } else { + html += endBlock(State::Normal); html += parseTextInNormalMode(line); } } else { + // Add new line in PRE mode. html += parseInPreMode(line); } } + html += endBlock(State::Normal); + + // IOFactory::writeFile("aa", html.toUtf8()); + return QSL("" "