markdownhighlighterdata.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef MARKDOWNHIGHLIGHTERDATA_H
  2. #define MARKDOWNHIGHLIGHTERDATA_H
  3. #include <QTextCharFormat>
  4. #include "vconstants.h"
  5. extern "C" {
  6. #include <pmh_parser.h>
  7. }
  8. struct HighlightingStyle
  9. {
  10. pmh_element_type type;
  11. QTextCharFormat format;
  12. };
  13. // One continuous region for a certain markdown highlight style
  14. // within a QTextBlock.
  15. // Pay attention to the change of HighlightingStyles[]
  16. struct HLUnit
  17. {
  18. // Highlight offset @start and @length with style HighlightingStyles[styleIndex]
  19. // within a QTextBlock
  20. unsigned long start;
  21. unsigned long length;
  22. unsigned int styleIndex;
  23. bool operator==(const HLUnit &p_a) const
  24. {
  25. return start == p_a.start
  26. && length == p_a.length
  27. && styleIndex == p_a.styleIndex;
  28. }
  29. QString toString() const
  30. {
  31. return QString("HLUnit %1 %2 %3").arg(start).arg(length).arg(styleIndex);
  32. }
  33. };
  34. struct HLUnitStyle
  35. {
  36. unsigned long start;
  37. unsigned long length;
  38. QString style;
  39. bool operator==(const HLUnitStyle &p_a) const
  40. {
  41. if (start != p_a.start || length != p_a.length) {
  42. return false;
  43. }
  44. return style == p_a.style;
  45. }
  46. };
  47. // Fenced code block only.
  48. struct VCodeBlock
  49. {
  50. // Global position of the start.
  51. int m_startPos;
  52. int m_startBlock;
  53. int m_endBlock;
  54. QString m_lang;
  55. QString m_text;
  56. bool equalContent(const VCodeBlock &p_block) const
  57. {
  58. return p_block.m_lang == m_lang && p_block.m_text == m_text;
  59. }
  60. void updateNonContent(const VCodeBlock &p_block)
  61. {
  62. m_startPos = p_block.m_startPos;
  63. m_startBlock = p_block.m_startBlock;
  64. m_endBlock = p_block.m_endBlock;
  65. }
  66. };
  67. struct VMathjaxBlock
  68. {
  69. VMathjaxBlock()
  70. : m_blockNumber(-1),
  71. m_previewedAsBlock(false),
  72. m_index(-1),
  73. m_length(-1)
  74. {
  75. }
  76. bool equalContent(const VMathjaxBlock &p_block) const
  77. {
  78. return m_text == p_block.m_text;
  79. }
  80. void updateNonContent(const VMathjaxBlock &p_block)
  81. {
  82. m_blockNumber = p_block.m_blockNumber;
  83. m_previewedAsBlock = p_block.m_previewedAsBlock;
  84. m_index = p_block.m_index;
  85. m_length = p_block.m_length;
  86. }
  87. // Block number for in-place preview.
  88. int m_blockNumber;
  89. // Whether it should be previewed as block or not.
  90. bool m_previewedAsBlock;
  91. // Start index wihtin block with number m_blockNumber, including the start mark.
  92. int m_index;
  93. // Length of this mathjax in block with number m_blockNumber, including the end mark.
  94. int m_length;
  95. QString m_text;
  96. };
  97. struct VTableBlock
  98. {
  99. VTableBlock()
  100. : m_startPos(-1),
  101. m_endPos(-1)
  102. {
  103. }
  104. bool isValid() const
  105. {
  106. return m_startPos > -1 && m_endPos >= m_startPos;
  107. }
  108. void clear()
  109. {
  110. m_startPos = m_endPos = -1;
  111. m_borders.clear();
  112. }
  113. QString toString() const
  114. {
  115. return QString("table [%1,%2) borders %3").arg(m_startPos)
  116. .arg(m_endPos)
  117. .arg(m_borders.size());
  118. }
  119. int m_startPos;
  120. int m_endPos;
  121. // Global position of the table borders in ascending order.
  122. QVector<int> m_borders;
  123. };
  124. // Highlight unit with global position and string style name.
  125. struct HLUnitPos
  126. {
  127. HLUnitPos() : m_position(-1), m_length(-1)
  128. {
  129. }
  130. HLUnitPos(int p_position, int p_length, const QString &p_style)
  131. : m_position(p_position), m_length(p_length), m_style(p_style)
  132. {
  133. }
  134. int m_position;
  135. int m_length;
  136. QString m_style;
  137. };
  138. // Denote the region of a certain Markdown element.
  139. struct VElementRegion
  140. {
  141. VElementRegion() : m_startPos(0), m_endPos(0) {}
  142. VElementRegion(int p_start, int p_end) : m_startPos(p_start), m_endPos(p_end) {}
  143. // The start position of the region in document.
  144. int m_startPos;
  145. // The end position of the region in document.
  146. int m_endPos;
  147. // Whether this region contains @p_pos.
  148. bool contains(int p_pos) const
  149. {
  150. return m_startPos <= p_pos && m_endPos > p_pos;
  151. }
  152. bool contains(const VElementRegion &p_reg) const
  153. {
  154. return m_startPos <= p_reg.m_startPos && m_endPos >= p_reg.m_endPos;
  155. }
  156. bool intersect(int p_start, int p_end) const
  157. {
  158. return !(p_end <= m_startPos || p_start >= m_endPos);
  159. }
  160. bool operator==(const VElementRegion &p_other) const
  161. {
  162. return (m_startPos == p_other.m_startPos
  163. && m_endPos == p_other.m_endPos);
  164. }
  165. bool operator<(const VElementRegion &p_other) const
  166. {
  167. if (m_startPos < p_other.m_startPos) {
  168. return true;
  169. } else if (m_startPos == p_other.m_startPos) {
  170. // If a < b is true, then b < a must be false.
  171. return m_endPos < p_other.m_endPos;
  172. } else {
  173. return false;
  174. }
  175. }
  176. QString toString() const
  177. {
  178. return QString("[%1,%2)").arg(m_startPos).arg(m_endPos);
  179. }
  180. };
  181. #endif // MARKDOWNHIGHLIGHTERDATA_H