16 #include <QRegularExpression>
17 #include <QRegularExpressionMatch>
25 while (from < text.length()) {
28 int end = text.indexOf(
'<', from);
31 parts.append(text.mid(from, end-from));
35 end = text.indexOf(
'>', from);
38 parts.append(text.mid(from, end-from+1));
45 static QRegularExpression
const rx(R
"regex(<br\s*/>)regex");
46 for (
int index = 2; index < parts.count(); ++index) {
47 if (parts.at(index).contains(rx) && parts.at(index-2).contains(rx))
56 if (i + 2 >= parts.count())
59 static QRegularExpression
const rx(R
"regex(note="([^"]*))regex");
60 if (
auto const match = rx.match(part); match.hasMatch()) {
61 auto const & footnoteText = parts.at(i + 1);
64 R
"HTML(<a class="footnote" href="sword://footnote/%1=%2">)HTML")
65 .arg(match.captured(1)).arg(footnoteText);
66 parts[i+1] = QStringLiteral("(%1)").arg(footnoteText);
67 parts[i+2] = QStringLiteral(
"</a>");
77 void rewriteHref(QStringList & parts,
int i, QString
const & part) {
78 static QRegularExpression
const rx(
79 R
"regex(<a\s+(\w+)="([^"]*)"\s+(\w+)="([^"]*)")regex");
80 if (
auto const match = rx.match(part); match.hasMatch())
82 ((match.captured(1) == QStringLiteral(
"href"))
83 ? QStringLiteral(R
"HTML(<a %1="%2||%3=%4" name="crossref">)HTML")
84 : QStringLiteral(R"HTML(<a %3="%4||%1=%2" name="crossref">)HTML"))
85 .arg(match.captured(1),
95 if (i + 2 >= parts.count())
100 static QRegularExpression
const rx(R
"regex(lemma="([^"]*)")regex");
101 if (
auto const match = rx.match(part); match.hasMatch())
102 value = QStringLiteral(
"lemma=") + match.captured(1);
104 static QRegularExpression
const rx(R
"regex(morph="([^"]*)")regex");
105 if (
auto const match = rx.match(part); match.hasMatch()) {
106 if (value.isEmpty()) {
107 value = QStringLiteral(
"morph=") + match.captured(1);
109 value = QStringLiteral(
"%1||morph=%2")
110 .arg(value, match.captured(1));
115 auto const & refText = parts.at(i + 1);
118 R
"HTM(<a id="lemmamorph" href="sword://lemmamorph/%1/%2">)HTM")
119 .arg(value, refText);
120 parts[i + 2] = QStringLiteral("</a>");
124 int rewriteTag(QStringList & parts,
int i, QString
const & tag) {
125 if (i + 2 >= parts.count())
127 parts[i] =
"<" + tag +
">";
128 parts[i+2] =
"</" + tag +
">";
133 if (i + 2 >= parts.count())
135 parts[i] =
"<div><big><" + tag +
">";
136 parts[i+2] =
"</" + tag +
"></big></div>";
142 if (part.contains(QStringLiteral(R
"HTML(class="footnote")HTML"))) {
144 }
else if (part.contains(QStringLiteral(R
"HTML(class="bold")HTML"))) {
146 }
else if (part.contains(QStringLiteral(R
"HTML(class="italic")HTML"))) {
148 }
else if (part.contains(QStringLiteral(R
"HTML(class="chaptertitle")HTML"))) {
150 }
else if (part.contains(QStringLiteral(R
"HTML(class="sectiontitle")HTML"))) {
152 }
else if (part.contains(QStringLiteral(R
"HTML(class="booktitle")HTML"))) {
167 QString localText = text;
170 while ((index = localText.indexOf(QStringLiteral(
"<!P>"))) >= 0)
171 localText.remove(index,4);
176 for (
int i = 0; i < parts.count();) {
177 if (
auto const & part = parts.at(i); part.startsWith(
'<')) {
178 if (part.contains(QStringLiteral(R
"HTML(class=)HTML"))) {
180 } else if (part.contains(QStringLiteral(R
"HTML(class="footnote")HTML"))) {
182 } else if (part.contains(QStringLiteral(R
"HTML(href=")HTML"))) {
185 } else if (part.contains(QStringLiteral(R
"HTML(lemma=")HTML"))
186 || part.contains(QStringLiteral(R"HTML(morph=")HTML")))
196 return parts.join(QString());
QString processText(const QString &text) override
QStringList splitText(QString const &text)
int rewriteClass(QStringList &parts, int i, QString const &part)
int rewriteLemmaOrMorphAsLink(QStringList &parts, int i, QString const &part)
int rewriteTitle(QStringList &parts, int i, QString const &tag)
void fixDoubleBR(QStringList &parts)
void rewriteHref(QStringList &parts, int i, QString const &part)
int rewriteFootnoteAsLink(QStringList &parts, int i, QString const &part)
int rewriteTag(QStringList &parts, int i, QString const &tag)