hgmarkdownhighlighter.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* PEG Markdown Highlight
  2. * Copyright 2011-2016 Ali Rantakari -- http://hasseg.org
  3. * Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
  4. *
  5. * highlighter.cpp
  6. *
  7. * Qt 4.7 example for highlighting a rich text widget.
  8. */
  9. #include <QtGui>
  10. #include <QtDebug>
  11. #include "hgmarkdownhighlighter.h"
  12. const int HGMarkdownHighlighter::initCapacity = 1024;
  13. void HGMarkdownHighlighter::resizeBuffer(int newCap)
  14. {
  15. if (newCap == capacity) {
  16. return;
  17. }
  18. if (capacity > 0) {
  19. Q_ASSERT(content);
  20. delete [] content;
  21. }
  22. capacity = newCap;
  23. content = new char [capacity];
  24. }
  25. // Will be freeed by parent automatically
  26. HGMarkdownHighlighter::HGMarkdownHighlighter(const QVector<HighlightingStyle> &styles, int waitInterval,
  27. QTextDocument *parent)
  28. : QSyntaxHighlighter(parent), parsing(0),
  29. waitInterval(waitInterval), content(NULL), capacity(0), result(NULL)
  30. {
  31. codeBlockStartExp = QRegExp("^(\\s)*```");
  32. codeBlockEndExp = QRegExp("^(\\s)*```$");
  33. codeBlockFormat.setForeground(QBrush(Qt::darkYellow));
  34. for (int index = 0; index < styles.size(); ++index) {
  35. if (styles[index].type == pmh_VERBATIM) {
  36. codeBlockFormat = styles[index].format;
  37. break;
  38. }
  39. }
  40. resizeBuffer(initCapacity);
  41. setStyles(styles);
  42. document = parent;
  43. timer = new QTimer(this);
  44. timer->setSingleShot(true);
  45. timer->setInterval(this->waitInterval);
  46. connect(timer, &QTimer::timeout, this, &HGMarkdownHighlighter::timerTimeout);
  47. connect(document, &QTextDocument::contentsChange,
  48. this, &HGMarkdownHighlighter::handleContentChange);
  49. }
  50. HGMarkdownHighlighter::~HGMarkdownHighlighter()
  51. {
  52. if (result) {
  53. pmh_free_elements(result);
  54. result = NULL;
  55. }
  56. if (content) {
  57. delete [] content;
  58. capacity = 0;
  59. content = NULL;
  60. }
  61. }
  62. void HGMarkdownHighlighter::highlightBlock(const QString &text)
  63. {
  64. int blockNum = currentBlock().blockNumber();
  65. if (!parsing && blockHighlights.size() > blockNum) {
  66. QVector<HLUnit> &units = blockHighlights[blockNum];
  67. for (int i = 0; i < units.size(); ++i) {
  68. // TODO: merge two format within the same range
  69. const HLUnit& unit = units[i];
  70. setFormat(unit.start, unit.length, highlightingStyles[unit.styleIndex].format);
  71. }
  72. }
  73. setCurrentBlockState(0);
  74. highlightCodeBlock(text);
  75. }
  76. void HGMarkdownHighlighter::setStyles(const QVector<HighlightingStyle> &styles)
  77. {
  78. this->highlightingStyles = styles;
  79. }
  80. void HGMarkdownHighlighter::initBlockHighlightFromResult(int nrBlocks)
  81. {
  82. blockHighlights.resize(nrBlocks);
  83. for (int i = 0; i < blockHighlights.size(); ++i) {
  84. blockHighlights[i].clear();
  85. }
  86. if (!result) {
  87. return;
  88. }
  89. for (int i = 0; i < highlightingStyles.size(); i++)
  90. {
  91. const HighlightingStyle &style = highlightingStyles[i];
  92. pmh_element *elem_cursor = result[style.type];
  93. while (elem_cursor != NULL)
  94. {
  95. if (elem_cursor->end <= elem_cursor->pos) {
  96. elem_cursor = elem_cursor->next;
  97. continue;
  98. }
  99. initBlockHighlihgtOne(elem_cursor->pos, elem_cursor->end, i);
  100. elem_cursor = elem_cursor->next;
  101. }
  102. }
  103. pmh_free_elements(result);
  104. result = NULL;
  105. }
  106. void HGMarkdownHighlighter::initBlockHighlihgtOne(unsigned long pos, unsigned long end, int styleIndex)
  107. {
  108. int startBlockNum = document->findBlock(pos).blockNumber();
  109. int endBlockNum = document->findBlock(end).blockNumber();
  110. for (int i = startBlockNum; i <= endBlockNum; ++i)
  111. {
  112. QTextBlock block = document->findBlockByNumber(i);
  113. int blockStartPos = block.position();
  114. HLUnit unit;
  115. if (i == startBlockNum) {
  116. unit.start = pos - blockStartPos;
  117. unit.length = (startBlockNum == endBlockNum) ?
  118. (end - pos) : (block.length() - unit.start);
  119. } else if (i == endBlockNum) {
  120. unit.start = 0;
  121. unit.length = end - blockStartPos;
  122. } else {
  123. unit.start = 0;
  124. unit.length = block.length();
  125. }
  126. unit.styleIndex = styleIndex;
  127. blockHighlights[i].append(unit);
  128. }
  129. }
  130. void HGMarkdownHighlighter::highlightCodeBlock(const QString &text)
  131. {
  132. int nextIndex = 0;
  133. int startIndex = 0;
  134. if (previousBlockState() != 1) {
  135. startIndex = codeBlockStartExp.indexIn(text);
  136. if (startIndex >= 0) {
  137. nextIndex = startIndex + codeBlockStartExp.matchedLength();
  138. } else {
  139. nextIndex = -1;
  140. }
  141. }
  142. while (nextIndex >= 0) {
  143. int endIndex = codeBlockEndExp.indexIn(text, nextIndex);
  144. int codeBlockLength;
  145. if (endIndex == -1) {
  146. setCurrentBlockState(1);
  147. codeBlockLength = text.length() - startIndex;
  148. } else {
  149. codeBlockLength = endIndex - startIndex + codeBlockEndExp.matchedLength();
  150. }
  151. setFormat(startIndex, codeBlockLength, codeBlockFormat);
  152. startIndex = codeBlockStartExp.indexIn(text, startIndex + codeBlockLength);
  153. if (startIndex >= 0) {
  154. nextIndex = startIndex + codeBlockStartExp.matchedLength();
  155. } else {
  156. nextIndex = -1;
  157. }
  158. }
  159. }
  160. void HGMarkdownHighlighter::parse()
  161. {
  162. if (!parsing.testAndSetRelaxed(0, 1)) {
  163. return;
  164. }
  165. int nrBlocks = document->blockCount();
  166. parseInternal();
  167. if (highlightingStyles.isEmpty()) {
  168. qWarning() << "error: HighlightingStyles is not set";
  169. return;
  170. }
  171. initBlockHighlightFromResult(nrBlocks);
  172. parsing.store(0);
  173. }
  174. void HGMarkdownHighlighter::parseInternal()
  175. {
  176. QString text = document->toPlainText();
  177. QByteArray ba = text.toUtf8();
  178. const char *data = (const char *)ba.data();
  179. int len = ba.size();
  180. if (result) {
  181. pmh_free_elements(result);
  182. result = NULL;
  183. }
  184. if (len == 0) {
  185. return;
  186. } else if (len >= capacity) {
  187. resizeBuffer(qMax(2 * capacity, len * 2));
  188. } else if (len < (capacity >> 2)) {
  189. resizeBuffer(qMax(capacity >> 1, len * 2));
  190. }
  191. memcpy(content, data, len);
  192. content[len] = '\0';
  193. pmh_markdown_to_elements(content, pmh_EXT_NONE, &result);
  194. }
  195. void HGMarkdownHighlighter::handleContentChange(int position, int charsRemoved, int charsAdded)
  196. {
  197. if (charsRemoved == 0 && charsAdded == 0) {
  198. return;
  199. }
  200. timer->stop();
  201. timer->start();
  202. }
  203. void HGMarkdownHighlighter::timerTimeout()
  204. {
  205. parse();
  206. rehighlight();
  207. }