This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathSignatureSectionProcessor.h
445 lines (352 loc) · 15.6 KB
/
SignatureSectionProcessor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//
// SignatureSectionProcessor.h
// snowcrash
//
// Created by Pavan Kumar Sunkara on 10/14/14.
// Copyright (c) 2014 Apiary Inc. All rights reserved.
//
#ifndef SNOWCRASH_SIGNATURESECTIONPROCESSOR_H
#define SNOWCRASH_SIGNATURESECTIONPROCESSOR_H
#include "SectionProcessor.h"
namespace scpl
{
using mdp::MarkdownNode;
using mdp::MarkdownNodes;
using mdp::MarkdownNodeIterator;
const char EscapeCharacter = '`';
/**
* \brief Signature Section Processor Base
*
* Defines default behaviours for Section Processor interface for Signature sections
*/
template <typename T>
struct SignatureSectionProcessorBase : public snowcrash::SectionProcessorBase<T> {
/**
* \brief Signature traits of the section
*
* \return Signature traits given for section
*/
static SignatureTraits signatureTraits()
{
SignatureTraits signatureTraits;
return signatureTraits;
};
/**
* \brief Process section signature markdown node (Default)
*/
static MarkdownNodeIterator processSignature(const MarkdownNodeIterator& node,
const MarkdownNodes& siblings,
snowcrash::SectionParserData& pd,
snowcrash::SectionLayout& layout,
const snowcrash::ParseResultRef<T>& out)
{
// Get the signature traits of the section
SignatureTraits signatureTraits = snowcrash::SectionProcessor<T>::signatureTraits();
// Parse Signature
Signature signature = snowcrash::SectionProcessor<T>::parseSignature(node, pd, signatureTraits, out.report);
// Do section specific logic using the signature data
return snowcrash::SectionProcessor<T>::finalizeSignature(node, pd, signature, out);
};
/**
* \brief Parse section signature markdown node
*
* \param node Markdown node
* \param pd Section Parser data
* \param traits Signature traits of the section signature
* \param report Parse result report of the section
* \param subjectOrig The string that needs to be parsed
*
* \return Signature data
*/
static Signature parseSignature(const MarkdownNodeIterator& node,
snowcrash::SectionParserData& pd,
const SignatureTraits& traits,
snowcrash::Report& report,
const mdp::ByteBuffer& subjectOrig = "")
{
Signature signature;
mdp::ByteBuffer subject = subjectOrig;
if (subject.empty()) {
subject = snowcrash::GetFirstLine(node->text, signature.remainingContent);
snowcrash::TrimString(subject);
}
if (traits.identifierTrait && !subject.empty()) {
parseSignatureIdentifier(traits, report, subject, signature);
if (signature.identifier.empty()) {
// WARN: Empty identifier
mdp::CharactersRangeSet sourceMap
= mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceCharacterIndex);
report.warnings.push_back(
snowcrash::Warning("no identifier specified", snowcrash::EmptyDefinitionWarning, sourceMap));
}
}
// Make sure values exist
if (traits.valuesTrait && !subject.empty() && subject[0] != Delimiters::AttributesBeginDelimiter) {
// When subject starts with values, add a ':' for easier processing
if (!traits.identifierTrait) {
subject = traits.delimiters.valuesDelimiter + subject;
}
if (subject[0] == traits.delimiters.valuesDelimiter) {
parseSignatureValues(traits, report, subject, signature);
if (signature.values.empty()) {
// WARN: Empty values
mdp::CharactersRangeSet sourceMap
= mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceCharacterIndex);
report.warnings.push_back(
snowcrash::Warning("no value(s) specified", snowcrash::EmptyDefinitionWarning, sourceMap));
}
}
}
if (traits.attributesTrait && !subject.empty()
&& subject.substr(0, traits.delimiters.contentDelimiter.length())
!= traits.delimiters.contentDelimiter) {
parseSignatureAttributes(report, subject, signature);
}
if (traits.contentTrait && !subject.empty()
&& subject.substr(0, traits.delimiters.contentDelimiter.length())
== traits.delimiters.contentDelimiter) {
subject = subject.substr(traits.delimiters.contentDelimiter.length());
snowcrash::TrimString(subject);
signature.content = subject;
}
return signature;
};
/**
* \brief Use the signature data to do section specific logic
*
* \param node Markdown node
* \param pd Section Parser data
* \param signature Signature data
* \param out Parse result of the section
*
* \return Result of process operation
*/
static MarkdownNodeIterator finalizeSignature(const MarkdownNodeIterator& node,
snowcrash::SectionParserData& pd,
const Signature& signature,
const snowcrash::ParseResultRef<T>& out)
{
return ++MarkdownNodeIterator(node);
};
/**
* \brief Parse the identifier from the signature
*
* \param traits Signature traits of the section signature
* \param report Parse Report
* \param subject String that needs to be parsed
* (which will be stripped of the parsed characters)
* \param out Signature data structure
*/
static void parseSignatureIdentifier(
const SignatureTraits& traits, snowcrash::Report& report, mdp::ByteBuffer& subject, Signature& out)
{
snowcrash::TrimString(subject);
// `*` and `_` are markdown characters for emphasis
std::string escapeCharacters = "*_`";
size_t i = 0;
mdp::ByteBuffer identifier;
// Traverse over the string
while (i < subject.length()) {
if (escapeCharacters.find(subject[i]) != std::string::npos) {
// If escaped string, retrieve it and strip it from the subject
mdp::ByteBuffer escapedString = snowcrash::RetrieveEscaped(subject, i);
if (!escapedString.empty()) {
identifier += escapedString;
i = 0;
} else {
identifier += subject[i];
i++;
}
} else if ((traits.valuesTrait && subject[i] == traits.delimiters.valuesDelimiter)
|| (traits.attributesTrait && subject[i] == Delimiters::AttributesBeginDelimiter)
|| (traits.contentTrait
&& subject.substr(i, traits.delimiters.contentDelimiter.length())
== traits.delimiters.contentDelimiter)) {
// If identifier ends, strip it from the subject
subject = subject.substr(i);
i = 0;
break;
} else {
identifier += subject[i];
i++;
}
}
// Assign the identifier
snowcrash::TrimString(identifier);
if (!identifier.empty()) {
out.identifier = identifier;
}
// If the subject ended with the identifier, strip it from the subject
if (i == subject.length()) {
subject = "";
}
snowcrash::TrimString(subject);
};
/**
* \brief Parse the values from the signature
*
* \param traits Signature traits of the section signature
* \param report Parse Report
* \param subject String that needs to be parsed
* (which will be stripped of the parsed characters)
* \param out Signature data structure
*/
static void parseSignatureValues(
const SignatureTraits& traits, snowcrash::Report& report, mdp::ByteBuffer& subject, Signature& out)
{
// Remove leading delimiter
subject = subject.substr(1);
snowcrash::TrimString(subject);
size_t i = 0;
mdp::ByteBuffer value;
out.value = subject;
// Traverse over the string
while (i < subject.length()) {
if (subject[i] == EscapeCharacter) {
// If escaped string, retrieve it and strip it from subject
mdp::ByteBuffer escapedString = snowcrash::RetrieveEscaped(subject, i);
if (!escapedString.empty()) {
value += escapedString;
i = 0;
} else {
value += subject[i];
i++;
}
} else if (subject[i] == Delimiters::ValueDelimiter) {
// If found value delimiter, add the value and strip it from subject
subject = subject.substr(i + 1);
snowcrash::TrimString(subject);
snowcrash::TrimString(value);
out.values.push_back(snowcrash::StripBackticks(value));
value = "";
i = 0;
} else if ((traits.attributesTrait && subject[i] == Delimiters::AttributesBeginDelimiter)
|| (traits.contentTrait
&& subject.substr(i, traits.delimiters.contentDelimiter.length())
== traits.delimiters.contentDelimiter)) {
// If values section ends, strip it from subject
subject = subject.substr(i);
i = 0;
break;
} else {
value += subject[i];
i++;
}
}
// Add the value at the end of values section if present
snowcrash::TrimString(value);
if (!value.empty()) {
out.values.push_back(value);
}
// If the subject ended with the values, strip the last value from the subject
if (i == subject.length()) {
subject = "";
}
snowcrash::TrimString(subject);
// Fill signature value with the string which was stripped from subject
out.value = out.value.substr(0, out.value.length() - subject.length());
snowcrash::TrimString(out.value);
out.value = snowcrash::StripBackticks(out.value);
};
/**
* \brief Parse the attributes from the signature
*
* \param report Parse Report
* \param subject String that needs to be parsed
* (which will be stripped of the parsed characters)
* \param out Signature data structure
*/
static void parseSignatureAttributes(snowcrash::Report& report, mdp::ByteBuffer& subject, Signature& out)
{
if (subject[0] != Delimiters::AttributesBeginDelimiter) {
return;
}
bool attributesNotFinished = true;
// While there are attributes still left
while (attributesNotFinished) {
// Retrieve attribute
mdp::ByteBuffer attribute = matchBrackets(subject, 0, Delimiters::AttributesEndDelimiter, true);
size_t length = attribute.size();
// If the last char is not an attribute delimiter, attributes are finished
if (attribute[length - 1] != Delimiters::AttributeDelimiter) {
attributesNotFinished = false;
} else {
attribute = attribute.substr(0, length - 1);
// For easier processing
subject = Delimiters::AttributesBeginDelimiter + subject;
}
snowcrash::TrimString(attribute);
if (!attribute.empty()) {
out.attributes.push_back(attribute);
}
};
snowcrash::TrimString(subject);
};
/**
* \brief Find the matching bracket while ignoring any nested brackets and return the string
* enclosed by them, while also stripping the string that needs to be parsed
*
* \param subject The string that needs to be parsed
* \param begin Character index representing the beginning of the bracket that needs to be matched
* \param endBracket The type of bracket that needs to be matched
* \param splitByAttribute If this is true, we need to return when we find a top-level attribute delimiter
* \param clearAtEnd If this is true, the string will be cleared at the end of parsing
*
* \return String inside the given brackets. If not splitting by comma, append the brackets too
*/
static mdp::ByteBuffer matchBrackets(mdp::ByteBuffer& subject,
size_t begin,
const char endBracket,
const bool splitByAttribute = false,
const bool clearAtEnd = false)
{
size_t i = begin + 1;
mdp::ByteBuffer returnString;
// Append the beginning bracket
if (!splitByAttribute) {
returnString += subject[begin];
}
while (i < subject.length()) {
if (subject[i] == EscapeCharacter) {
// If escaped string, retrieve it and strip it from subject
mdp::ByteBuffer escapedString = snowcrash::RetrieveEscaped(subject, i);
if (!escapedString.empty()) {
returnString += escapedString;
i = 0;
} else {
returnString += subject[i];
i++;
}
} else if (subject[i] == '[') {
returnString += matchBrackets(subject, i, ']', false, true);
i = 0;
} else if (subject[i] == '(') {
returnString += matchBrackets(subject, i, ')', false, true);
i = 0;
} else if (subject[i] == endBracket) {
// Append the ending bracket
if (!splitByAttribute) {
returnString += subject[i];
}
subject = subject.substr(i + 1);
i = 0;
break;
} else if (splitByAttribute && subject[i] == Delimiters::AttributeDelimiter) {
// Return when encountering comma
returnString += subject[i];
subject = subject.substr(i + 1);
i = 0;
break;
} else {
returnString += subject[i];
i++;
}
}
if (i == subject.length() && clearAtEnd) {
subject.clear();
}
return returnString;
}
};
}
#endif