| 71 | } |
| 72 | |
| 73 | QString MobiDocument::fixMobiMarkup(const QString &data) |
| 74 | { |
| 75 | QString ret = data; |
| 76 | QMap<int, QString> anchorPositions; |
| 77 | static QRegularExpression anchors(QStringLiteral("<a(?: href=\"[^\"]*\"){0,1}[\\s]+filepos=['\"]{0,1}([\\d]+)[\"']{0,1}"), QRegularExpression::CaseInsensitiveOption); |
| 78 | |
| 79 | // find all link destinations |
| 80 | auto matcher = anchors.globalMatch(data); |
| 81 | while (matcher.hasNext()) { |
| 82 | auto match = matcher.next(); |
| 83 | int filepos = match.captured(1).toUInt(); |
| 84 | if (filepos) { |
| 85 | anchorPositions[filepos] = match.captured(1); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // put HTML anchors in all link destinations |
| 90 | int offset = 0; |
| 91 | QMapIterator<int, QString> it(anchorPositions); |
| 92 | while (it.hasNext()) { |
| 93 | it.next(); |
| 94 | // link pointing outside the document, ignore |
| 95 | if ((it.key() + offset) >= ret.size()) { |
| 96 | continue; |
| 97 | } |
| 98 | int fixedpos = outsideTag(ret, it.key() + offset); |
| 99 | ret.insert(fixedpos, QStringLiteral("<a name=\"") + it.value() + QStringLiteral("\"> </a>")); |
| 100 | // inserting anchor shifts all offsets after the anchor |
| 101 | offset += 21 + it.value().size(); |
| 102 | } |
| 103 | |
| 104 | // replace links referencing filepos with normal internal links |
| 105 | ret.replace(anchors, QStringLiteral("<a href=\"#\\1\"")); |
| 106 | // Mobipocket uses strange variang of IMG tags: <img recindex="3232"> where recindex is number of |
| 107 | // record containing image |
| 108 | static QRegularExpression imgs(QStringLiteral("<img.*?recindex=\"([\\d]*)\".*?>"), QRegularExpression::CaseInsensitiveOption); |
| 109 | |
| 110 | ret.replace(imgs, QStringLiteral("<img src=\"pdbrec:/\\1\">")); |
| 111 | ret.replace(QStringLiteral("<mbp:pagebreak/>"), QStringLiteral("<p style=\"page-break-after:always\"></p>")); |
| 112 | |
| 113 | return ret; |
| 114 | } |
nothing calls this directly
no test coverage detected