| 28 | namespace log = MOBase::log; |
| 29 | |
| 30 | class BBCodeMap |
| 31 | { |
| 32 | |
| 33 | typedef std::map<QString, std::pair<QRegularExpression, QString>> TagMap; |
| 34 | |
| 35 | public: |
| 36 | static BBCodeMap& instance() |
| 37 | { |
| 38 | static BBCodeMap s_Instance; |
| 39 | return s_Instance; |
| 40 | } |
| 41 | |
| 42 | QString convertTag(QString input, int& length) |
| 43 | { |
| 44 | // extract the tag name |
| 45 | auto match = m_TagNameExp.match(input, 1, QRegularExpression::NormalMatch, |
| 46 | QRegularExpression::AnchoredMatchOption); |
| 47 | QString tagName = match.captured(0).toLower(); |
| 48 | TagMap::iterator tagIter = m_TagMap.find(tagName); |
| 49 | if (tagIter != m_TagMap.end()) { |
| 50 | // recognized tag |
| 51 | if (tagName.endsWith('=')) { |
| 52 | tagName.chop(1); |
| 53 | } |
| 54 | |
| 55 | int closeTagPos = 0; |
| 56 | int nextTagPos = 0; |
| 57 | int nextTagSearchIndex = input.indexOf("]"); |
| 58 | int closeTagLength = 0; |
| 59 | if (tagName == "*") { |
| 60 | // ends at the next bullet point |
| 61 | closeTagPos = |
| 62 | input.indexOf(QRegularExpression("(\\[\\*\\]|</ul>)", |
| 63 | QRegularExpression::CaseInsensitiveOption), |
| 64 | 3); |
| 65 | // leave closeTagLength at 0 because we don't want to "eat" the next bullet |
| 66 | // point |
| 67 | } else if (tagName == "line") { |
| 68 | // ends immediately after the tag |
| 69 | closeTagPos = 6; |
| 70 | // leave closeTagLength at 0 because there is no close tag to skip over |
| 71 | } else { |
| 72 | QRegularExpression nextTag(QString("\\[%1[=\\]]?").arg(tagName), |
| 73 | QRegularExpression::CaseInsensitiveOption); |
| 74 | QString closeTag = QString("[/%1]").arg(tagName); |
| 75 | closeTagPos = input.indexOf(closeTag, 0, Qt::CaseInsensitive); |
| 76 | nextTagPos = nextTag.match(input, nextTagSearchIndex).capturedStart(0); |
| 77 | while (nextTagPos != -1 && closeTagPos != -1 && nextTagPos < closeTagPos) { |
| 78 | closeTagPos = input.indexOf(closeTag, closeTagPos + closeTag.size(), |
| 79 | Qt::CaseInsensitive); |
| 80 | nextTagSearchIndex = input.indexOf("]", nextTagPos); |
| 81 | nextTagPos = nextTag.match(input, nextTagSearchIndex).capturedStart(0); |
| 82 | } |
| 83 | if (closeTagPos == -1) { |
| 84 | // workaround to improve compatibility: add fake closing tag |
| 85 | input.append(closeTag); |
| 86 | closeTagPos = input.size() - closeTag.size(); |
| 87 | } |
nothing calls this directly
no outgoing calls
no test coverage detected