| 263 | }; |
| 264 | |
| 265 | QString convertToHTML(const QString& inputParam) |
| 266 | { |
| 267 | // this code goes over the input string once and replaces all bbtags |
| 268 | // it encounters. This function is called recursively for every replaced |
| 269 | // string to convert nested tags. |
| 270 | // |
| 271 | // This could be implemented simpler by applying a set of regular expressions |
| 272 | // for each recognized bb-tag one after the other but that would probably be |
| 273 | // very inefficient (O(n^2)). |
| 274 | |
| 275 | QString input = inputParam.mid(0).replace("\r\n", "<br/>"); |
| 276 | input.replace("\\\"", "\"").replace("\\'", "'"); |
| 277 | QString result; |
| 278 | int lastBlock = 0; |
| 279 | int pos = 0; |
| 280 | |
| 281 | // iterate over the input buffer |
| 282 | while ((pos = input.indexOf('[', lastBlock)) != -1) { |
| 283 | // append everything between the previous tag-block and the current one |
| 284 | result.append(input.mid(lastBlock, pos - lastBlock)); |
| 285 | |
| 286 | if ((pos < (input.size() - 1)) && (input.at(pos + 1) == '/')) { |
| 287 | // skip invalid end tag |
| 288 | int tagEnd = input.indexOf(']', pos) + 1; |
| 289 | if (tagEnd == 0) { |
| 290 | // no closing tag found |
| 291 | // move the pos up one so that the opening bracket is ignored next iteration |
| 292 | pos++; |
| 293 | } else { |
| 294 | pos = tagEnd; |
| 295 | } |
| 296 | } else { |
| 297 | // convert the tag and content if necessary |
| 298 | int length = -1; |
| 299 | QString replacement = BBCodeMap::instance().convertTag(input.mid(pos), length); |
| 300 | if (length != 0) { |
| 301 | result.append(convertToHTML(replacement)); |
| 302 | // length contains the number of characters in the original tag |
| 303 | pos += length; |
| 304 | } else { |
| 305 | // nothing replaced |
| 306 | result.append('['); |
| 307 | ++pos; |
| 308 | } |
| 309 | } |
| 310 | lastBlock = pos; |
| 311 | } |
| 312 | |
| 313 | // append the remainder (everything after the last tag) |
| 314 | result.append(input.mid(lastBlock)); |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | } // namespace BBCode |
no test coverage detected