redsnow 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -0
  3. data/ext/snowcrash/Makefile +1 -1
  4. data/ext/snowcrash/bin/snowcrash +0 -0
  5. data/ext/snowcrash/configure +9 -9
  6. data/ext/snowcrash/ext/markdown-parser/Makefile +87 -0
  7. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/CONTRIBUTING.md +0 -0
  8. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/Makefile +2 -1
  9. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/Makefile.win +0 -0
  10. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/examples/smartypants.c +0 -0
  11. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/examples/sundown.c +0 -0
  12. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html/houdini.h +0 -0
  13. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html/houdini_href_e.c +0 -0
  14. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html/houdini_html_e.c +0 -0
  15. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html/html.c +0 -0
  16. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html/html.h +0 -0
  17. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html/html_smartypants.c +0 -0
  18. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/html_block_names.txt +0 -0
  19. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/autolink.c +0 -0
  20. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/autolink.h +0 -0
  21. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/buffer.c +0 -0
  22. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/buffer.h +1 -1
  23. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/html_blocks.h +0 -0
  24. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/markdown.c +9 -3
  25. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/markdown.h +0 -0
  26. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/src_map.c +11 -7
  27. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/src_map.h +1 -1
  28. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/stack.c +0 -0
  29. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/src/stack.h +0 -0
  30. data/ext/snowcrash/{sundown → ext/markdown-parser/ext/sundown}/sundown.def +0 -0
  31. data/ext/snowcrash/ext/markdown-parser/msvc/markdown/markdown.vcproj +188 -0
  32. data/ext/snowcrash/ext/markdown-parser/msvc/msvc.sln +38 -0
  33. data/ext/snowcrash/ext/markdown-parser/msvc/sundown/sundown.vcproj +206 -0
  34. data/ext/snowcrash/ext/markdown-parser/src/ByteBuffer.cc +92 -0
  35. data/ext/snowcrash/ext/markdown-parser/src/ByteBuffer.h +82 -0
  36. data/ext/snowcrash/ext/markdown-parser/src/MarkdownNode.cc +152 -0
  37. data/ext/snowcrash/ext/markdown-parser/src/MarkdownNode.h +103 -0
  38. data/ext/snowcrash/ext/markdown-parser/src/MarkdownParser.cc +388 -0
  39. data/ext/snowcrash/{src → ext/markdown-parser/src}/MarkdownParser.h +43 -33
  40. data/ext/snowcrash/snowcrash.gyp +114 -63
  41. data/ext/snowcrash/src/ActionParser.h +334 -398
  42. data/ext/snowcrash/src/AssetParser.h +82 -171
  43. data/ext/snowcrash/src/Blueprint.h +7 -2
  44. data/ext/snowcrash/src/BlueprintParser.h +212 -286
  45. data/ext/snowcrash/src/BlueprintUtility.h +2 -2
  46. data/ext/snowcrash/src/CBlueprint.h +1 -1
  47. data/ext/snowcrash/src/CSourceAnnotation.cc +11 -11
  48. data/ext/snowcrash/src/CSourceAnnotation.h +9 -9
  49. data/ext/snowcrash/src/CodeBlockUtility.h +199 -149
  50. data/ext/snowcrash/src/HeadersParser.h +197 -0
  51. data/ext/snowcrash/src/ParameterParser.h +429 -0
  52. data/ext/snowcrash/src/ParametersParser.h +136 -211
  53. data/ext/snowcrash/src/PayloadParser.h +458 -562
  54. data/ext/snowcrash/src/Platform.h +0 -3
  55. data/ext/snowcrash/src/ResourceGroupParser.h +183 -164
  56. data/ext/snowcrash/src/ResourceParser.h +325 -493
  57. data/ext/snowcrash/src/Section.cc +42 -0
  58. data/ext/snowcrash/src/Section.h +47 -0
  59. data/ext/snowcrash/src/SectionParser.h +229 -0
  60. data/ext/snowcrash/src/SectionParserData.h +81 -0
  61. data/ext/snowcrash/src/SectionProcessor.h +211 -0
  62. data/ext/snowcrash/src/Signature.cc +74 -0
  63. data/ext/snowcrash/src/Signature.h +32 -0
  64. data/ext/snowcrash/src/SourceAnnotation.h +7 -20
  65. data/ext/snowcrash/src/StringUtility.h +30 -10
  66. data/ext/snowcrash/src/SymbolTable.h +7 -7
  67. data/ext/snowcrash/src/UriTemplateParser.cc +10 -10
  68. data/ext/snowcrash/src/UriTemplateParser.h +11 -14
  69. data/ext/snowcrash/src/ValuesParser.h +122 -0
  70. data/ext/snowcrash/src/Version.h +2 -2
  71. data/ext/snowcrash/src/csnowcrash.cc +5 -5
  72. data/ext/snowcrash/src/csnowcrash.h +3 -3
  73. data/ext/snowcrash/src/snowcrash.cc +74 -4
  74. data/ext/snowcrash/src/snowcrash.h +9 -4
  75. data/ext/snowcrash/src/snowcrash/snowcrash.cc +16 -16
  76. data/ext/snowcrash/tools/homebrew/snowcrash.rb +3 -2
  77. data/ext/snowcrash/vcbuild.bat +13 -4
  78. data/lib/redsnow.rb +5 -5
  79. data/lib/redsnow/binding.rb +1 -1
  80. data/lib/redsnow/blueprint.rb +33 -2
  81. data/lib/redsnow/parseresult.rb +7 -4
  82. data/lib/redsnow/version.rb +1 -1
  83. data/test/redsnow_binding_test.rb +6 -6
  84. data/test/redsnow_parseresult_test.rb +1 -1
  85. metadata +62 -42
  86. data/ext/snowcrash/src/BlockUtility.h +0 -186
  87. data/ext/snowcrash/src/BlueprintParserCore.h +0 -190
  88. data/ext/snowcrash/src/BlueprintSection.h +0 -140
  89. data/ext/snowcrash/src/DescriptionSectionUtility.h +0 -156
  90. data/ext/snowcrash/src/HeaderParser.h +0 -289
  91. data/ext/snowcrash/src/ListBlockUtility.h +0 -273
  92. data/ext/snowcrash/src/ListUtility.h +0 -95
  93. data/ext/snowcrash/src/MarkdownBlock.cc +0 -176
  94. data/ext/snowcrash/src/MarkdownBlock.h +0 -93
  95. data/ext/snowcrash/src/MarkdownParser.cc +0 -266
  96. data/ext/snowcrash/src/ParameterDefinitonParser.h +0 -645
  97. data/ext/snowcrash/src/Parser.cc +0 -71
  98. data/ext/snowcrash/src/Parser.h +0 -29
  99. data/ext/snowcrash/src/ParserCore.cc +0 -120
  100. data/ext/snowcrash/src/ParserCore.h +0 -82
  101. data/ext/snowcrash/src/SectionUtility.h +0 -142
@@ -0,0 +1,103 @@
1
+ //
2
+ // MarkdownNode.h
3
+ // markdownparser
4
+ //
5
+ // Created by Zdenek Nemec on 4/16/14.
6
+ // Copyright (c) 2014 Apiary. All rights reserved.
7
+ //
8
+
9
+ #ifndef MARKDOWNPARSER_NODE_H
10
+ #define MARKDOWNPARSER_NODE_H
11
+
12
+ #include <deque>
13
+ #include <memory>
14
+ #include <iostream>
15
+ #include "ByteBuffer.h"
16
+
17
+ namespace mdp {
18
+
19
+ /**
20
+ * AST block node types
21
+ */
22
+ enum MarkdownNodeType {
23
+ RootMarkdownNodeType = 0,
24
+ CodeMarkdownNodeType,
25
+ QuoteMarkdownNodeType,
26
+ HTMLMarkdownNodeType,
27
+ HeaderMarkdownNodeType,
28
+ HRuleMarkdownNodeType,
29
+ ListItemMarkdownNodeType,
30
+ ParagraphMarkdownNodeType,
31
+ TableMarkdownNodeType,
32
+ TableRowMarkdownNodeType,
33
+ TableCellMarkdownNodeType,
34
+ UndefinedMarkdownNodeType = -1
35
+ };
36
+
37
+ /* Forward declaration of AST Node */
38
+ class MarkdownNode;
39
+
40
+ /** Markdown AST nodes collection */
41
+ typedef std::deque<MarkdownNode> MarkdownNodes;
42
+
43
+ /**
44
+ * AST node
45
+ */
46
+ class MarkdownNode {
47
+ public:
48
+ typedef int Data;
49
+
50
+ /** Node type */
51
+ MarkdownNodeType type;
52
+
53
+ /** Textual content, where applicable */
54
+ ByteBuffer text;
55
+
56
+ /** Additinonal data, if applicable */
57
+ Data data;
58
+
59
+ /** Source map of the node including any and all children */
60
+ BytesRangeSet sourceMap;
61
+
62
+ /** Parent node, throws exception if no parent is defined */
63
+ MarkdownNode& parent();
64
+ const MarkdownNode& parent() const;
65
+
66
+ /** Sets parent node */
67
+ void setParent(MarkdownNode *parent);
68
+
69
+ /** True if section's parent is specified, false otherwise */
70
+ bool hasParent() const;
71
+
72
+ /** Children nodes */
73
+ MarkdownNodes& children();
74
+ const MarkdownNodes& children() const;
75
+
76
+ /** Constructor */
77
+ MarkdownNode(MarkdownNodeType type_ = UndefinedMarkdownNodeType,
78
+ MarkdownNode *parent_ = NULL,
79
+ const ByteBuffer& text_ = ByteBuffer(),
80
+ const Data& data_ = Data());
81
+
82
+ /** Copy constructor */
83
+ MarkdownNode(const MarkdownNode& rhs);
84
+
85
+ /** Assignment operator */
86
+ MarkdownNode& operator=(const MarkdownNode& rhs);
87
+
88
+ /** Destructor */
89
+ ~MarkdownNode();
90
+
91
+ /** Prints the node to the stdout */
92
+ void printNode(size_t level = 0) const;
93
+
94
+ private:
95
+ MarkdownNode* m_parent;
96
+ std::auto_ptr<MarkdownNodes> m_children;
97
+ };
98
+
99
+ /** Markdown AST nodes collection iterator */
100
+ typedef MarkdownNodes::iterator MarkdownNodeIterator;
101
+ }
102
+
103
+ #endif
@@ -0,0 +1,388 @@
1
+ //
2
+ // MarkdownParser.cc
3
+ // markdownparser
4
+ //
5
+ // Created by Zdenek Nemec on 4/18/14.
6
+ // Copyright (c) 2014 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include <cstring>
10
+ #include <stdexcept>
11
+ #include "MarkdownParser.h"
12
+
13
+ using namespace mdp;
14
+
15
+ const size_t MarkdownParser::OutputUnitSize = 64;
16
+ const size_t MarkdownParser::MaxNesting = 16;
17
+ const int MarkdownParser::ParserExtensions = MKDEXT_FENCED_CODE | MKDEXT_NO_INTRA_EMPHASIS | MKDEXT_LAX_SPACING /*| MKDEXT_TABLES */;
18
+
19
+ #define NO_WORKING_NODE_ERR std::logic_error("no working node")
20
+ #define WORKING_NODE_MISMATCH_ERR std::logic_error("working node mismatch")
21
+
22
+ /**
23
+ * \brief Create a byte buffer from a sundown buffer
24
+ */
25
+ static ByteBuffer ByteBufferFromSundown(const struct buf *text)
26
+ {
27
+ if (!text || !text->data || !text->size)
28
+ return ByteBuffer();
29
+
30
+ return ByteBuffer(reinterpret_cast<char *>(text->data), text->size);
31
+ }
32
+
33
+ MarkdownParser::MarkdownParser()
34
+ : m_workingNode(NULL), m_listBlockContext(false), m_source(NULL), m_sourceLength(0)
35
+ {
36
+ }
37
+
38
+ void MarkdownParser::parse(const ByteBuffer& source, MarkdownNode& ast)
39
+ {
40
+ ast = MarkdownNode();
41
+ m_workingNode = &ast;
42
+ m_workingNode->type = RootMarkdownNodeType;
43
+ m_workingNode->sourceMap.push_back(BytesRange(0, source.length()));
44
+ m_source = &source;
45
+ m_sourceLength = source.length();
46
+ m_listBlockContext = false;
47
+
48
+ RenderCallbacks callbacks = renderCallbacks();
49
+
50
+ ::sd_markdown *sundown = ::sd_markdown_new(ParserExtensions, MaxNesting, &callbacks, renderCallbackData());
51
+ ::buf *output = ::bufnew(OutputUnitSize);
52
+
53
+ ::sd_markdown_render(output, reinterpret_cast<const uint8_t *>(source.c_str()), source.length(), sundown);
54
+
55
+ ::bufrelease(output);
56
+ ::sd_markdown_free(sundown);
57
+
58
+ m_workingNode = NULL;
59
+ m_source = NULL;
60
+ m_sourceLength = 0;
61
+ m_listBlockContext = false;
62
+
63
+ #ifdef DEBUG
64
+ ast.printNode();
65
+ #endif
66
+ }
67
+
68
+ MarkdownParser::RenderCallbacks MarkdownParser::renderCallbacks()
69
+ {
70
+ RenderCallbacks callbacks;
71
+ ::memset(&callbacks, 0, sizeof(RenderCallbacks));
72
+
73
+ callbacks.blockcode = &MarkdownParser::renderBlockCode;
74
+ callbacks.blockquote = &MarkdownParser::renderQuote;
75
+ callbacks.blockhtml = &MarkdownParser::renderHTML;
76
+ callbacks.header = &MarkdownParser::renderHeader;
77
+ callbacks.hrule = &MarkdownParser::renderHorizontalRule;
78
+ callbacks.list = &MarkdownParser::renderList;
79
+ callbacks.listitem = &MarkdownParser::renderListItem;
80
+ callbacks.paragraph = &MarkdownParser::renderParagraph;
81
+
82
+ callbacks.table = NULL;
83
+ callbacks.table_row = NULL;
84
+ callbacks.table_cell = NULL;
85
+
86
+ callbacks.blockquote_begin = &MarkdownParser::beginQuote;
87
+ callbacks.list_begin = &MarkdownParser::beginList;
88
+ callbacks.listitem_begin = &MarkdownParser::beginListItem;
89
+
90
+ callbacks.block_did_parse = &MarkdownParser::blockDidParse;
91
+
92
+ return callbacks;
93
+ }
94
+
95
+ MarkdownParser::RenderCallbackData MarkdownParser::renderCallbackData()
96
+ {
97
+ return this;
98
+ }
99
+
100
+ void MarkdownParser::renderHeader(struct buf *ob, const struct buf *text, int level, void *opaque)
101
+ {
102
+ if (!opaque)
103
+ return;
104
+
105
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
106
+ p->renderHeader(ByteBufferFromSundown(text), level);
107
+ }
108
+
109
+ void MarkdownParser::renderHeader(const ByteBuffer& text, int level)
110
+ {
111
+ if (!m_workingNode)
112
+ throw NO_WORKING_NODE_ERR;
113
+
114
+ MarkdownNode node(HeaderMarkdownNodeType, m_workingNode, text, level);
115
+ m_workingNode->children().push_back(node);
116
+ }
117
+
118
+ void MarkdownParser::beginList(int flags, void *opaque)
119
+ {
120
+ if (!opaque)
121
+ return;
122
+
123
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
124
+ p->beginList(flags);
125
+ }
126
+
127
+ void MarkdownParser::beginList(int flags)
128
+ {
129
+ if (!m_workingNode)
130
+ throw NO_WORKING_NODE_ERR;
131
+ }
132
+
133
+ void MarkdownParser::renderList(struct buf *ob, const struct buf *text, int flags, void *opaque)
134
+ {
135
+ if (!opaque)
136
+ return;
137
+
138
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
139
+ p->renderList(ByteBufferFromSundown(text), flags);
140
+ }
141
+
142
+ void MarkdownParser::renderList(const ByteBuffer& text, int flags)
143
+ {
144
+ m_listBlockContext = true;
145
+ }
146
+
147
+ void MarkdownParser::beginListItem(int flags, void *opaque)
148
+ {
149
+ if (!opaque)
150
+ return;
151
+
152
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
153
+ p->beginListItem(flags);
154
+ }
155
+
156
+ void MarkdownParser::beginListItem(int flags)
157
+ {
158
+ if (!m_workingNode)
159
+ throw NO_WORKING_NODE_ERR;
160
+
161
+ MarkdownNode node(ListItemMarkdownNodeType, m_workingNode, ByteBuffer(), flags);
162
+ m_workingNode->children().push_back(node);
163
+
164
+ // Push context
165
+ m_workingNode = &m_workingNode->children().back();
166
+ }
167
+
168
+ void MarkdownParser::renderListItem(struct buf *ob, const struct buf *text, int flags, void *opaque)
169
+ {
170
+ if (!opaque)
171
+ return;
172
+
173
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
174
+ p->renderListItem(ByteBufferFromSundown(text), flags);
175
+ }
176
+
177
+ void MarkdownParser::renderListItem(const ByteBuffer& text, int flags)
178
+ {
179
+ if (!m_workingNode)
180
+ throw NO_WORKING_NODE_ERR;
181
+
182
+ if (m_workingNode->type != ListItemMarkdownNodeType)
183
+ throw WORKING_NODE_MISMATCH_ERR;
184
+
185
+ // No "inline" list items:
186
+ // Instead of storing the text on the list item
187
+ // create the artificial paragraph node to store the text.
188
+ if (m_workingNode->children().empty() ||
189
+ m_workingNode->children().front().type != ParagraphMarkdownNodeType) {
190
+ MarkdownNode textNode(ParagraphMarkdownNodeType, m_workingNode, text);
191
+ m_workingNode->children().push_front(textNode);
192
+ }
193
+
194
+ m_workingNode->data = flags;
195
+
196
+ // Pop context
197
+ m_workingNode = &m_workingNode->parent();
198
+ }
199
+
200
+ void MarkdownParser::renderBlockCode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
201
+ {
202
+ if (!opaque)
203
+ return;
204
+
205
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
206
+ p->renderBlockCode(ByteBufferFromSundown(text), ByteBufferFromSundown(lang));
207
+ }
208
+
209
+ void MarkdownParser::renderBlockCode(const ByteBuffer& text, const ByteBuffer& language)
210
+ {
211
+ if (!m_workingNode)
212
+ throw NO_WORKING_NODE_ERR;
213
+
214
+ MarkdownNode node(CodeMarkdownNodeType, m_workingNode, text);
215
+ m_workingNode->children().push_back(node);
216
+ }
217
+
218
+ void MarkdownParser::renderParagraph(struct buf *ob, const struct buf *text, void *opaque)
219
+ {
220
+ if (!opaque)
221
+ return;
222
+
223
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
224
+ p->renderParagraph(ByteBufferFromSundown(text));
225
+ }
226
+
227
+ void MarkdownParser::renderParagraph(const ByteBuffer& text)
228
+ {
229
+ if (!m_workingNode)
230
+ throw NO_WORKING_NODE_ERR;
231
+
232
+ MarkdownNode node(ParagraphMarkdownNodeType, m_workingNode, text);
233
+ m_workingNode->children().push_back(node);
234
+ }
235
+
236
+ void MarkdownParser::renderHorizontalRule(struct buf *ob, void *opaque)
237
+ {
238
+ if (!opaque)
239
+ return;
240
+
241
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
242
+ p->renderHorizontalRule();
243
+ }
244
+
245
+ void MarkdownParser::renderHorizontalRule()
246
+ {
247
+ if (!m_workingNode)
248
+ throw NO_WORKING_NODE_ERR;
249
+
250
+ MarkdownNode node(HRuleMarkdownNodeType, m_workingNode, ByteBuffer(), MarkdownNode::Data());
251
+ m_workingNode->children().push_back(node);
252
+ }
253
+
254
+ void MarkdownParser::renderHTML(struct buf *ob, const struct buf *text, void *opaque)
255
+ {
256
+ if (!opaque)
257
+ return;
258
+
259
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
260
+ p->renderHTML(ByteBufferFromSundown(text));
261
+ }
262
+
263
+ void MarkdownParser::renderHTML(const ByteBuffer& text)
264
+ {
265
+ if (!m_workingNode)
266
+ throw NO_WORKING_NODE_ERR;
267
+
268
+ MarkdownNode node(HTMLMarkdownNodeType, m_workingNode, text);
269
+ m_workingNode->children().push_back(node);
270
+ }
271
+
272
+ void MarkdownParser::beginQuote(void *opaque)
273
+ {
274
+ if (!opaque)
275
+ return;
276
+
277
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
278
+ p->beginQuote();
279
+ }
280
+
281
+ void MarkdownParser::beginQuote()
282
+ {
283
+ if (!m_workingNode)
284
+ throw NO_WORKING_NODE_ERR;
285
+
286
+ MarkdownNode node(QuoteMarkdownNodeType, m_workingNode);
287
+ m_workingNode->children().push_back(node);
288
+
289
+ // Push context
290
+ m_workingNode = &m_workingNode->children().back();
291
+ }
292
+
293
+ void MarkdownParser::renderQuote(struct buf *ob, const struct buf *text, void *opaque)
294
+ {
295
+ if (!opaque)
296
+ return;
297
+
298
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
299
+ p->renderQuote(ByteBufferFromSundown(text));
300
+ }
301
+
302
+ void MarkdownParser::renderQuote(const ByteBuffer& text)
303
+ {
304
+ if (!m_workingNode)
305
+ throw NO_WORKING_NODE_ERR;
306
+
307
+ if (m_workingNode->type != QuoteMarkdownNodeType)
308
+ throw WORKING_NODE_MISMATCH_ERR;
309
+
310
+ m_workingNode->text = text;
311
+
312
+ // Pop context
313
+ m_workingNode = &m_workingNode->parent();
314
+ }
315
+
316
+ void MarkdownParser::blockDidParse(const src_map* map, const uint8_t *txt_data, size_t size, void *opaque)
317
+ {
318
+ if (!opaque || !map)
319
+ return;
320
+
321
+ BytesRangeSet sourceMap;
322
+ for (size_t i = 0; i < map->size; ++i) {
323
+ BytesRange byteRange(((range *)map->item[i])->loc, ((range *)map->item[i])->len);
324
+ sourceMap.push_back(byteRange);
325
+ }
326
+
327
+ MarkdownParser *p = static_cast<MarkdownParser *>(opaque);
328
+ p->blockDidParse(sourceMap);
329
+ }
330
+
331
+ void MarkdownParser::blockDidParse(const BytesRangeSet& sourceMap)
332
+ {
333
+ if (m_listBlockContext) {
334
+ m_listBlockContext = false; // ignore list blocks events
335
+ return;
336
+ }
337
+
338
+ if (!m_workingNode)
339
+ throw NO_WORKING_NODE_ERR;
340
+
341
+ if (m_workingNode->children().empty())
342
+ return;
343
+
344
+ MarkdownNode &lMarkdownNode = m_workingNode->children().back();
345
+
346
+ // Sundown +1 newline compensation
347
+ //
348
+ // If new source map would exceed the actual source size
349
+ // this happens when sundown appends an artifical new line
350
+ // truncate the source map length to match the actual size
351
+ if (sourceMap.back().location + sourceMap.back().length > m_sourceLength) {
352
+
353
+ size_t workMapLength = m_sourceLength - sourceMap.back().location;
354
+ if (!workMapLength)
355
+ return; // Ignore any artifical trailing new lines in source maps
356
+
357
+ BytesRangeSet workMap = sourceMap;
358
+ workMap.back().length = workMapLength;
359
+ lMarkdownNode.sourceMap.append(workMap);
360
+ }
361
+ else {
362
+
363
+ lMarkdownNode.sourceMap.append(sourceMap);
364
+ }
365
+
366
+ // No "inline" list items:
367
+ // Share the list item source map with its artifical node, if exists.
368
+ if (lMarkdownNode.type == ListItemMarkdownNodeType &&
369
+ !lMarkdownNode.children().empty() &&
370
+ lMarkdownNode.children().front().sourceMap.empty()) {
371
+
372
+ ByteBuffer& buffer = lMarkdownNode.children().front().text;
373
+ ByteBuffer mapped = MapBytesRangeSet(sourceMap, *m_source);
374
+ size_t pos = mapped.find(buffer);
375
+
376
+ if (pos != mapped.npos) {
377
+ BytesRange range = sourceMap.front();
378
+ range.location += pos;
379
+ range.length = buffer.length();
380
+ BytesRangeSet newMap;
381
+ newMap.push_back(range);
382
+ lMarkdownNode.children().front().sourceMap.append(newMap);
383
+ }
384
+ else {
385
+ lMarkdownNode.children().front().sourceMap.append(sourceMap);
386
+ }
387
+ }
388
+ }